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;
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())) {
107 roleReceived.setRoleName(RoleClient.getBackendRoleName(roleReceived.getRoleName(),
108 roleFound.getTenantId()));
109 merge(roleReceived, roleFound);
114 * Merge fields manually from 'from' to the 'to' role
115 * -this method is created due to inefficiency of JPA EM merge
118 * @return merged role
120 private Role merge(Role from, Role to) throws Exception {
121 // A role's name cannot be changed
122 if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
123 String msg = "Role name cannot be changed " + to.getRoleName();
125 throw new BadRequestException(msg);
128 if (from.getDisplayName() != null && !from.getDisplayName().trim().isEmpty() ) {
129 to.setDisplayName(from.getDisplayName());
131 if (from.getRoleGroup() != null && !from.getRoleGroup().trim().isEmpty()) {
132 to.setRoleGroup(from.getRoleGroup());
134 if (from.getDescription() != null && !from.getDescription().trim().isEmpty()) {
135 to.setDescription(from.getDescription());
138 if (logger.isDebugEnabled()) {
139 org.collectionspace.services.authorization.ObjectFactory objectFactory =
140 new org.collectionspace.services.authorization.ObjectFactory();
141 logger.debug("Merged role on update=" + JaxbUtils.toString(objectFactory.createRole(to), Role.class));
148 public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
149 Role upAcc = wrapDoc.getWrappedObject();
150 getServiceContext().setOutput(upAcc);
155 public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
156 setCommonPart(extractCommonPart(wrapDoc));
157 sanitize(getCommonPart());
158 getServiceContext().setOutput(role);
162 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
163 RolesList rolesList = extractCommonPartList(wrapDoc);
164 setCommonPartList(rolesList);
165 getServiceContext().setOutput(getCommonPartList());
169 public Role extractCommonPart(
170 DocumentWrapper<Role> wrapDoc)
172 return wrapDoc.getWrappedObject();
176 public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
178 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
182 public RolesList extractCommonPartList(
183 DocumentWrapper<List> wrapDoc)
186 RolesList rolesList = new RolesList();
187 List<Role> list = new ArrayList<Role>();
188 rolesList.setRole(list);
189 for (Object obj : wrapDoc.getWrappedObject()) {
190 Role role = (Role) obj;
198 public Role getCommonPart() {
203 public void setCommonPart(Role role) {
208 public RolesList getCommonPartList() {
213 public void setCommonPartList(RolesList rolesList) {
214 this.rolesList = rolesList;
218 public String getQProperty(
224 public DocumentFilter createDocumentFilter() {
225 DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
230 * sanitize removes data not needed to be sent to the consumer
233 private void sanitize(Role role) {
234 if (!SecurityUtils.isCSpaceAdmin()) {
235 role.setTenantId(null); // REM - See no reason for hiding the tenant ID?
239 private void setTenant(Role role) {
240 //set tenant only if not available from input
241 if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
242 role.setTenantId(getServiceContext().getTenantId());