diff --git a/classes/components/AccountComponent.php b/classes/components/AccountComponent.php index 2d75631..f07d24b 100644 --- a/classes/components/AccountComponent.php +++ b/classes/components/AccountComponent.php @@ -409,8 +409,12 @@ class AccountComponent { */ public function delete(int $userID) : bool { + /*//Delete all group memberships + if(!components()->groups->deleteAllUsersGroups($userID)) + return FALSE; + //Delete user comments - /*if(!components()->comments->deleteAllUser($userID)) + if(!components()->comments->deleteAllUser($userID)) return false; //Delete user posts diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index f5ec882..513c705 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -521,6 +521,20 @@ class GroupsComponent { == GroupMember::ADMINISTRATOR; } + /** + * Check out whether a user is the last administrator of a group + * or not + * + * @param int $userID The ID of the user to check + * @param int $groupID The ID of the target group + * @return bool TRUE if the user is an admin and the last one of the group + * and FALSE else + */ + public function isLastAdmin(int $userID, int $groupID) : bool { + return $this->isAdmin($userID, $groupID) + && $this->countMembersAtLevel($groupID, GroupMember::ADMINISTRATOR) === 1; + } + /** * Check whether a group is open or not * @@ -736,6 +750,36 @@ class GroupsComponent { return TRUE; } + /** + * Delete all the groups a user belongs to + * + * @param int $userID The ID of the target user + * @return bool TRUE in case of success / FALSE else + */ + public function deleteAllUsersGroups(int $userID) : bool { + + //Get all user gropus + foreach($this->getListUser($userID) as $groupID){ + + //Get information about user membership to determine whether the group has to be + // deleted or not, to do so we check whether the user is the last administrator + // of the group or not + if($this->isLastAdmin($userID, $groupID)) { + if(!$this->delete_group($groupID)) + return FALSE; + } + else + //Make the user leave the group + if(!$this->deleteMembershipWithStatus( + $userID, $groupID, $this->getMembershipLevel($userID, $groupID))) + return FALSE; + + } + + //Success + return TRUE; + } + /** * Turn a database entry into a GroupInfo object *