@@ -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 // remove request, this will fail silently if the request doesn't exist
232242 $ this ->removeMemberUID ($ new_user ->uid );
@@ -329,7 +339,7 @@ private function init(): void
329339 \ensure (!$ this ->entry ->exists ());
330340 $ nextGID = $ this ->LDAP ->getNextPIGIDNumber ();
331341 $ this ->entry ->create ([
332- "objectclass " => UnityLDAP:: POSIX_GROUP_CLASS ,
342+ "objectclass " => [ " unityClusterPIGroup " , " posixGroup " , " top " ] ,
333343 "gidnumber " => strval ($ nextGID ),
334344 "memberuid " => [$ owner ->uid ],
335345 ]);
@@ -385,4 +395,40 @@ public function getGroupMembersAttributes(array $attributes, array $default_valu
385395 $ default_values ,
386396 );
387397 }
398+
399+ public function getIsDisabled (): bool
400+ {
401+ $ value = $ this ->entry ->getAttribute ("isDisabled " );
402+ switch (count ($ value )) {
403+ case 0 :
404+ return false ;
405+ case 1 :
406+ switch ($ value [0 ]) {
407+ case "TRUE " :
408+ return true ;
409+ case "FALSE " :
410+ return false ;
411+ default :
412+ throw new \RuntimeException (
413+ sprintf (
414+ "unexpected value for isDisabled: '%s'. expected 'TRUE' or 'FALSE' " ,
415+ $ value [0 ],
416+ ),
417+ );
418+ }
419+ default :
420+ throw new \RuntimeException (
421+ sprintf (
422+ "expected value of length 0 or 1, found value %s of length %s " ,
423+ jsonEncode ($ value ),
424+ count ($ value ),
425+ ),
426+ );
427+ }
428+ }
429+
430+ public function setIsDisabled (bool $ new_value ): void
431+ {
432+ $ this ->entry ->setAttribute ("isDisabled " , $ new_value ? "TRUE " : "FALSE " );
433+ }
388434}
0 commit comments