@@ -40,7 +40,7 @@ public function __toString(): string
4040 public function requestGroup (?bool $ send_mail_to_admins = null , bool $ send_mail = true ): void
4141 {
4242 $ send_mail_to_admins ??= CONFIG ["mail " ]["send_pimesg_to_admins " ];
43- if ($ this ->exists ()) {
43+ if ($ this ->exists () && ! $ this -> getIsDisabled () ) {
4444 return ;
4545 }
4646 if ($ this ->SQL ->accDeletionRequestExists ($ this ->getOwner ()->uid )) {
@@ -63,18 +63,64 @@ public function requestGroup(?bool $send_mail_to_admins = null, bool $send_mail
6363 }
6464 }
6565
66+ public function disable (bool $ send_mail = true ): void
67+ {
68+ $ this ->SQL ->addLog ("disable_pi_group " , $ this ->gid );
69+ $ memberuids = $ this ->getMemberUIDs ();
70+ if ($ send_mail ) {
71+ $ member_attributes = $ this ->LDAP ->getUsersAttributes ($ memberuids , ["mail " ]);
72+ $ member_mails = array_map (fn ($ x ) => $ x ["mail " ][0 ], $ member_attributes );
73+ $ this ->MAILER ->sendMail ($ member_mails , "group_disabled " , [
74+ "group_name " => $ this ->gid ,
75+ ]);
76+ }
77+ $ this ->setIsDisabled (true );
78+ if (count ($ memberuids ) > 0 ) {
79+ $ this ->entry ->setAttribute ("memberuid " , []);
80+ }
81+ // TODO optimmize
82+ // UnityUser::__construct() makes one LDAP query for each user
83+ // updateIsQualified() makes one LDAP query for each member
84+ // if user is no longer in any PI group, disqualify them
85+ // FIXME uncomment
86+ // foreach ($memberuids as $uid) {
87+ // $user = new UnityUser($uid, $this->LDAP, $this->SQL, $this->MAILER, $this->WEBHOOK);
88+ // $user->updateIsQualified($send_mail);
89+ // }
90+ }
91+
92+ private function reenable (bool $ send_mail = true )
93+ {
94+ $ this ->SQL ->addLog ("reenabled_pi_group " , $ this ->gid );
95+ if ($ send_mail ) {
96+ $ this ->MAILER ->sendMail ($ this ->getOwner ()->getMail (), "group_reenabled " , [
97+ "group_name " => $ this ->gid ,
98+ ]);
99+ }
100+ $ this ->setIsDisabled (false );
101+ $ owner_uid = $ this ->getOwner ()->uid ;
102+ if (!$ this ->memberUIDExists ($ owner_uid )) {
103+ $ this ->addMemberUID ($ owner_uid );
104+ }
105+ // FIXME uncomment
106+ // $this->getOwner()->updateIsQualified($send_mail);
107+ }
108+
66109 /**
67110 * This method will create the group (this is what is executed when an admin approved the group)
68111 */
69112 public function approveGroup (bool $ send_mail = true ): void
70113 {
71114 $ uid = $ this ->getOwner ()->uid ;
72115 $ request = $ this ->SQL ->getRequest ($ uid , UnitySQL::REQUEST_BECOME_PI );
73- if ($ this ->exists ()) {
74- return ;
75- }
76116 \ensure ($ this ->getOwner ()->exists ());
77- $ this ->init ();
117+ if (!$ this ->entry ->exists ()) {
118+ $ this ->init ();
119+ } elseif ($ this ->getIsDisabled ()) {
120+ $ this ->reenable ();
121+ } else {
122+ throw new Exception ("cannot approve group that already exists and is not disabled " );
123+ }
78124 $ this ->SQL ->removeRequest ($ this ->getOwner ()->uid , UnitySQL::REQUEST_BECOME_PI );
79125 $ this ->SQL ->addLog ("approved_group " , $ this ->getOwner ()->uid );
80126 if ($ send_mail ) {
@@ -126,42 +172,6 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
126172 }
127173 }
128174
129- // /**
130- // * This method will delete the group, either by admin action or PI action
131- // */
132- // public function removeGroup($send_mail = true)
133- // {
134- // // remove any pending requests
135- // // this will silently fail if the request doesn't exist (which is what we want)
136- // $this->SQL->removeRequests($this->gid);
137-
138- // // we don't need to do anything extra if the group is already deleted
139- // if (!$this->exists()) {
140- // return;
141- // }
142-
143- // // first, we must record the users in the group currently
144- // $users = $this->getGroupMembers();
145-
146- // // now we delete the ldap entry
147- // $this->entry->ensureExists();
148- // $this->entry->delete();
149-
150- // // Logs the change
151- // $this->SQL->addLog("removed_group", $this->gid);
152-
153- // // send email to every user of the now deleted PI group
154- // if ($send_mail) {
155- // foreach ($users as $user) {
156- // $this->MAILER->sendMail(
157- // $user->getMail(),
158- // "group_disband",
159- // array("group_name" => $this->gid)
160- // );
161- // }
162- // }
163- // }
164-
165175 /**
166176 * This method is executed when a user is approved to join the group
167177 * (either by admin or the group owner)
@@ -226,7 +236,7 @@ public function removeUser(UnityUser $new_user, bool $send_mail = true): void
226236 return ;
227237 }
228238 if ($ new_user ->uid == $ this ->getOwner ()->uid ) {
229- throw new Exception ("Cannot delete group owner from group. Disband group instead " );
239+ throw new Exception ("Cannot delete group owner from group. Disable group instead " );
230240 }
231241 $ this ->removeMemberUID ($ new_user ->uid );
232242 $ this ->SQL ->addLog (
@@ -328,7 +338,7 @@ private function init(): void
328338 \ensure (!$ this ->entry ->exists ());
329339 $ nextGID = $ this ->LDAP ->getNextPIGIDNumber ();
330340 $ this ->entry ->create ([
331- "objectclass " => UnityLDAP:: POSIX_GROUP_CLASS ,
341+ "objectclass " => [ " unityClusterPIGroup " , " posixGroup " , " top " ] ,
332342 "gidnumber " => strval ($ nextGID ),
333343 "memberuid " => [$ owner ->uid ],
334344 ]);
@@ -383,4 +393,40 @@ public function getGroupMembersAttributes(array $attributes, array $default_valu
383393 $ default_values ,
384394 );
385395 }
396+
397+ public function getIsDisabled (): bool
398+ {
399+ $ value = $ this ->entry ->getAttribute ("isDisabled " );
400+ switch (count ($ value )) {
401+ case 0 :
402+ return false ;
403+ case 1 :
404+ switch ($ value [0 ]) {
405+ case "TRUE " :
406+ return true ;
407+ case "FALSE " :
408+ return false ;
409+ default :
410+ throw new \RuntimeException (
411+ sprintf (
412+ "unexpected value for isDisabled: '%s'. expected 'TRUE' or 'FALSE' " ,
413+ $ value [0 ],
414+ ),
415+ );
416+ }
417+ default :
418+ throw new \RuntimeException (
419+ sprintf (
420+ "expected value of length 0 or 1, found value %s of length %s " ,
421+ jsonEncode ($ value ),
422+ count ($ value ),
423+ ),
424+ );
425+ }
426+ }
427+
428+ public function setIsDisabled (bool $ new_value ): void
429+ {
430+ $ this ->entry ->setAttribute ("isDisabled " , $ new_value ? "TRUE " : "FALSE " );
431+ }
386432}
0 commit comments