|
22 | 22 |
|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Set;
|
| 25 | +import java.util.stream.Collectors; |
25 | 26 |
|
26 | 27 | /**
|
27 | 28 | * Service implementation for updating roles.
|
@@ -62,19 +63,78 @@ public void update(final String id,
|
62 | 63 | .filter(roleFromDatabase -> identity.getInstitutionId().equals(roleFromDatabase.getInstitution().getId()))
|
63 | 64 | .orElseThrow(() -> new AysRoleNotExistByIdException(id));
|
64 | 65 |
|
65 |
| - if (!role.getName().equals(updateRequest.getName())) { |
| 66 | + final boolean isRoleNameChanged = !role.getName().equals(updateRequest.getName()); |
| 67 | + if (isRoleNameChanged) { |
66 | 68 | this.checkExistingRoleNameByWithoutId(id, updateRequest.getName());
|
67 | 69 | }
|
68 | 70 |
|
69 |
| - final List<AysPermission> permissions = this.checkExistingPermissionsAndGet(updateRequest.getPermissionIds()); |
70 |
| - |
71 |
| - role.setName(updateRequest.getName()); |
72 |
| - role.setPermissions(permissions); |
73 |
| - role.setUpdatedUser(identity.getUserId()); |
| 71 | + final Set<String> existingPermissionIds = role.getPermissions().stream() |
| 72 | + .map(AysPermission::getId) |
| 73 | + .collect(Collectors.toSet()); |
| 74 | + final boolean permissionsChanged = !existingPermissionIds.equals(updateRequest.getPermissionIds()); |
| 75 | + if (permissionsChanged) { |
| 76 | + this.validatePermissions(updateRequest.getPermissionIds()); |
| 77 | + } |
74 | 78 |
|
| 79 | + role.update( |
| 80 | + updateRequest.getName(), |
| 81 | + updateRequest.getPermissionIds(), |
| 82 | + identity.getUserId() |
| 83 | + ); |
75 | 84 | roleSavePort.save(role);
|
76 | 85 | }
|
77 | 86 |
|
| 87 | + /** |
| 88 | + * Checks the existence of another role with the same name, excluding the current role ID. |
| 89 | + * |
| 90 | + * @param id The ID of the role being updated. |
| 91 | + * @param name The name to check for uniqueness. |
| 92 | + * @throws AysRoleAlreadyExistsByNameException if a role with the same name already exists, excluding the current role ID. |
| 93 | + */ |
| 94 | + private void checkExistingRoleNameByWithoutId(final String id, final String name) { |
| 95 | + roleReadPort.findByName(name) |
| 96 | + .filter(role -> !id.equals(role.getId())) |
| 97 | + .ifPresent(role -> { |
| 98 | + throw new AysRoleAlreadyExistsByNameException(name); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Validates the specified permission IDs and checks user authorization. |
| 104 | + * <p> |
| 105 | + * This method verifies if all permission IDs exist in the system and checks if the user |
| 106 | + * has appropriate access level for super permissions. Only super admin users can assign |
| 107 | + * super permissions. |
| 108 | + * </p> |
| 109 | + * |
| 110 | + * @param permissionIds the set of permission IDs to validate |
| 111 | + * @throws AysPermissionNotExistException if any of the specified permissions do not exist |
| 112 | + * @throws AysUserNotSuperAdminException if a non-super admin user attempts to assign super permissions |
| 113 | + */ |
| 114 | + private void validatePermissions(final Set<String> permissionIds) { |
| 115 | + |
| 116 | + final List<AysPermission> permissions = permissionReadPort.findAllByIds(permissionIds); |
| 117 | + |
| 118 | + if (permissions.size() != permissionIds.size()) { |
| 119 | + |
| 120 | + final List<String> notExistsPermissionIds = permissionIds.stream() |
| 121 | + .filter(permissionId -> permissions.stream() |
| 122 | + .noneMatch(permissionEntity -> permissionEntity.getId().equals(permissionId))) |
| 123 | + .toList(); |
| 124 | + |
| 125 | + throw new AysPermissionNotExistException(notExistsPermissionIds); |
| 126 | + } |
| 127 | + |
| 128 | + if (identity.isSuperAdmin()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + boolean haveSuperPermissions = permissions.stream().anyMatch(AysPermission::isSuper); |
| 133 | + if (haveSuperPermissions) { |
| 134 | + throw new AysUserNotSuperAdminException(identity.getUserId()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
78 | 138 |
|
79 | 139 | /**
|
80 | 140 | * Activates an existing role.
|
@@ -163,54 +223,4 @@ public void delete(final String id) {
|
163 | 223 | roleSavePort.save(role);
|
164 | 224 | }
|
165 | 225 |
|
166 |
| - |
167 |
| - /** |
168 |
| - * Checks the existence of another role with the same name, excluding the current role ID. |
169 |
| - * |
170 |
| - * @param id The ID of the role being updated. |
171 |
| - * @param name The name to check for uniqueness. |
172 |
| - * @throws AysRoleAlreadyExistsByNameException if a role with the same name already exists, excluding the current role ID. |
173 |
| - */ |
174 |
| - private void checkExistingRoleNameByWithoutId(final String id, final String name) { |
175 |
| - roleReadPort.findByName(name) |
176 |
| - .filter(role -> !id.equals(role.getId())) |
177 |
| - .ifPresent(role -> { |
178 |
| - throw new AysRoleAlreadyExistsByNameException(name); |
179 |
| - }); |
180 |
| - } |
181 |
| - |
182 |
| - /** |
183 |
| - * Checks the existence of permissions based on the provided permission IDs. |
184 |
| - * Verifies if all permission IDs exist and validates super admin restrictions. |
185 |
| - * |
186 |
| - * @param permissionIds the set of permission IDs to check |
187 |
| - * @return the list of permissions corresponding to the provided IDs |
188 |
| - * @throws AysPermissionNotExistException if any of the permission IDs do not exist |
189 |
| - * @throws AysUserNotSuperAdminException if the current user is not authorized to assign super permissions |
190 |
| - */ |
191 |
| - private List<AysPermission> checkExistingPermissionsAndGet(final Set<String> permissionIds) { |
192 |
| - final List<AysPermission> permissions = permissionReadPort.findAllByIds(permissionIds); |
193 |
| - |
194 |
| - if (permissions.size() != permissionIds.size()) { |
195 |
| - |
196 |
| - final List<String> notExistsPermissionIds = permissionIds.stream() |
197 |
| - .filter(permissionId -> permissions.stream() |
198 |
| - .noneMatch(permissionEntity -> permissionEntity.getId().equals(permissionId))) |
199 |
| - .toList(); |
200 |
| - |
201 |
| - throw new AysPermissionNotExistException(notExistsPermissionIds); |
202 |
| - } |
203 |
| - |
204 |
| - if (identity.isSuperAdmin()) { |
205 |
| - return permissions; |
206 |
| - } |
207 |
| - |
208 |
| - boolean haveSuperPermissions = permissions.stream().anyMatch(AysPermission::isSuper); |
209 |
| - if (haveSuperPermissions) { |
210 |
| - throw new AysUserNotSuperAdminException(identity.getUserId()); |
211 |
| - } |
212 |
| - |
213 |
| - return permissions; |
214 |
| - } |
215 |
| - |
216 | 226 | }
|
0 commit comments