]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
94d9e03a616e41901f3a8f1434fb3734d14394fe
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29
30 import org.collectionspace.services.authorization.AccountRole;
31 import org.collectionspace.services.authorization.AccountRoleRel;
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionsList;
35 import org.collectionspace.services.authorization.PermissionsRolesList;
36
37 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
38 import org.collectionspace.services.common.document.BadRequestException;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentWrapper;
41 import org.collectionspace.services.common.document.JaxbUtils;
42 import org.collectionspace.services.common.security.SecurityUtils;
43 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Document handler for Permission
49  * @author 
50  */
51 public class PermissionDocumentHandler
52                 extends JpaDocumentHandler<Permission, PermissionsList, Permission, List> {
53
54     private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
55     private Permission permission;
56     private PermissionsList permissionsList;
57
58     @Override
59     public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
60         String id = UUID.randomUUID().toString();
61         Permission permission = wrapDoc.getWrappedObject();
62         permission.setCsid(id);
63         setTenant(permission);
64     }
65
66     @Override
67     public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
68     }
69
70     @Override
71     public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
72         Permission permissionFound = wrapDoc.getWrappedObject();
73         Permission permissionReceived = getCommonPart();
74         merge(permissionReceived, permissionFound);
75     }
76
77     /**
78      * merge manually merges the from from to the to permission
79      * -this method is created due to inefficiency of JPA EM merge
80      * @param from
81      * @param to
82      * @return merged permission
83      */
84     private Permission merge(Permission from, Permission to) throws Exception {
85         if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
86             String msg = "Resource name cannot be changed " + to.getResourceName();
87             logger.error(msg);
88             throw new BadRequestException(msg);
89         }
90         //resource name, attribute  cannot be changed
91
92         if (from.getDescription() != null) {
93             to.setDescription(from.getDescription());
94         }
95         if (from.getEffect() != null) {
96             to.setEffect(from.getEffect());
97         }
98         List<PermissionAction> fromActions = from.getActions();
99         if (!fromActions.isEmpty()) {
100             //override the whole list, no reconcilliation by design
101             to.setActions(fromActions);
102         }
103
104         if (logger.isDebugEnabled()) {
105             logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
106         }
107
108         return to;
109     }
110
111     @Override
112     public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
113         Permission upAcc = wrapDoc.getWrappedObject();
114         getServiceContext().setOutput(permission);
115         sanitize(upAcc);
116         //FIXME update lower-layer authorization (acls)
117         //will require deleting old permissions for this resource and adding
118         //new based on new actions and effect
119     }
120
121     @Override
122     public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
123         setCommonPart(extractCommonPart(wrapDoc));
124         sanitize(getCommonPart());
125         getServiceContext().setOutput(permission);
126     }
127
128     @Override
129     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
130         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
131         setCommonPartList(permissionsList);
132         getServiceContext().setOutput(getCommonPartList());
133     }
134
135     @Override
136     public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
137     }
138
139     @Override
140     public Permission extractCommonPart(
141             DocumentWrapper<Permission> wrapDoc)
142             throws Exception {
143         return wrapDoc.getWrappedObject();
144     }
145
146     @Override
147     public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
148             throws Exception {
149         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
150     }
151
152     @Override
153     public PermissionsList extractCommonPartList(
154             DocumentWrapper<List> wrapDoc)
155             throws Exception {
156
157         PermissionsList permissionsList = new PermissionsList();
158         List<Permission> list = new ArrayList<Permission>();
159         permissionsList.setPermissions(list);
160         for (Object obj : wrapDoc.getWrappedObject()) {
161             Permission permission = (Permission) obj;
162             sanitize(permission);
163             list.add(permission);
164         }
165         return permissionsList;
166     }
167
168     @Override
169     public Permission getCommonPart() {
170         return permission;
171     }
172
173     @Override
174     public void setCommonPart(Permission permission) {
175         this.permission = permission;
176     }
177
178     @Override
179     public PermissionsList getCommonPartList() {
180         return permissionsList;
181     }
182
183     @Override
184     public void setCommonPartList(PermissionsList permissionsList) {
185         this.permissionsList = permissionsList;
186     }
187
188     @Override
189     public String getQProperty(
190             String prop) {
191         return null;
192     }
193
194     @Override
195     public DocumentFilter createDocumentFilter() {
196         DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
197         return filter;
198     }
199
200     /**
201      * sanitize removes data not needed to be sent to the consumer
202      * @param permission
203      */
204     private void sanitize(Permission permission) {
205         if (!SecurityUtils.isCSpaceAdmin()) {
206             permission.setTenantId(null);
207         }
208     }
209
210     private void setTenant(Permission permission) {
211         //set tenant only if not available from input
212         if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
213             permission.setTenantId(getServiceContext().getTenantId());
214         }
215     }
216 }