1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-27 05:58:52 +00:00

Can delete all user groups

This commit is contained in:
Pierre HUBERT 2020-03-26 17:08:56 +01:00
parent 9cc205f59a
commit c222c8b054
4 changed files with 47 additions and 2 deletions

View File

@ -298,8 +298,9 @@ export class AccountController {
public static async DeleteAccount(h: RequestHandler) {
await h.needUserPostPassword("password");
// TODO : implement
h.error(500, "Method not implemented yet.");
await AccountHelper.Delete(h.getUserId());
h.error(500, "Method not completely implemented yet.");
}
/**

View File

@ -414,6 +414,7 @@ export class GroupsController {
h.error(404, "Membership not found!");
// If the user is an admin, he must not be the last admin of the group
// TODO : use new method
if(userID == h.getUserId() && currUserMembership.level == GroupMembershipLevels.ADMINISTRATOR
&& await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1)
h.error(401, "You are the last administrator of this group!");
@ -533,6 +534,7 @@ export class GroupsController {
const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS);
const level = await GroupsHelper.GetMembershipLevel(groupID, h.getUserId());
// TODO : use new method
if(level == GroupMembershipLevels.ADMINISTRATOR
&& await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1)
h.error(401, "You are the last administrator of the group!");

View File

@ -434,4 +434,18 @@ export class AccountHelper {
return data;
}
/**
* Remove completely a user account
*
* @param userID Target user ID
*/
public static async Delete(userID: number) {
// Delete all groups memberships
await GroupsHelper.DeleteAllUsersGroups(userID);
// TODO : continue work
}
}

View File

@ -392,6 +392,17 @@ export class GroupsHelper {
}
/**
* Check out whether a user is the last admin of a user or not
*
* @param groupID Target group ID
* @param userID Target user ID
*/
private static async IsLastAdmin(groupID: number, userID: number) : Promise<boolean> {
return await this.GetMembershipLevel(groupID, userID) == GroupMembershipLevels.ADMINISTRATOR
&& await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1;
}
/**
* Get the current access of a user to a group
*
@ -617,6 +628,23 @@ export class GroupsHelper {
});
}
/**
* Delete all the groups the user belongs to
*
* @param userID Target user ID
*/
public static async DeleteAllUsersGroups(userID: number) {
// Process each group
for(const groupID of await this.GetListUser(userID)) {
if(await this.IsLastAdmin(groupID, userID))
await this.Delete(groupID);
else
await this.DeleteMember(groupID, userID);
}
}
/**
* Turn a database row into a {GroupInfo} object
*