Now with duplication avoidance

ref #6
This commit is contained in:
Nicolai Ort 2020-12-18 16:10:33 +01:00
parent 882065470a
commit 6a7e8ccc37
1 changed files with 7 additions and 0 deletions

View File

@ -59,6 +59,8 @@ export class PermissionController {
} catch (error) {
throw error;
}
let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: permission.principal }, { relations: ['principal'] });
if (existingPermission) { return new ResponsePermission(existingPermission); }
permission = await this.permissionRepository.save(permission);
permission = await this.permissionRepository.findOne(permission, { relations: ['principal'] });
@ -84,6 +86,11 @@ export class PermissionController {
if (oldPermission.id != permission.id) {
throw new PermissionIdsNotMatchingError();
}
let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: permission.principal }, { relations: ['principal'] });
if (existingPermission) {
await this.remove(permission.id, true);
return new ResponsePermission(existingPermission);
}
await this.permissionRepository.update(oldPermission, await permission.toPermission());