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 javax.persistence.EntityExistsException;
31 import javax.persistence.EntityManager;
32 import javax.persistence.NoResultException;
34 import org.collectionspace.services.authorization.perms.ActionType;
35 import org.collectionspace.services.authorization.CSpaceAction;
36 import org.collectionspace.services.authorization.RolesList;
37 import org.collectionspace.services.authorization.perms.Permission;
38 import org.collectionspace.services.authorization.perms.PermissionAction;
39 import org.collectionspace.services.authorization.perms.PermissionsList;
40 import org.collectionspace.services.client.PermissionClient;
41 import org.collectionspace.services.client.PermissionClient.ActionCompare;
42 import org.collectionspace.services.authorization.URIResourceImpl;
43 import org.collectionspace.services.common.authorization_mgt.AuthorizationStore;
44 import org.collectionspace.services.common.document.BadRequestException;
45 import org.collectionspace.services.common.document.DocumentException;
46 import org.collectionspace.services.common.document.DocumentFilter;
47 import org.collectionspace.services.common.document.DocumentWrapper;
48 import org.collectionspace.services.common.document.JaxbUtils;
49 import org.collectionspace.services.common.security.SecurityUtils;
50 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
51 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Document handler for Permission
60 public class PermissionDocumentHandler
61 extends JpaDocumentHandler<Permission, PermissionsList, Permission, List<Permission>> {
63 private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
64 private Permission permission;
65 private PermissionsList permissionsList;
67 public CSpaceAction getAction(ActionType action) {
68 if (ActionType.CREATE.name().equals(action.name())) {
69 return CSpaceAction.CREATE;
70 } else if (ActionType.READ.equals(action)) {
71 return CSpaceAction.READ;
72 } else if (ActionType.UPDATE.equals(action)) {
73 return CSpaceAction.UPDATE;
74 } else if (ActionType.DELETE.equals(action)) {
75 return CSpaceAction.DELETE;
76 } else if (ActionType.SEARCH.equals(action)) {
77 return CSpaceAction.SEARCH;
78 } else if (ActionType.ADMIN.equals(action)) {
79 return CSpaceAction.ADMIN;
80 } else if (ActionType.START.equals(action)) {
81 return CSpaceAction.START;
82 } else if (ActionType.STOP.equals(action)) {
83 return CSpaceAction.STOP;
86 // We could not find a match, so we need to throw an exception.
88 throw new IllegalArgumentException("action = " + action.toString());
92 * Add the ACE hashed ID to the permission action so we can map the permission to the Spring Security
95 private void handlePermissionActions(Permission perm) throws DocumentException {
97 // Verify the permission actions. If the action group is missing, create it from the action list and vice versa.
99 ActionCompare compareResult = PermissionClient.validatePermActions(perm);
100 switch (compareResult) {
101 case ACTIONS_MISSING:
102 String msg = "Permission resource encountered with missing action group and action list.";
103 throw new DocumentException(msg);
105 case ACTION_GROUP_EMPTY:
106 String actionGroup = PermissionClient.getActionGroup(perm.getAction());
107 perm.setActionGroup(actionGroup);
110 case ACTION_LIST_EMPTY:
111 List<PermissionAction> actionList = PermissionClient.getActionList(perm.getActionGroup());
112 perm.setAction(actionList);
120 msg = String.format("Permission has mismatching action group and action list. Action group='%s' and Action list = '%s'.",
121 perm.getActionGroup(), PermissionClient.getActionGroup(perm.getAction()));
122 throw new DocumentException(msg);
125 List<PermissionAction> permActions = perm.getAction();
126 for (PermissionAction permAction : permActions) {
127 CSpaceAction action = getAction(permAction.getName());
128 URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(), perm.getResourceName(), action);
129 permAction.setObjectIdentity(uriRes.getHashedId().toString());
130 permAction.setObjectIdentityResource(uriRes.getId());
131 //PermissionActionUtil.update(perm, permAction);
135 private Permission findExistingPermission(EntityManager em, Permission perm) {
136 Permission result = null;
137 String tenantId = getServiceContext().getTenantId(); // we need a tenant ID
140 result = (Permission)JpaStorageUtils.getEntityByDualKeys(em,
141 Permission.class.getName(),
142 PermissionStorageConstants.RESOURCE_NAME, perm.getResourceName(),
143 PermissionStorageConstants.ACTION_GROUP, perm.getActionGroup(),
145 } catch (NoResultException e) {
146 if (logger.isTraceEnabled()) {
147 String msg = String.format("Looked for but could not find permission with resource name = '%s', action group = '%s', tenat ID = '%s'.",
148 perm.getResourceName(), perm.getActionGroup(), tenantId);
157 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws EntityExistsException, DocumentException {
159 // First check to see if an equivalent permission exists
161 Permission permission = wrapDoc.getWrappedObject();
163 EntityManager em = (EntityManager) this.getServiceContext().getProperty(AuthorizationStore.ENTITY_MANAGER_PROP_KEY);
164 Permission existingPermission = findExistingPermission(em, permission);
166 if (existingPermission == null) {
167 String id = UUID.randomUUID().toString();
168 permission.setCsid(id);
169 setTenant(permission);
170 handlePermissionActions(permission);
172 String msg = String.format("Found existing permission with resource name = '%s', action group = '%s', and tenant ID = '%s'.",
173 existingPermission.getResourceName(), existingPermission.getActionGroup(), existingPermission.getTenantId());
174 wrapDoc.resetWrapperObject(existingPermission); // update the wrapped document with the existing permission instance
175 throw new EntityExistsException(msg);
180 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
184 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
185 Permission permissionFound = wrapDoc.getWrappedObject();
186 Permission permissionReceived = getCommonPart();
187 merge(permissionReceived, permissionFound);
191 * merge manually merges the from from to the to permission
192 * -this method is created due to inefficiency of JPA EM merge
195 * @return merged permission
197 private Permission merge(Permission from, Permission to) throws Exception {
198 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
199 String msg = "Resource name cannot be changed " + to.getResourceName();
201 throw new BadRequestException(msg);
203 //resource name, attribute cannot be changed
205 if (from.getDescription() != null) {
206 to.setDescription(from.getDescription());
208 if (from.getEffect() != null) {
209 to.setEffect(from.getEffect());
211 List<PermissionAction> fromActions = from.getAction();
212 if (!fromActions.isEmpty()) {
213 // Override the whole list, no reconciliation by design
214 to.setAction(fromActions);
215 // Update the actionGroup field to reflect the new action list
216 to.setActionGroup(PermissionClient.getActionGroup(fromActions));
219 if (logger.isDebugEnabled()) {
220 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
223 handlePermissionActions(to);
228 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
229 Permission upAcc = wrapDoc.getWrappedObject();
230 getServiceContext().setOutput(upAcc);
232 //FIXME update lower-layer authorization (acls)
233 //will require deleting old permissions for this resource and adding
234 //new based on new actions and effect
238 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
239 setCommonPart(extractCommonPart(wrapDoc));
240 sanitize(getCommonPart());
241 getServiceContext().setOutput(permission);
245 public void handleGetAll(DocumentWrapper<List<Permission>> wrapDoc) throws Exception {
246 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
247 setCommonPartList(permissionsList);
248 getServiceContext().setOutput(getCommonPartList());
252 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
256 * See https://issues.collectionspace.org/browse/DRYD-181
258 * For backward compatibility, we could not change the permission list to be a child class of AbstractCommonList. This
259 * would have change the result payload and would break existing API clients. So the best we can do, it treat
260 * the role list payload as a special case and return the paging information.
263 protected PermissionsList extractPagingInfoForPerms(PermissionsList permList, DocumentWrapper<List<Permission>> wrapDoc)
266 DocumentFilter docFilter = this.getDocumentFilter();
267 long pageSize = docFilter.getPageSize();
268 long pageNum = pageSize != 0 ? docFilter.getOffset() / pageSize : pageSize;
269 // set the page size and page number
270 permList.setPageNum(pageNum);
271 permList.setPageSize(pageSize);
272 List<Permission> docList = wrapDoc.getWrappedObject();
273 // Set num of items in list. this is useful to our testing framework.
274 permList.setItemsInPage(docList.size());
275 // set the total result size
276 permList.setTotalItems(docFilter.getTotalItemsResult());
282 public Permission extractCommonPart(
283 DocumentWrapper<Permission> wrapDoc)
285 return wrapDoc.getWrappedObject();
289 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
291 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
295 public PermissionsList extractCommonPartList(
296 DocumentWrapper<List<Permission>> wrapDoc)
299 PermissionsList permissionsList = extractPagingInfoForPerms(new PermissionsList(), wrapDoc);
300 List<Permission> list = new ArrayList<Permission>();
301 permissionsList.setPermission(list);
302 for (Object obj : wrapDoc.getWrappedObject()) {
303 Permission permission = (Permission) obj;
304 sanitize(permission);
305 list.add(permission);
308 return permissionsList;
312 public Permission getCommonPart() {
317 public void setCommonPart(Permission permission) {
318 this.permission = permission;
322 public PermissionsList getCommonPartList() {
323 return permissionsList;
327 public void setCommonPartList(PermissionsList permissionsList) {
328 this.permissionsList = permissionsList;
332 public String getQProperty(
338 public DocumentFilter createDocumentFilter() {
339 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
344 * Sanitize removes data not needed to be sent to the consumer
347 private void sanitize(Permission permission) {
348 if (!SecurityUtils.isCSpaceAdmin()) {
349 // permission.setTenantId(null); // REM - Why are we removing the tenant ID from the payload? Commenting out this line for now.
353 private void setTenant(Permission permission) {
354 //set tenant only if not available from input
355 if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
356 permission.setTenantId(getServiceContext().getTenantId());