Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Role V2 definitions #392

Merged
merged 11 commits into from
Oct 21, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.charon3.core.extensions;

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.NotFoundException;
import org.wso2.charon3.core.exceptions.NotImplementedException;
import org.wso2.charon3.core.objects.RoleV2;
import org.wso2.charon3.core.objects.plainobjects.RolesV2GetResponse;
import org.wso2.charon3.core.utils.codeutils.Node;
import org.wso2.charon3.core.utils.codeutils.PatchOperation;
import org.wso2.charon3.core.utils.codeutils.SearchRequest;

import java.util.List;
import java.util.Map;

/**
* OSGi service interface which uses to manage roleV2.
*/
public interface RoleV2Manager {

/**
* Create a role.
*
* @param role Role Object.
* @return Role.
* @throws CharonException CharonException.
* @throws ConflictException ConflictException.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
*/
RoleV2 createRole(RoleV2 role)
throws CharonException, ConflictException, NotImplementedException, BadRequestException;

/**
* Get the role for the given ID.
*
* @param id Role ID.
* @param requiredAttributes Required Attributes.
* @return Role.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
* @throws CharonException CharonException.
* @throws NotFoundException NotFoundException.
*/
RoleV2 getRole(String id, Map<String, Boolean> requiredAttributes)
throws NotImplementedException, BadRequestException, CharonException, NotFoundException;

/**
* Delete the given role.
*
* @param id Role ID.
* @throws NotFoundException NotFoundException.
* @throws CharonException CharonException.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
*/
void deleteRole(String id) throws NotFoundException, CharonException, NotImplementedException, BadRequestException;

/**
* List roles with Get.
*
* @param node Node
* @param startIndex Start Index
* @param count Count
* @param sortBy Sort by
* @param sortOrder Sort order
* @return List of roles.
* @throws CharonException CharonException.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
*/
RolesV2GetResponse listRolesWithGET(Node node, Integer startIndex, Integer count, String sortBy, String sortOrder)
throws CharonException, NotImplementedException, BadRequestException;

/**
* Update the role.
*
* @param oldRole Old role.
* @param newRole new role.
* @return Updated role.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
* @throws CharonException CharonException.
* @throws ConflictException ConflictException.
* @throws NotFoundException NotFoundException.
*/
RoleV2 updateRole(RoleV2 oldRole, RoleV2 newRole)
throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException;

/**
* List roles with Post.
*
* @param searchRequest Search request.
* @return List of roles.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
* @throws CharonException CharonException.
*/
RolesV2GetResponse listRolesWithPost(SearchRequest searchRequest)
throws NotImplementedException, BadRequestException, CharonException;

/**
* Updates the role via PATCH.
*
* @param roleId ID of the role.
* @param patchOperations A map of patch operations.
* @return Updated group.
* @throws CharonException Charon exception.
* @throws BadRequestException Bad request exception.
* @throws NotFoundException Not found exception.
* @throws NotImplementedException Functionality no implemented exception.
*/
RoleV2 patchRole(String roleId, Map<String, List<PatchOperation>> patchOperations)
throws NotImplementedException, BadRequestException, CharonException, ConflictException, NotFoundException,
ForbiddenException;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2016-2023, WSO2 LLC. (http://www.wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.charon3.core.objects;

Expand All @@ -23,6 +25,7 @@
import org.wso2.charon3.core.attributes.SimpleAttribute;
import org.wso2.charon3.core.exceptions.BadRequestException;
import org.wso2.charon3.core.exceptions.CharonException;
import org.wso2.charon3.core.schema.SCIMAttributeSchema;
import org.wso2.charon3.core.schema.SCIMConstants;
import org.wso2.charon3.core.schema.SCIMResourceSchemaManager;
import org.wso2.charon3.core.schema.SCIMResourceTypeSchema;
Expand All @@ -45,6 +48,7 @@ public class Group extends AbstractSCIMObject {
private static final long serialVersionUID = 6106269076155338045L;
/**
* get the display name of the group
*
* @return
* @throws CharonException
*/
Expand All @@ -59,6 +63,7 @@ public String getDisplayName() {

/**
* set the display name of the group
*
* @param displayName
* @throws CharonException
* @throws BadRequestException
Expand All @@ -85,6 +90,7 @@ public void replaceDisplayName(String displayname) {

/**
* get the members of the group
*
* @return
*/
public List<Object> getMembers() {
Expand All @@ -108,6 +114,7 @@ public List<Object> getMembers() {

/**
* get the members of the group with their display names
*
* @return
*/
public List<String> getMembersWithDisplayName() {
Expand Down Expand Up @@ -169,6 +176,7 @@ public void setMember(String value, String display, String ref, String type)

/**
* set member to the group
*
* @param userId
* @param userName
* @return
Expand Down Expand Up @@ -411,4 +419,89 @@ private void setRole(ComplexAttribute groupPropertiesAttribute) throws CharonExc
}
}

/**
* Set the assigned V2 roles of the group.
*
* @param role RoleV2 object.
* @throws CharonException CharonException.
* @throws BadRequestException BadRequestException.
*/
public void setRoleV2(RoleV2 role) throws CharonException, BadRequestException {

SimpleAttribute valueSimpleAttribute = null;
String reference = role.getLocation();
String value = role.getId();
String display = role.getDisplayName();
String audienceValue = role.getAudienceValue();
String audienceDisplay = role.getAudienceDisplayName();
String audienceType = role.getAudienceType();
ComplexAttribute complexAttribute = new ComplexAttribute();

if (StringUtils.isNotBlank(value)) {
valueSimpleAttribute = getSimpleAttribute(SCIMConstants.CommonSchemaConstants.VALUE, value,
SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.ROLES_VALUE);
complexAttribute.setSubAttribute(valueSimpleAttribute);
}

if (StringUtils.isNotBlank(reference)) {
complexAttribute.setSubAttribute(getSimpleAttribute(SCIMConstants.CommonSchemaConstants.REF, reference,
SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.ROLES_REF));
}

if (StringUtils.isNotBlank(display)) {
complexAttribute.setSubAttribute(getSimpleAttribute(SCIMConstants.CommonSchemaConstants.DISPLAY, display,
SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.ROLES_DISPLAY));
}

if (StringUtils.isNotBlank(audienceValue)) {
complexAttribute.setSubAttribute(getSimpleAttribute(SCIMConstants.CommonSchemaConstants.AUDIENCE_VALUE,
audienceValue, SCIMSchemaDefinitions.SCIMUserSchemaDefinition.ROLES_AUDIENCE_VALUE));
}

if (StringUtils.isNotBlank(audienceDisplay)) {
complexAttribute.setSubAttribute(getSimpleAttribute(SCIMConstants.CommonSchemaConstants.AUDIENCE_DISPLAY,
audienceDisplay, SCIMSchemaDefinitions.SCIMUserSchemaDefinition.ROLES_AUDIENCE_DISPLAY));
}

if (StringUtils.isNotBlank(audienceType)) {
complexAttribute.setSubAttribute(getSimpleAttribute(SCIMConstants.CommonSchemaConstants.AUDIENCE_TYPE,
audienceType, SCIMSchemaDefinitions.SCIMUserSchemaDefinition.ROLES_AUDIENCE_TYPE));
}

if (!complexAttribute.getSubAttributesList().isEmpty()) {
Object typeVal = SCIMConstants.DEFAULT;
Object valueVal = SCIMConstants.DEFAULT;
if (valueSimpleAttribute != null && valueSimpleAttribute.getValue() != null) {
valueVal = valueSimpleAttribute.getValue();
}
String complexAttributeName = SCIMConstants.GroupSchemaConstants.ROLES + "_" + valueVal + "_" + typeVal;
complexAttribute.setName(complexAttributeName);
DefaultAttributeFactory
.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.ROLES_SCHEMA, complexAttribute);
setRoleV2(complexAttribute);
}
}

private SimpleAttribute getSimpleAttribute(String attributeName, String attributeValue,
SCIMAttributeSchema attributeSchema)
throws CharonException, BadRequestException {

return (SimpleAttribute) DefaultAttributeFactory.createAttribute(attributeSchema,
new SimpleAttribute(attributeName, attributeValue));
}

private void setRoleV2(ComplexAttribute rolePropertiesAttribute) throws CharonException, BadRequestException {

MultiValuedAttribute rolesAttribute;
if (this.attributeList.containsKey(SCIMConstants.GroupSchemaConstants.ROLES)) {
rolesAttribute = (MultiValuedAttribute) this.attributeList.get(SCIMConstants.GroupSchemaConstants.ROLES);
rolesAttribute.setAttributeValue(rolePropertiesAttribute);
} else {
rolesAttribute = new MultiValuedAttribute(SCIMConstants.GroupSchemaConstants.ROLES);
rolesAttribute.setAttributeValue(rolePropertiesAttribute);
rolesAttribute = (MultiValuedAttribute) DefaultAttributeFactory
.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.ROLES_SCHEMA, rolesAttribute);
this.attributeList.put(SCIMConstants.GroupSchemaConstants.ROLES, rolesAttribute);
}
}
}
Loading
Loading