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.AccountRole;
31 import org.collectionspace.services.authorization.AccountRoleRel;
32 import org.collectionspace.services.authorization.ActionType;
33 import org.collectionspace.services.authorization.AuthZ;
34 import org.collectionspace.services.authorization.CSpaceAction;
35 import org.collectionspace.services.authorization.EffectType;
36 import org.collectionspace.services.authorization.Permission;
37 import org.collectionspace.services.authorization.PermissionAction;
38 import org.collectionspace.services.authorization.PermissionActionUtil;
39 import org.collectionspace.services.authorization.PermissionsList;
40 import org.collectionspace.services.authorization.PermissionsRolesList;
41 import org.collectionspace.services.authorization.URIResourceImpl;
43 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
44 import org.collectionspace.services.common.document.BadRequestException;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentWrapper;
47 import org.collectionspace.services.common.document.JaxbUtils;
48 import org.collectionspace.services.common.security.SecurityUtils;
49 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * Document handler for Permission
57 public class PermissionDocumentHandler
58 extends JpaDocumentHandler<Permission, PermissionsList, Permission, List> {
60 private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
61 private Permission permission;
62 private PermissionsList permissionsList;
64 public CSpaceAction getAction(ActionType action) {
65 if (ActionType.CREATE.name().equals(action.name())) {
66 return CSpaceAction.CREATE;
67 } else if (ActionType.READ.equals(action)) {
68 return CSpaceAction.READ;
69 } else if (ActionType.UPDATE.equals(action)) {
70 return CSpaceAction.UPDATE;
71 } else if (ActionType.DELETE.equals(action)) {
72 return CSpaceAction.DELETE;
73 } else if (ActionType.SEARCH.equals(action)) {
74 return CSpaceAction.SEARCH;
75 } else if (ActionType.ADMIN.equals(action)) {
76 return CSpaceAction.ADMIN;
77 } else if (ActionType.START.equals(action)) {
78 return CSpaceAction.START;
79 } else if (ActionType.STOP.equals(action)) {
80 return CSpaceAction.STOP;
83 // We could not find a match, so we need to throw an exception.
85 throw new IllegalArgumentException("action = " + action.toString());
89 * Add the ACE hashed ID to the permission action so we can map the permission to the Spring Security
92 private void handlePermissionActions(Permission perm) {
93 //FIXME: REM - Having Java class loader issues with ActionType class. Not sure of the cause.
95 List<PermissionAction> permActions = perm.getActions();
96 for (PermissionAction permAction : permActions) {
97 CSpaceAction action = getAction(permAction.getName());
98 URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
99 perm.getResourceName(), action);
100 permAction.setObjectIdentity(uriRes.getHashedId().toString());
101 permAction.setObjectIdentityResource(uriRes.getId());
102 //PermissionActionUtil.update(perm, permAction);
104 } catch (Exception x) {
110 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
111 String id = UUID.randomUUID().toString();
112 Permission permission = wrapDoc.getWrappedObject();
113 permission.setCsid(id);
114 setTenant(permission);
115 handlePermissionActions(permission);
119 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
123 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
124 Permission permissionFound = wrapDoc.getWrappedObject();
125 Permission permissionReceived = getCommonPart();
126 merge(permissionReceived, permissionFound);
130 * merge manually merges the from from to the to permission
131 * -this method is created due to inefficiency of JPA EM merge
134 * @return merged permission
136 private Permission merge(Permission from, Permission to) throws Exception {
137 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
138 String msg = "Resource name cannot be changed " + to.getResourceName();
140 throw new BadRequestException(msg);
142 //resource name, attribute cannot be changed
144 if (from.getDescription() != null) {
145 to.setDescription(from.getDescription());
147 if (from.getEffect() != null) {
148 to.setEffect(from.getEffect());
150 List<PermissionAction> fromActions = from.getActions();
151 if (!fromActions.isEmpty()) {
152 //override the whole list, no reconcilliation by design
153 to.setActions(fromActions);
156 if (logger.isDebugEnabled()) {
157 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
160 handlePermissionActions(to);
165 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
166 Permission upAcc = wrapDoc.getWrappedObject();
167 getServiceContext().setOutput(upAcc);
169 //FIXME update lower-layer authorization (acls)
170 //will require deleting old permissions for this resource and adding
171 //new based on new actions and effect
175 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
176 setCommonPart(extractCommonPart(wrapDoc));
177 sanitize(getCommonPart());
178 getServiceContext().setOutput(permission);
182 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
183 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
184 setCommonPartList(permissionsList);
185 getServiceContext().setOutput(getCommonPartList());
189 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
193 public Permission extractCommonPart(
194 DocumentWrapper<Permission> wrapDoc)
196 return wrapDoc.getWrappedObject();
200 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
202 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
206 public PermissionsList extractCommonPartList(
207 DocumentWrapper<List> wrapDoc)
210 PermissionsList permissionsList = new PermissionsList();
211 List<Permission> list = new ArrayList<Permission>();
212 permissionsList.setPermissions(list);
213 for (Object obj : wrapDoc.getWrappedObject()) {
214 Permission permission = (Permission) obj;
215 sanitize(permission);
216 list.add(permission);
218 return permissionsList;
222 public Permission getCommonPart() {
227 public void setCommonPart(Permission permission) {
228 this.permission = permission;
232 public PermissionsList getCommonPartList() {
233 return permissionsList;
237 public void setCommonPartList(PermissionsList permissionsList) {
238 this.permissionsList = permissionsList;
242 public String getQProperty(
248 public DocumentFilter createDocumentFilter() {
249 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
254 * sanitize removes data not needed to be sent to the consumer
257 private void sanitize(Permission permission) {
258 if (!SecurityUtils.isCSpaceAdmin()) {
259 permission.setTenantId(null);
263 private void setTenant(Permission permission) {
264 //set tenant only if not available from input
265 if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
266 permission.setTenantId(getServiceContext().getTenantId());