]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bf6977c149cb4706f63d44e790c19971d9935d7e
[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                     permAction.setObjectIdentityResource(uriRes.getId());
102                     //PermissionActionUtil.update(perm, permAction);
103                 }
104         } catch (Exception x) {
105                 x.printStackTrace();
106         }
107     }
108
109     @Override
110     public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
111         String id = UUID.randomUUID().toString();
112         Permission permission = wrapDoc.getWrappedObject();
113         permission.setCsid(id);
114         setTenant(permission);
115         handlePermissionActions(permission);
116     }
117
118     @Override
119     public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
120     }
121
122     @Override
123     public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
124         Permission permissionFound = wrapDoc.getWrappedObject();
125         Permission permissionReceived = getCommonPart();
126         merge(permissionReceived, permissionFound);
127     }
128
129     /**
130      * merge manually merges the from from to the to permission
131      * -this method is created due to inefficiency of JPA EM merge
132      * @param from
133      * @param to
134      * @return merged permission
135      */
136     private Permission merge(Permission from, Permission to) throws Exception {
137         if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
138             String msg = "Resource name cannot be changed " + to.getResourceName();
139             logger.error(msg);
140             throw new BadRequestException(msg);
141         }
142         //resource name, attribute  cannot be changed
143
144         if (from.getDescription() != null) {
145             to.setDescription(from.getDescription());
146         }
147         if (from.getEffect() != null) {
148             to.setEffect(from.getEffect());
149         }
150         List<PermissionAction> fromActions = from.getActions();
151         if (!fromActions.isEmpty()) {
152             //override the whole list, no reconcilliation by design
153             to.setActions(fromActions);
154         }
155
156         if (logger.isDebugEnabled()) {
157             logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
158         }
159
160         handlePermissionActions(to);
161         return to;
162     }
163
164     @Override
165     public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
166         Permission upAcc = wrapDoc.getWrappedObject();
167         getServiceContext().setOutput(permission);
168         sanitize(upAcc);
169         //FIXME update lower-layer authorization (acls)
170         //will require deleting old permissions for this resource and adding
171         //new based on new actions and effect
172     }
173
174     @Override
175     public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
176         setCommonPart(extractCommonPart(wrapDoc));
177         sanitize(getCommonPart());
178         getServiceContext().setOutput(permission);
179     }
180
181     @Override
182     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
183         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
184         setCommonPartList(permissionsList);
185         getServiceContext().setOutput(getCommonPartList());
186     }
187
188     @Override
189     public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
190     }
191
192     @Override
193     public Permission extractCommonPart(
194             DocumentWrapper<Permission> wrapDoc)
195             throws Exception {
196         return wrapDoc.getWrappedObject();
197     }
198
199     @Override
200     public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
201             throws Exception {
202         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
203     }
204
205     @Override
206     public PermissionsList extractCommonPartList(
207             DocumentWrapper<List> wrapDoc)
208             throws Exception {
209
210         PermissionsList permissionsList = new PermissionsList();
211         List<Permission> list = new ArrayList<Permission>();
212         permissionsList.setPermissions(list);
213         for (Object obj : wrapDoc.getWrappedObject()) {
214             Permission permission = (Permission) obj;
215             sanitize(permission);
216             list.add(permission);
217         }
218         return permissionsList;
219     }
220
221     @Override
222     public Permission getCommonPart() {
223         return permission;
224     }
225
226     @Override
227     public void setCommonPart(Permission permission) {
228         this.permission = permission;
229     }
230
231     @Override
232     public PermissionsList getCommonPartList() {
233         return permissionsList;
234     }
235
236     @Override
237     public void setCommonPartList(PermissionsList permissionsList) {
238         this.permissionsList = permissionsList;
239     }
240
241     @Override
242     public String getQProperty(
243             String prop) {
244         return null;
245     }
246
247     @Override
248     public DocumentFilter createDocumentFilter() {
249         DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
250         return filter;
251     }
252
253     /**
254      * sanitize removes data not needed to be sent to the consumer
255      * @param permission
256      */
257     private void sanitize(Permission permission) {
258         if (!SecurityUtils.isCSpaceAdmin()) {
259             permission.setTenantId(null);
260         }
261     }
262
263     private void setTenant(Permission permission) {
264         //set tenant only if not available from input
265         if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
266             permission.setTenantId(getServiceContext().getTenantId());
267         }
268     }
269 }