From b9c9a9d4c7cbffdc296ab42dad5ca603ffc21d09 Mon Sep 17 00:00:00 2001 From: asha15 <165079T@uom.lk> Date: Tue, 3 Jun 2025 14:36:31 +0530 Subject: [PATCH 1/2] Throw ForbiddenException --- .../org/wso2/charon3/core/extensions/RoleManager.java | 9 ++++++--- .../org/wso2/charon3/core/extensions/RoleV2Manager.java | 8 +++++--- .../org/wso2/charon3/core/extensions/UserManager.java | 6 ++++-- .../core/protocol/endpoints/GroupResourceManager.java | 8 +++++--- .../core/protocol/endpoints/RoleResourceManager.java | 8 ++++---- .../core/protocol/endpoints/RoleResourceV2Manager.java | 8 ++++---- .../protocol/endpoints/GroupResourceManagerTest.java | 7 +++++-- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleManager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleManager.java index 7e1409bac..60a3b6d59 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleManager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleManager.java @@ -48,7 +48,8 @@ public interface RoleManager { * @throws NotImplementedException NotImplementedException. * @throws BadRequestException BadRequestException. */ - Role createRole(Role role) throws CharonException, ConflictException, NotImplementedException, BadRequestException; + Role createRole(Role role) throws CharonException, ConflictException, NotImplementedException, + BadRequestException, ForbiddenException; /** * Get the role for the given ID. @@ -73,7 +74,8 @@ Role getRole(String id, Map requiredAttributes) * @throws NotImplementedException NotImplementedException. * @throws BadRequestException BadRequestException. */ - void deleteRole(String id) throws NotFoundException, CharonException, NotImplementedException, BadRequestException; + void deleteRole(String id) throws NotFoundException, CharonException, NotImplementedException, + BadRequestException, ForbiddenException; /** * List roles with Get. @@ -104,7 +106,8 @@ RolesGetResponse listRolesWithGET(Node node, Integer startIndex, Integer count, * @throws NotFoundException NotFoundException. */ Role updateRole(Role oldRole, Role newRole) - throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException; + throws NotImplementedException, BadRequestException, CharonException, ConflictException, + NotFoundException, ForbiddenException; /** * List roles with Post. diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java index 3d20fb274..803f80e2d 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java @@ -49,7 +49,7 @@ public interface RoleV2Manager { * @throws BadRequestException BadRequestException. */ RoleV2 createRole(RoleV2 role) - throws CharonException, ConflictException, NotImplementedException, BadRequestException; + throws CharonException, ConflictException, NotImplementedException, BadRequestException, ForbiddenException; /** * Get the role for the given ID. @@ -74,7 +74,8 @@ RoleV2 getRole(String id, Map requiredAttributes) * @throws NotImplementedException NotImplementedException. * @throws BadRequestException BadRequestException. */ - void deleteRole(String id) throws NotFoundException, CharonException, NotImplementedException, BadRequestException; + void deleteRole(String id) throws NotFoundException, CharonException, NotImplementedException, BadRequestException, + ForbiddenException; /** * List roles with Get. @@ -107,7 +108,8 @@ RolesV2GetResponse listRolesWithGET(Node node, Integer startIndex, Integer count * @throws NotFoundException NotFoundException. */ RoleV2 updateRole(RoleV2 oldRole, RoleV2 newRole) - throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException; + throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException + , ForbiddenException; /** * List roles with Post. diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/UserManager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/UserManager.java index 405c6a3c4..9fbb5f0bd 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/UserManager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/UserManager.java @@ -216,7 +216,8 @@ default void updateGroup(Group oldGroup, Group newGroup) */ default Group patchGroup(String groupId, String currentGroupName, Map> patchOperations, Map requiredAttributes) - throws NotImplementedException, BadRequestException, CharonException, NotFoundException { + throws NotImplementedException, BadRequestException, CharonException, + NotFoundException, ForbiddenException { throw new NotImplementedException(); } @@ -234,7 +235,8 @@ default Group patchGroup(String groupId, String currentGroupName, Map> patchOperations) - throws NotImplementedException, BadRequestException, CharonException, NotFoundException { + throws NotImplementedException, BadRequestException, CharonException, + NotFoundException, ForbiddenException { throw new NotImplementedException(); } diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManager.java index b07699aa9..7aeb5ba09 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManager.java @@ -30,6 +30,7 @@ import org.wso2.charon3.core.exceptions.BadRequestException; import org.wso2.charon3.core.exceptions.CharonException; import org.wso2.charon3.core.exceptions.ConflictException; +import org.wso2.charon3.core.exceptions.ForbiddenException; import org.wso2.charon3.core.exceptions.InternalErrorException; import org.wso2.charon3.core.exceptions.NotFoundException; import org.wso2.charon3.core.exceptions.NotImplementedException; @@ -747,7 +748,8 @@ public SCIMResponse updateWithPatchForAddRemoveOperations(String existingGroupId String error = "Updated group resource is null."; throw new CharonException(error); } - } catch (NotFoundException | BadRequestException | NotImplementedException | CharonException e) { + } catch (NotFoundException | BadRequestException | NotImplementedException | + CharonException | ForbiddenException e) { return AbstractResourceManager.encodeSCIMException(e); } catch (RuntimeException e) { CharonException e1 = new CharonException("Error in performing the patch operation on group resource.", e); @@ -785,7 +787,7 @@ private Map> buildPatchOperationsMap(List opList, UserManager userManager) throws BadRequestException, - NotImplementedException, NotFoundException, CharonException { + NotImplementedException, NotFoundException, CharonException, ForbiddenException { Map> patchOperations = buildPatchOperationsMap(opList); SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema(); @@ -1011,7 +1013,7 @@ public SCIMResponse updateWithPATCH(String existingGroupId, String patchRequest, httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON); return new SCIMResponse(ResponseCodeConstants.CODE_NO_CONTENT, null, httpHeaders); } catch (NotFoundException | BadRequestException | NotImplementedException | CharonException | - InternalErrorException e) { + InternalErrorException | ForbiddenException e) { return AbstractResourceManager.encodeSCIMException(e); } catch (RuntimeException e) { CharonException ex = new CharonException("Error in performing the patch operation on group resource.", e); diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceManager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceManager.java index 26d8c3500..e0445405b 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceManager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceManager.java @@ -135,7 +135,7 @@ public SCIMResponse createRole(String postRequest, RoleManager roleManager) { return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedRole, httpHeaders); } catch (InternalErrorException | BadRequestException | ConflictException | CharonException | NotFoundException - | NotImplementedException e) { + | NotImplementedException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -152,7 +152,7 @@ public SCIMResponse deleteRole(String id, RoleManager roleManager) { return new SCIMResponse(ResponseCodeConstants.CODE_NO_CONTENT, null, null); } catch (InternalErrorException | CharonException | NotFoundException | NotImplementedException - | BadRequestException e) { + | BadRequestException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -363,7 +363,7 @@ public SCIMResponse updateWithPUTRole(String id, String putRequest, RoleManager return getScimResponse(encoder, updatedRole); } catch (NotFoundException | BadRequestException | CharonException | ConflictException | InternalErrorException - | NotImplementedException e) { + | NotImplementedException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -397,7 +397,7 @@ public SCIMResponse updateWithPATCHRole(String id, String patchRequest, RoleMana return getScimResponse(encoder, updatedRole); } catch (NotFoundException | BadRequestException | NotImplementedException | CharonException | ConflictException - | InternalErrorException e) { + | InternalErrorException | ForbiddenException e) { return AbstractResourceManager.encodeSCIMException(e); } catch (RuntimeException e) { CharonException ex = new CharonException("Error in performing the patch operation on role resource.", e); diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceV2Manager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceV2Manager.java index 1813ac24a..d194bfa84 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceV2Manager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/protocol/endpoints/RoleResourceV2Manager.java @@ -135,7 +135,7 @@ public SCIMResponse createRole(String postRequest, RoleV2Manager roleManager) { return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedRole, httpHeaders); } catch (InternalErrorException | BadRequestException | ConflictException | CharonException | NotFoundException - | NotImplementedException e) { + | NotImplementedException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -151,7 +151,7 @@ public SCIMResponse deleteRole(String id, RoleV2Manager roleManager) { roleManager.deleteRole(id); return new SCIMResponse(ResponseCodeConstants.CODE_NO_CONTENT, null, null); } catch (InternalErrorException | CharonException | NotFoundException | NotImplementedException - | BadRequestException e) { + | BadRequestException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -287,7 +287,7 @@ public SCIMResponse updateWithPUTRole(String id, String putRequest, RoleV2Manage updatedRole = roleManager.updateRole(oldRole, newRole); return getScimResponse(encoder, updatedRole); } catch (NotFoundException | BadRequestException | CharonException | ConflictException | InternalErrorException - | NotImplementedException e) { + | NotImplementedException | ForbiddenException e) { return encodeSCIMException(e); } } @@ -320,7 +320,7 @@ public SCIMResponse updateWithPATCHRole(String id, String patchRequest, RoleV2Ma RoleV2 updatedRole = roleManager.updateRole(originalRole, patchedRole); return getScimResponse(encoder, updatedRole); } catch (NotFoundException | BadRequestException | NotImplementedException | CharonException | ConflictException - | InternalErrorException e) { + | InternalErrorException | ForbiddenException e) { return AbstractResourceManager.encodeSCIMException(e); } catch (RuntimeException e) { CharonException ex = new CharonException("Error in performing the patch operation on role resource.", e); diff --git a/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManagerTest.java b/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManagerTest.java index 6b9a15577..77bc16f34 100644 --- a/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManagerTest.java +++ b/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/GroupResourceManagerTest.java @@ -32,6 +32,7 @@ import org.wso2.charon3.core.exceptions.BadRequestException; import org.wso2.charon3.core.exceptions.CharonException; import org.wso2.charon3.core.exceptions.ConflictException; +import org.wso2.charon3.core.exceptions.ForbiddenException; import org.wso2.charon3.core.exceptions.InternalErrorException; import org.wso2.charon3.core.exceptions.NotFoundException; import org.wso2.charon3.core.exceptions.NotImplementedException; @@ -786,7 +787,8 @@ public Object[][] dataToUpdateWithPATCH() throws BadRequestException, CharonExce @Test(dataProvider = "dataForUpdateWithPATCH") public void testUpdateWithPATCH(String existingId, String patchRequest, String attributes, String excludeAttributes, Object scimNewGroupObject, Object scimOldGroupObject) - throws BadRequestException, CharonException, NotImplementedException, NotFoundException { + throws BadRequestException, CharonException, NotImplementedException, + NotFoundException, ForbiddenException { Group groupNew = (Group) scimNewGroupObject; Group groupOld = (Group) scimOldGroupObject; @@ -817,7 +819,8 @@ public Object[][] dataToUpdateWithPATCHOverride() @Test(dataProvider = "dataForUpdateWithPATCHOverride") public void testUpdateWithPATCHOverride(String existingId, String patchRequest, Object scimNewGroupObject, Object scimOldGroupObject) - throws BadRequestException, CharonException, NotImplementedException, NotFoundException { + throws BadRequestException, CharonException, NotImplementedException, + NotFoundException, ForbiddenException { Group groupNew = (Group) scimNewGroupObject; Group groupOld = (Group) scimOldGroupObject; From 67e404e5a93ec1550b8528fa871508fe34be3500 Mon Sep 17 00:00:00 2001 From: asha15 <165079T@uom.lk> Date: Thu, 5 Jun 2025 22:04:18 +0530 Subject: [PATCH 2/2] Refactor the code --- .../java/org/wso2/charon3/core/extensions/RoleV2Manager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java index 803f80e2d..3811d7cd5 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/extensions/RoleV2Manager.java @@ -108,8 +108,8 @@ RolesV2GetResponse listRolesWithGET(Node node, Integer startIndex, Integer count * @throws NotFoundException NotFoundException. */ RoleV2 updateRole(RoleV2 oldRole, RoleV2 newRole) - throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException - , ForbiddenException; + throws NotImplementedException, BadRequestException, CharonException, ConflictException, + NotFoundException, ForbiddenException; /** * List roles with Post.