Delete groups membership when deleting an account

This commit is contained in:
Pierre HUBERT 2019-04-26 23:49:23 +02:00
parent fe702519b1
commit 9dfc400fe2
2 changed files with 49 additions and 1 deletions

View File

@ -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

View File

@ -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
*