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 System.out.println("Hello, world? " + action.name());
66 System.out.println("Hello, world? " + ActionType.CREATE.name());
69 if (ActionType.CREATE.name().equals(action.name())) {
70 return CSpaceAction.CREATE;
71 } else if (ActionType.READ.equals(action)) {
72 return CSpaceAction.READ;
73 } else if (ActionType.UPDATE.equals(action)) {
74 return CSpaceAction.UPDATE;
75 } else if (ActionType.DELETE.equals(action)) {
76 return CSpaceAction.DELETE;
77 } else if (ActionType.SEARCH.equals(action)) {
78 return CSpaceAction.SEARCH;
79 } else if (ActionType.ADMIN.equals(action)) {
80 return CSpaceAction.ADMIN;
81 } else if (ActionType.START.equals(action)) {
82 return CSpaceAction.START;
83 } else if (ActionType.STOP.equals(action)) {
84 return CSpaceAction.STOP;
86 } catch (Exception x) {
89 throw new IllegalArgumentException("action = " + action.toString());
93 * Add the ACE hashed ID to the permission action so we can map the permission to the Spring Security
96 private void handlePermissionActions(Permission perm) {
97 //FIXME: REM - Having Java class loader issues with ActionType class. Not sure of the cause.
99 List<PermissionAction> permActions = perm.getActions();
100 for (PermissionAction permAction : permActions) {
101 CSpaceAction action = getAction(permAction.getName());
102 URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
103 perm.getResourceName(), action);
104 permAction.setObjectIdentity(uriRes.getHashedId().toString());
105 //PermissionActionUtil.update(perm, permAction);
107 } catch (Exception x) {
113 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
114 String id = UUID.randomUUID().toString();
115 Permission permission = wrapDoc.getWrappedObject();
116 permission.setCsid(id);
117 setTenant(permission);
118 handlePermissionActions(permission);
122 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
126 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
127 Permission permissionFound = wrapDoc.getWrappedObject();
128 Permission permissionReceived = getCommonPart();
129 merge(permissionReceived, permissionFound);
133 * merge manually merges the from from to the to permission
134 * -this method is created due to inefficiency of JPA EM merge
137 * @return merged permission
139 private Permission merge(Permission from, Permission to) throws Exception {
140 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
141 String msg = "Resource name cannot be changed " + to.getResourceName();
143 throw new BadRequestException(msg);
145 //resource name, attribute cannot be changed
147 if (from.getDescription() != null) {
148 to.setDescription(from.getDescription());
150 if (from.getEffect() != null) {
151 to.setEffect(from.getEffect());
153 List<PermissionAction> fromActions = from.getActions();
154 if (!fromActions.isEmpty()) {
155 //override the whole list, no reconcilliation by design
156 to.setActions(fromActions);
159 if (logger.isDebugEnabled()) {
160 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
163 handlePermissionActions(to);
168 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
169 Permission upAcc = wrapDoc.getWrappedObject();
170 getServiceContext().setOutput(permission);
172 //FIXME update lower-layer authorization (acls)
173 //will require deleting old permissions for this resource and adding
174 //new based on new actions and effect
178 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
179 setCommonPart(extractCommonPart(wrapDoc));
180 sanitize(getCommonPart());
181 getServiceContext().setOutput(permission);
185 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
186 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
187 setCommonPartList(permissionsList);
188 getServiceContext().setOutput(getCommonPartList());
192 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
196 public Permission extractCommonPart(
197 DocumentWrapper<Permission> wrapDoc)
199 return wrapDoc.getWrappedObject();
203 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
205 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
209 public PermissionsList extractCommonPartList(
210 DocumentWrapper<List> wrapDoc)
213 PermissionsList permissionsList = new PermissionsList();
214 List<Permission> list = new ArrayList<Permission>();
215 permissionsList.setPermissions(list);
216 for (Object obj : wrapDoc.getWrappedObject()) {
217 Permission permission = (Permission) obj;
218 sanitize(permission);
219 list.add(permission);
221 return permissionsList;
225 public Permission getCommonPart() {
230 public void setCommonPart(Permission permission) {
231 this.permission = permission;
235 public PermissionsList getCommonPartList() {
236 return permissionsList;
240 public void setCommonPartList(PermissionsList permissionsList) {
241 this.permissionsList = permissionsList;
245 public String getQProperty(
251 public DocumentFilter createDocumentFilter() {
252 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
257 * sanitize removes data not needed to be sent to the consumer
260 private void sanitize(Permission permission) {
261 if (!SecurityUtils.isCSpaceAdmin()) {
262 permission.setTenantId(null);
266 private void setTenant(Permission permission) {
267 //set tenant only if not available from input
268 if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
269 permission.setTenantId(getServiceContext().getTenantId());