2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization.storage;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
30 import org.collectionspace.services.authorization.PermissionRole;
31 import org.collectionspace.services.authorization.PermissionRoleSubResource;
32 import org.collectionspace.services.authorization.PermissionValue;
33 import org.collectionspace.services.authorization.Role;
34 import org.collectionspace.services.authorization.RoleValue;
35 import org.collectionspace.services.authorization.RolesList;
36 import org.collectionspace.services.authorization.SubjectType;
38 import org.collectionspace.services.client.PermissionRoleFactory;
39 import org.collectionspace.services.client.RoleClient;
40 import org.collectionspace.services.client.RoleFactory;
41 import org.collectionspace.services.common.api.Tools;
42 import org.collectionspace.services.common.document.BadRequestException;
43 import org.collectionspace.services.common.document.DocumentFilter;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.document.JaxbUtils;
46 import org.collectionspace.services.common.security.SecurityUtils;
47 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * Document handler for Role
56 public class RoleDocumentHandler
57 extends JpaDocumentHandler<Role, RolesList, Role, List> {
58 private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
60 private RolesList rolesList;
63 public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
64 String id = UUID.randomUUID().toString();
65 Role role = wrapDoc.getWrappedObject();
67 // Synthesize the display name if it was not passed in.
68 String displayName = role.getDisplayName();
69 boolean displayNameEmpty = true;
70 if (displayName != null) {
71 displayNameEmpty = displayName.trim().isEmpty();
73 if (displayNameEmpty == true) {
74 role.setDisplayName(role.getRoleName());
78 role.setRoleName(RoleClient.getBackendRoleName(role.getRoleName(), role.getTenantId()));
80 // We do not allow creation of locked roles through the services.
81 role.setMetadataProtection(null);
82 role.setPermsProtection(null);
86 public void completeCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
87 Role role = wrapDoc.getWrappedObject();
88 List<PermissionValue> permValueList = role.getPermission();
89 if (permValueList != null && permValueList.size() > 0) {
90 // create and persist a permrole instance
91 // The caller of this method needs to ensure a valid and active EM (EntityManager) instance is in the Service context
92 RoleValue roleValue = RoleFactory.createRoleValueInstance(role);
93 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(SubjectType.PERMISSION, roleValue,
94 permValueList, true, true);
95 PermissionRoleSubResource subResource =
96 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
97 String permrolecsid = subResource.createPermissionRole(permRole, SubjectType.PERMISSION);
102 public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
103 Role roleFound = wrapDoc.getWrappedObject();
104 Role roleReceived = getCommonPart();
105 // If marked as metadata immutable, do not do update
106 if (!RoleClient.IMMUTABLE.equals(roleFound.getMetadataProtection())) {
108 .setRoleName(RoleClient.getBackendRoleName(roleReceived.getRoleName(), roleFound.getTenantId()));
109 merge(roleReceived, roleFound);
112 // Update perms is supplied.
114 List<PermissionValue> permValueList = roleReceived.getPermission();
115 if (permValueList != null) {
116 PermissionRoleSubResource subResource =
117 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
119 // First, delete the existing permroles
121 subResource.deletePermissionRole(roleFound.getCsid(), SubjectType.PERMISSION);
123 // Next, create the new permroles
125 RoleValue roleValue = RoleFactory.createRoleValueInstance(roleFound);
126 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(SubjectType.PERMISSION, roleValue,
127 permValueList, true, true);
128 subResource.createPermissionRole(permRole, SubjectType.PERMISSION);
130 // Finally, set the updated perm list in the result
132 PermissionRole newPermRole = subResource.getPermissionRole(roleFound.getCsid(), SubjectType.PERMISSION);
133 roleFound.setPermission(newPermRole.getPermission());
138 * Merge fields manually from 'from' to the 'to' role
139 * -this method is created due to inefficiency of JPA EM merge
142 * @return merged role
144 private Role merge(Role from, Role to) throws Exception {
145 // A role's name cannot be changed
146 if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
147 String msg = "Role name cannot be changed " + to.getRoleName();
149 throw new BadRequestException(msg);
152 if (from.getDisplayName() != null && !from.getDisplayName().trim().isEmpty() ) {
153 to.setDisplayName(from.getDisplayName());
155 if (from.getRoleGroup() != null && !from.getRoleGroup().trim().isEmpty()) {
156 to.setRoleGroup(from.getRoleGroup());
158 if (from.getDescription() != null && !from.getDescription().trim().isEmpty()) {
159 to.setDescription(from.getDescription());
162 if (logger.isDebugEnabled()) {
163 org.collectionspace.services.authorization.ObjectFactory objectFactory =
164 new org.collectionspace.services.authorization.ObjectFactory();
165 logger.debug("Merged role on update=" + JaxbUtils.toString(objectFactory.createRole(to), Role.class));
172 public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
173 Role upAcc = wrapDoc.getWrappedObject();
174 getServiceContext().setOutput(upAcc);
179 public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
180 setCommonPart(extractCommonPart(wrapDoc));
181 sanitize(getCommonPart());
182 getServiceContext().setOutput(role);
186 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
187 RolesList rolesList = extractCommonPartList(wrapDoc);
188 setCommonPartList(rolesList);
189 getServiceContext().setOutput(getCommonPartList());
193 public Role extractCommonPart(
194 DocumentWrapper<Role> wrapDoc)
196 Role role = wrapDoc.getWrappedObject();
198 String includePermsQueryParamValue = (String) getServiceContext().getQueryParams().getFirst(RoleClient.INCLUDE_PERMS_QP);
199 boolean includePerms = Tools.isTrue(includePermsQueryParamValue);
201 PermissionRoleSubResource permRoleResource =
202 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
203 PermissionRole permRole = permRoleResource.getPermissionRole(role.getCsid(), SubjectType.PERMISSION);
204 role.setPermission(permRole.getPermission());
211 public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
213 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
217 public RolesList extractCommonPartList(
218 DocumentWrapper<List> wrapDoc)
221 RolesList rolesList = new RolesList();
222 List<Role> list = new ArrayList<Role>();
223 rolesList.setRole(list);
224 for (Object obj : wrapDoc.getWrappedObject()) {
225 Role role = (Role) obj;
233 public Role getCommonPart() {
238 public void setCommonPart(Role role) {
243 public RolesList getCommonPartList() {
248 public void setCommonPartList(RolesList rolesList) {
249 this.rolesList = rolesList;
253 public String getQProperty(
259 public DocumentFilter createDocumentFilter() {
260 DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
265 * sanitize removes data not needed to be sent to the consumer
268 private void sanitize(Role role) {
269 if (!SecurityUtils.isCSpaceAdmin()) {
270 // role.setTenantId(null); // REM - There's no reason for hiding the tenant ID is there?
274 private void setTenant(Role role) {
275 //set tenant only if not available from input
276 if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
277 role.setTenantId(getServiceContext().getTenantId());