]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
29503a36a2ed5d284b7c6356f418a8893b1407ea
[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         System.out.println("Hello, world? " + action.name());
66         System.out.println("Hello, world? " + ActionType.CREATE.name());
67         
68         try {
69         if (ActionType.CREATE.name().equals(action.name())) {
70             return CSpaceAction.CREATE;
71         } else if (ActionType.READ.equals(action)) {
72             return CSpaceAction.READ;
73         } else if (ActionType.UPDATE.equals(action)) {
74             return CSpaceAction.UPDATE;
75         } else if (ActionType.DELETE.equals(action)) {
76             return CSpaceAction.DELETE;
77         } else if (ActionType.SEARCH.equals(action)) {
78             return CSpaceAction.SEARCH;
79         } else if (ActionType.ADMIN.equals(action)) {
80             return CSpaceAction.ADMIN;
81         } else if (ActionType.START.equals(action)) {
82             return CSpaceAction.START;
83         } else if (ActionType.STOP.equals(action)) {
84             return CSpaceAction.STOP;
85         }
86         } catch (Exception x) {
87                 x.printStackTrace();
88         }
89         throw new IllegalArgumentException("action = " + action.toString());
90     }
91     
92     /*
93      * Add the ACE hashed ID to the permission action so we can map the permission to the Spring Security
94      * tables.
95      */
96     private void handlePermissionActions(Permission perm) {
97         //FIXME: REM - Having Java class loader issues with ActionType class.  Not sure of the cause.
98         try {
99                 List<PermissionAction> permActions = perm.getActions();
100                 for (PermissionAction permAction : permActions) {
101                     CSpaceAction action = getAction(permAction.getName());
102                     URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
103                             perm.getResourceName(), action);
104                     permAction.setObjectIdentity(uriRes.getHashedId().toString());
105                     //PermissionActionUtil.update(perm, permAction);
106                 }
107         } catch (Exception x) {
108                 x.printStackTrace();
109         }
110     }
111
112     @Override
113     public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
114         String id = UUID.randomUUID().toString();
115         Permission permission = wrapDoc.getWrappedObject();
116         permission.setCsid(id);
117         setTenant(permission);
118         handlePermissionActions(permission);
119     }
120
121     @Override
122     public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
123     }
124
125     @Override
126     public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
127         Permission permissionFound = wrapDoc.getWrappedObject();
128         Permission permissionReceived = getCommonPart();
129         merge(permissionReceived, permissionFound);
130     }
131
132     /**
133      * merge manually merges the from from to the to permission
134      * -this method is created due to inefficiency of JPA EM merge
135      * @param from
136      * @param to
137      * @return merged permission
138      */
139     private Permission merge(Permission from, Permission to) throws Exception {
140         if (!(from.getResourceName().equalsIgnoreCase(to.getResourceName()))) {
141             String msg = "Resource name cannot be changed " + to.getResourceName();
142             logger.error(msg);
143             throw new BadRequestException(msg);
144         }
145         //resource name, attribute  cannot be changed
146
147         if (from.getDescription() != null) {
148             to.setDescription(from.getDescription());
149         }
150         if (from.getEffect() != null) {
151             to.setEffect(from.getEffect());
152         }
153         List<PermissionAction> fromActions = from.getActions();
154         if (!fromActions.isEmpty()) {
155             //override the whole list, no reconcilliation by design
156             to.setActions(fromActions);
157         }
158
159         if (logger.isDebugEnabled()) {
160             logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
161         }
162
163         handlePermissionActions(to);
164         return to;
165     }
166
167     @Override
168     public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
169         Permission upAcc = wrapDoc.getWrappedObject();
170         getServiceContext().setOutput(permission);
171         sanitize(upAcc);
172         //FIXME update lower-layer authorization (acls)
173         //will require deleting old permissions for this resource and adding
174         //new based on new actions and effect
175     }
176
177     @Override
178     public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
179         setCommonPart(extractCommonPart(wrapDoc));
180         sanitize(getCommonPart());
181         getServiceContext().setOutput(permission);
182     }
183
184     @Override
185     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
186         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
187         setCommonPartList(permissionsList);
188         getServiceContext().setOutput(getCommonPartList());
189     }
190
191     @Override
192     public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
193     }
194
195     @Override
196     public Permission extractCommonPart(
197             DocumentWrapper<Permission> wrapDoc)
198             throws Exception {
199         return wrapDoc.getWrappedObject();
200     }
201
202     @Override
203     public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
204             throws Exception {
205         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
206     }
207
208     @Override
209     public PermissionsList extractCommonPartList(
210             DocumentWrapper<List> wrapDoc)
211             throws Exception {
212
213         PermissionsList permissionsList = new PermissionsList();
214         List<Permission> list = new ArrayList<Permission>();
215         permissionsList.setPermissions(list);
216         for (Object obj : wrapDoc.getWrappedObject()) {
217             Permission permission = (Permission) obj;
218             sanitize(permission);
219             list.add(permission);
220         }
221         return permissionsList;
222     }
223
224     @Override
225     public Permission getCommonPart() {
226         return permission;
227     }
228
229     @Override
230     public void setCommonPart(Permission permission) {
231         this.permission = permission;
232     }
233
234     @Override
235     public PermissionsList getCommonPartList() {
236         return permissionsList;
237     }
238
239     @Override
240     public void setCommonPartList(PermissionsList permissionsList) {
241         this.permissionsList = permissionsList;
242     }
243
244     @Override
245     public String getQProperty(
246             String prop) {
247         return null;
248     }
249
250     @Override
251     public DocumentFilter createDocumentFilter() {
252         DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
253         return filter;
254     }
255
256     /**
257      * sanitize removes data not needed to be sent to the consumer
258      * @param permission
259      */
260     private void sanitize(Permission permission) {
261         if (!SecurityUtils.isCSpaceAdmin()) {
262             permission.setTenantId(null);
263         }
264     }
265
266     private void setTenant(Permission permission) {
267         //set tenant only if not available from input
268         if (permission.getTenantId() == null || permission.getTenantId().isEmpty()) {
269             permission.setTenantId(getServiceContext().getTenantId());
270         }
271     }
272 }