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 //PermissionActionUtil.update(perm, permAction);
103 } catch (Exception x) {
109 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
110 String id = UUID.randomUUID().toString();
111 Permission permission = wrapDoc.getWrappedObject();
112 permission.setCsid(id);
113 setTenant(permission);
114 handlePermissionActions(permission);
118 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
122 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
123 Permission permissionFound = wrapDoc.getWrappedObject();
124 Permission permissionReceived = getCommonPart();
125 merge(permissionReceived, permissionFound);
129 * merge manually merges the from from to the to permission
130 * -this method is created due to inefficiency of JPA EM merge
133 * @return merged permission
135 private Permission merge(Permission from, Permission to) throws Exception {
136 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
137 String msg = "Resource name cannot be changed " + to.getResourceName();
139 throw new BadRequestException(msg);
141 //resource name, attribute cannot be changed
143 if (from.getDescription() != null) {
144 to.setDescription(from.getDescription());
146 if (from.getEffect() != null) {
147 to.setEffect(from.getEffect());
149 List<PermissionAction> fromActions = from.getActions();
150 if (!fromActions.isEmpty()) {
151 //override the whole list, no reconcilliation by design
152 to.setActions(fromActions);
155 if (logger.isDebugEnabled()) {
156 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
159 handlePermissionActions(to);
164 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
165 Permission upAcc = wrapDoc.getWrappedObject();
166 getServiceContext().setOutput(permission);
168 //FIXME update lower-layer authorization (acls)
169 //will require deleting old permissions for this resource and adding
170 //new based on new actions and effect
174 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
175 setCommonPart(extractCommonPart(wrapDoc));
176 sanitize(getCommonPart());
177 getServiceContext().setOutput(permission);
181 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
182 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
183 setCommonPartList(permissionsList);
184 getServiceContext().setOutput(getCommonPartList());
188 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
192 public Permission extractCommonPart(
193 DocumentWrapper<Permission> wrapDoc)
195 return wrapDoc.getWrappedObject();
199 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
201 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
205 public PermissionsList extractCommonPartList(
206 DocumentWrapper<List> wrapDoc)
209 PermissionsList permissionsList = new PermissionsList();
210 List<Permission> list = new ArrayList<Permission>();
211 permissionsList.setPermissions(list);
212 for (Object obj : wrapDoc.getWrappedObject()) {
213 Permission permission = (Permission) obj;
214 sanitize(permission);
215 list.add(permission);
217 return permissionsList;
221 public Permission getCommonPart() {
226 public void setCommonPart(Permission permission) {
227 this.permission = permission;
231 public PermissionsList getCommonPartList() {
232 return permissionsList;
236 public void setCommonPartList(PermissionsList permissionsList) {
237 this.permissionsList = permissionsList;
241 public String getQProperty(
247 public DocumentFilter createDocumentFilter() {
248 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
253 * sanitize removes data not needed to be sent to the consumer
256 private void sanitize(Permission permission) {
257 if (!SecurityUtils.isCSpaceAdmin()) {
258 permission.setTenantId(null);
262 private void setTenant(Permission permission) {
263 //set tenant only if not available from input
264 if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
265 permission.setTenantId(getServiceContext().getTenantId());