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 //FIXME: if admin updating the permission is a CS admin rather than
59 //the tenant admin, tenant id should be retrieved from the request
60 permission.setTenantId(getServiceContext().getTenantId());
64 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
68 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
69 Permission permissionFound = wrapDoc.getWrappedObject();
70 Permission permissionReceived = getCommonPart();
71 merge(permissionReceived, permissionFound);
75 * merge manually merges the from from to the to permission
76 * -this method is created due to inefficiency of JPA EM merge
79 * @return merged permission
81 private Permission merge(Permission from, Permission to) throws Exception {
82 if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
83 String msg = "Resource name cannot be changed " + to.getResourceName();
85 throw new BadRequestException(msg);
87 //resource name, attribute cannot be changed
89 if (from.getDescription() != null) {
90 to.setDescription(from.getDescription());
92 if (from.getEffect() != null) {
93 to.setEffect(from.getEffect());
95 List<PermissionAction> fromActions = from.getActions();
96 if (!fromActions.isEmpty()) {
97 //override the whole list, no reconcilliation by design
98 to.setActions(fromActions);
101 if (logger.isDebugEnabled()) {
102 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
109 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
110 Permission upAcc = wrapDoc.getWrappedObject();
111 getServiceContext().setOutput(permission);
113 //FIXME update lower-layer authorization (acls)
114 //will require deleting old permissions for this resource and adding
115 //new based on new actions and effect
119 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
120 setCommonPart(extractCommonPart(wrapDoc));
121 sanitize(getCommonPart());
122 getServiceContext().setOutput(permission);
126 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
127 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
128 setCommonPartList(permissionsList);
129 getServiceContext().setOutput(getCommonPartList());
133 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
137 public Permission extractCommonPart(
138 DocumentWrapper<Permission> wrapDoc)
140 return wrapDoc.getWrappedObject();
144 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
146 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
150 public PermissionsList extractCommonPartList(
151 DocumentWrapper<List> wrapDoc)
154 PermissionsList permissionsList = new PermissionsList();
155 List<Permission> list = new ArrayList<Permission>();
156 permissionsList.setPermissions(list);
157 for (Object obj : wrapDoc.getWrappedObject()) {
158 Permission permission = (Permission) obj;
159 sanitize(permission);
160 list.add(permission);
162 return permissionsList;
166 public Permission getCommonPart() {
171 public void setCommonPart(Permission permission) {
172 this.permission = permission;
176 public PermissionsList getCommonPartList() {
177 return permissionsList;
181 public void setCommonPartList(PermissionsList permissionsList) {
182 this.permissionsList = permissionsList;
186 public String getQProperty(
192 public DocumentFilter createDocumentFilter() {
193 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
198 * sanitize removes data not needed to be sent to the consumer
201 private void sanitize(Permission permission) {
202 permission.setTenantId(null);