From 6a7e8ccc37a05a2745ff85d785e6ceb9a180c71b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 18 Dec 2020 16:10:33 +0100 Subject: [PATCH] Now with duplication avoidance ref #6 --- src/controllers/PermissionController.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 5304fee..3763145 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -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());