]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9af981151e9925bff1d3876255663e09fb57cf48
[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.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;
42
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;
52
53 /**
54  * Document handler for Permission
55  * @author 
56  */
57 public class PermissionDocumentHandler
58                 extends JpaDocumentHandler<Permission, PermissionsList, Permission, List> {
59
60     private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
61     private Permission permission;
62     private PermissionsList permissionsList;
63     
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;
81         }
82         //
83         // We could not find a match, so we need to throw an exception.
84         //
85         throw new IllegalArgumentException("action = " + action.toString());
86     }
87     
88     /*
89      * Add the ACE hashed ID to the permission action so we can map the permission to the Spring Security
90      * tables.
91      */
92     private void handlePermissionActions(Permission perm) {
93         //FIXME: REM - Having Java class loader issues with ActionType class.  Not sure of the cause.
94         try {
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);
102                 }
103         } catch (Exception x) {
104                 x.printStackTrace();
105         }
106     }
107
108     @Override
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);
115     }
116
117     @Override
118     public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
119     }
120
121     @Override
122     public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
123         Permission permissionFound = wrapDoc.getWrappedObject();
124         Permission permissionReceived = getCommonPart();
125         merge(permissionReceived, permissionFound);
126     }
127
128     /**
129      * merge manually merges the from from to the to permission
130      * -this method is created due to inefficiency of JPA EM merge
131      * @param from
132      * @param to
133      * @return merged permission
134      */
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();
138             logger.error(msg);
139             throw new BadRequestException(msg);
140         }
141         //resource name, attribute  cannot be changed
142
143         if (from.getDescription() != null) {
144             to.setDescription(from.getDescription());
145         }
146         if (from.getEffect() != null) {
147             to.setEffect(from.getEffect());
148         }
149         List<PermissionAction> fromActions = from.getActions();
150         if (!fromActions.isEmpty()) {
151             //override the whole list, no reconcilliation by design
152             to.setActions(fromActions);
153         }
154
155         if (logger.isDebugEnabled()) {
156             logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
157         }
158
159         handlePermissionActions(to);
160         return to;
161     }
162
163     @Override
164     public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
165         Permission upAcc = wrapDoc.getWrappedObject();
166         getServiceContext().setOutput(permission);
167         sanitize(upAcc);
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
171     }
172
173     @Override
174     public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
175         setCommonPart(extractCommonPart(wrapDoc));
176         sanitize(getCommonPart());
177         getServiceContext().setOutput(permission);
178     }
179
180     @Override
181     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
182         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
183         setCommonPartList(permissionsList);
184         getServiceContext().setOutput(getCommonPartList());
185     }
186
187     @Override
188     public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
189     }
190
191     @Override
192     public Permission extractCommonPart(
193             DocumentWrapper<Permission> wrapDoc)
194             throws Exception {
195         return wrapDoc.getWrappedObject();
196     }
197
198     @Override
199     public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
200             throws Exception {
201         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
202     }
203
204     @Override
205     public PermissionsList extractCommonPartList(
206             DocumentWrapper<List> wrapDoc)
207             throws Exception {
208
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);
216         }
217         return permissionsList;
218     }
219
220     @Override
221     public Permission getCommonPart() {
222         return permission;
223     }
224
225     @Override
226     public void setCommonPart(Permission permission) {
227         this.permission = permission;
228     }
229
230     @Override
231     public PermissionsList getCommonPartList() {
232         return permissionsList;
233     }
234
235     @Override
236     public void setCommonPartList(PermissionsList permissionsList) {
237         this.permissionsList = permissionsList;
238     }
239
240     @Override
241     public String getQProperty(
242             String prop) {
243         return null;
244     }
245
246     @Override
247     public DocumentFilter createDocumentFilter() {
248         DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
249         return filter;
250     }
251
252     /**
253      * sanitize removes data not needed to be sent to the consumer
254      * @param permission
255      */
256     private void sanitize(Permission permission) {
257         if (!SecurityUtils.isCSpaceAdmin()) {
258             permission.setTenantId(null);
259         }
260     }
261
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());
266         }
267     }
268 }