mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-21 17:26:29 -05:00
refactor: make permission service less complex
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
0f8effd318
commit
8fc59aad82
1 changed files with 34 additions and 14 deletions
|
@ -236,14 +236,12 @@ export class PermissionsService {
|
|||
canEdit: boolean,
|
||||
): Promise<Note> {
|
||||
const permissions = await note.userPermissions;
|
||||
let permissionIndex = 0;
|
||||
const permission = permissions.find(async (value, index) => {
|
||||
permissionIndex = index;
|
||||
return (await value.user).id == permissionUser.id;
|
||||
});
|
||||
if (permission != undefined) {
|
||||
const permission = await this.findPermissionForUser(
|
||||
permissions,
|
||||
permissionUser,
|
||||
);
|
||||
if (permission !== undefined) {
|
||||
permission.canEdit = canEdit;
|
||||
permissions[permissionIndex] = permission;
|
||||
} else {
|
||||
const noteUserPermission = NoteUserPermission.create(
|
||||
permissionUser,
|
||||
|
@ -256,6 +254,18 @@ export class PermissionsService {
|
|||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
private async findPermissionForUser(
|
||||
permissions: NoteUserPermission[],
|
||||
user: User,
|
||||
): Promise<NoteUserPermission | undefined> {
|
||||
for (const permission of permissions) {
|
||||
if ((await permission.user).id == user.id) {
|
||||
return permission;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* Remove permission for a specific user on a note.
|
||||
|
@ -294,14 +304,12 @@ export class PermissionsService {
|
|||
'setGroupPermission',
|
||||
);
|
||||
const permissions = await note.groupPermissions;
|
||||
let permissionIndex = 0;
|
||||
const permission = permissions.find(async (value, index) => {
|
||||
permissionIndex = index;
|
||||
return (await value.group).id == permissionGroup.id;
|
||||
});
|
||||
if (permission != undefined) {
|
||||
const permission = await this.findPermissionForGroup(
|
||||
permissions,
|
||||
permissionGroup,
|
||||
);
|
||||
if (permission !== undefined) {
|
||||
permission.canEdit = canEdit;
|
||||
permissions[permissionIndex] = permission;
|
||||
} else {
|
||||
this.logger.debug(
|
||||
`Permission does not exist yet, creating new one.`,
|
||||
|
@ -318,6 +326,18 @@ export class PermissionsService {
|
|||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
private async findPermissionForGroup(
|
||||
permissions: NoteGroupPermission[],
|
||||
group: Group,
|
||||
): Promise<NoteGroupPermission | undefined> {
|
||||
for (const permission of permissions) {
|
||||
if ((await permission.group).id == group.id) {
|
||||
return permission;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* Remove permission for a specific group on a note.
|
||||
|
|
Loading…
Reference in a new issue