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.Permission;
31 import org.collectionspace.services.authorization.PermissionAction;
32 import org.collectionspace.services.authorization.PermissionsList;
34 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
35 import org.collectionspace.services.common.document.BadRequestException;
36 import org.collectionspace.services.common.document.DocumentFilter;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.common.document.JaxbUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Document handler for Permission
46 public class PermissionDocumentHandler
47 extends AbstractDocumentHandlerImpl<Permission, PermissionsList, Permission, List> {
49 private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
50 private Permission permission;
51 private PermissionsList permissionsList;
54 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
55 String id = UUID.randomUUID().toString();
56 Permission permission = wrapDoc.getWrappedObject();
57 permission.setCsid(id);
58 setTenant(permission);
62 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
66 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
67 Permission permissionFound = wrapDoc.getWrappedObject();
68 Permission permissionReceived = getCommonPart();
69 merge(permissionReceived, permissionFound);
73 * merge manually merges the from from to the to permission
74 * -this method is created due to inefficiency of JPA EM merge
77 * @return merged permission
79 private Permission merge(Permission from, Permission to) throws Exception {
80 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
81 String msg = "Resource name cannot be changed " + to.getResourceName();
83 throw new BadRequestException(msg);
85 //resource name, attribute cannot be changed
87 if (from.getDescription() != null) {
88 to.setDescription(from.getDescription());
90 if (from.getEffect() != null) {
91 to.setEffect(from.getEffect());
93 List<PermissionAction> fromActions = from.getActions();
94 if (!fromActions.isEmpty()) {
95 //override the whole list, no reconcilliation by design
96 to.setActions(fromActions);
99 if (logger.isDebugEnabled()) {
100 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
107 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
108 Permission upAcc = wrapDoc.getWrappedObject();
109 getServiceContext().setOutput(permission);
111 //FIXME update lower-layer authorization (acls)
112 //will require deleting old permissions for this resource and adding
113 //new based on new actions and effect
117 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
118 setCommonPart(extractCommonPart(wrapDoc));
119 sanitize(getCommonPart());
120 getServiceContext().setOutput(permission);
124 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
125 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
126 setCommonPartList(permissionsList);
127 getServiceContext().setOutput(getCommonPartList());
131 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
135 public Permission extractCommonPart(
136 DocumentWrapper<Permission> wrapDoc)
138 return wrapDoc.getWrappedObject();
142 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
144 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
148 public PermissionsList extractCommonPartList(
149 DocumentWrapper<List> wrapDoc)
152 PermissionsList permissionsList = new PermissionsList();
153 List<Permission> list = new ArrayList<Permission>();
154 permissionsList.setPermissions(list);
155 for (Object obj : wrapDoc.getWrappedObject()) {
156 Permission permission = (Permission) obj;
157 sanitize(permission);
158 list.add(permission);
160 return permissionsList;
164 public Permission getCommonPart() {
169 public void setCommonPart(Permission permission) {
170 this.permission = permission;
174 public PermissionsList getCommonPartList() {
175 return permissionsList;
179 public void setCommonPartList(PermissionsList permissionsList) {
180 this.permissionsList = permissionsList;
184 public String getQProperty(
190 public DocumentFilter createDocumentFilter() {
191 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
196 * sanitize removes data not needed to be sent to the consumer
199 private void sanitize(Permission permission) {
200 permission.setTenantId(null);
203 private void setTenant(Permission permission) {
204 //set tenant only if not available from input
205 if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
206 permission.setTenantId(getServiceContext().getTenantId());