]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
87519d4363d3971f64edfc7de479dffbe3a2ad55
[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 2010 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 org.collectionspace.services.authorization.ActionType;
29 import org.collectionspace.services.authorization.AuthZ;
30 import org.collectionspace.services.authorization.CSpaceAction;
31 import org.collectionspace.services.authorization.CSpaceResource;
32 import org.collectionspace.services.authorization.EffectType;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionException;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionValue;
38 import org.collectionspace.services.authorization.Role;
39 import org.collectionspace.services.authorization.RoleValue;
40 import org.collectionspace.services.authorization.SubjectType;
41 import org.collectionspace.services.authorization.URIResourceImpl;
42 import org.collectionspace.services.common.context.ServiceContext;
43 import org.collectionspace.services.common.document.DocumentNotFoundException;
44 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * AuthorizationDelegate delegates permissions management to the underlying authorization
50  * service from the RESTful service layer. The authorization service for example
51  * might manage permissions with the help of a provider (e.g. Spring Security ACL)
52  * @author
53  */
54 public class AuthorizationDelegate {
55
56     private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
57
58     /**
59      * addPermissions add permissions represented given PermissionRole
60      * @param ctx
61      * @param pr permission role
62      * @throws Exception
63      * @see PermissionRole
64      */
65     static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
66         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
67         AuthZ authz = AuthZ.get();
68         if (subject.equals(SubjectType.ROLE)) {
69             PermissionValue pv = pr.getPermissions().get(0);
70             Permission p = getPermission(pv.getPermissionId());
71             if (p == null) {
72                 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
73                 logger.error(msg);
74                 throw new DocumentNotFoundException(msg);
75             }
76             CSpaceResource[] resources = getResources(p);
77             String[] roles = getRoles(pr.getRoles());
78             for (CSpaceResource res : resources) {
79                 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
80                 authz.addPermissions(res, roles, grant);
81             }
82         } else if (SubjectType.PERMISSION.equals(subject)) {
83             RoleValue rv = pr.getRoles().get(0);
84             Role r = getRole(rv.getRoleId());
85             if (r == null) {
86                 String msg = "addPermissions: No role found for id=" + rv.getRoleId();
87                 logger.error(msg);
88                 throw new DocumentNotFoundException(msg);
89             }
90             String[] roles = {rv.getRoleName()};
91             for (PermissionValue pv : pr.getPermissions()) {
92                 Permission p = getPermission(pv.getPermissionId());
93                 if (p == null) {
94                     String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
95                     logger.error(msg);
96                     //TODO: would be nice contiue to still send 400 back
97                     continue;
98                 }
99                 CSpaceResource[] resources = getResources(p);
100                 for (CSpaceResource res : resources) {
101                     boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
102                     authz.addPermissions(res, roles, grant);
103                 }
104             }
105         }
106     }
107
108     /**
109      * deletePermissions delete all permissions associated with given permission role
110      * @param ctx
111      * @param pr permissionrole
112      * @throws Exception
113      */
114     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
115             throws Exception {
116         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
117         AuthZ authz = AuthZ.get();
118         if (subject.equals(SubjectType.ROLE)) {
119             PermissionValue pv = pr.getPermissions().get(0);
120             Permission p = getPermission(pv.getPermissionId());
121             if (p == null) {
122                 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
123                 logger.error(msg);
124                 throw new DocumentNotFoundException(msg);
125             }
126             CSpaceResource[] resources = getResources(p);
127             String[] roles = getRoles(pr.getRoles());
128             for (CSpaceResource res : resources) {
129                 authz.deletePermissions(res, roles);
130             }
131         } else if (SubjectType.PERMISSION.equals(subject)) {
132             RoleValue rv = pr.getRoles().get(0);
133             Role r = getRole(rv.getRoleId());
134             if (r == null) {
135                 String msg = "deletePermissions: No role found for id=" + rv.getRoleId();
136                 logger.error(msg);
137                 throw new DocumentNotFoundException(msg);
138             }
139             String[] roles = {rv.getRoleName()};
140             for (PermissionValue pv : pr.getPermissions()) {
141                 Permission p = getPermission(pv.getPermissionId());
142                 if (p == null) {
143                     String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
144                     logger.error(msg);
145                     //TODO: would be nice contiue to still send 400 back
146                     continue;
147                 }
148                 CSpaceResource[] resources = getResources(p);
149                 for (CSpaceResource res : resources) {
150                     authz.deletePermissions(res, roles);
151                 }
152             }
153         }
154     }
155
156     /**
157      * deletePermissions delete permissions associated with given permission id
158      * @param permCsid
159      * @throws Exception
160      */
161     //Non-javadoc comment : this is a very dangerous operation as it deletes
162     //the Spring ACL instead of ACE(s) that is associated with each role
163     //the ACL might be needed for other ACEs (including those for ROLE_ADMINISTRATOR,
164     //ROLE_TENANT_ADMINISTRATOR, etc.)...
165     static public void deletePermissions(String permCsid) throws Exception {
166         Permission p = getPermission(permCsid);
167         if (p == null) {
168             String msg = "deletePermissions: No permission found for id=" + permCsid;
169             logger.error(msg);
170             throw new DocumentNotFoundException(msg);
171         }
172         CSpaceResource[] resources = getResources(p);
173         AuthZ authz = AuthZ.get();
174
175         for (CSpaceResource res : resources) {
176             try {
177                 authz.deletePermissions(res);
178             } catch (PermissionException pe) {
179                 //perms are created downthere only if roles are related to the permissions
180                 logger.info("no permissions found in authz service provider for "
181                         + "permCsid=" + permCsid + " res=" + res.getId());
182             }
183         }
184     }
185
186     /**
187      * getRoles get roles (string) array from given RoleValue list
188      * @param rvl rolevalue list
189      * @return string array with role names
190      * @see RoleValue
191      */
192     private static String[] getRoles(List<RoleValue> rvl)
193                 throws DocumentNotFoundException {
194         List<String> rvls = new ArrayList<String>();
195         for (RoleValue rv : rvl) {
196             Role r = getRole(rv.getRoleId());
197             if (r == null) {
198                 String msg = "getRoles: No role found for id=" + rv.getRoleId();
199                 logger.error(msg);
200                 //TODO: would be nice contiue to still send 400 back
201                 continue;
202             }
203             rvls.add(r.getRoleName());
204         }
205         return rvls.toArray(new String[0]);
206     }
207
208     /**
209      * getResources from given PermissionValue
210      * @param permisison csid
211      * @return array of CSpaceResource
212      * @see PermissionValue
213      * @see CSpaceResource
214      */
215     private static CSpaceResource[] getResources(Permission p) {
216         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
217
218         for (PermissionAction pa : p.getActions()) {
219             CSpaceResource res = null;
220             if (p.getTenantId() == null) {
221                 res = new URIResourceImpl(p.getResourceName(),
222                         getAction(pa.getName()));
223             } else {
224                 res = new URIResourceImpl(p.getTenantId(), p.getResourceName(),
225                         getAction(pa.getName()));
226             }
227             rl.add(res);
228         }
229         return rl.toArray(new CSpaceResource[0]);
230     }
231
232     private static Permission getPermission(String permCsid)
233                 throws DocumentNotFoundException {
234         Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
235                 Permission.class);
236         return p;
237     }
238
239     private static Role getRole(String roleCsid)
240                 throws DocumentNotFoundException {
241         Role r = (Role) JpaStorageUtils.getEntity(roleCsid,
242                 Role.class);
243         return r;
244     }
245
246     /**
247      * getAction is a convenience method to get corresponding action for
248      * given ActionType
249      * @param action
250      * @return
251      */
252     public static CSpaceAction getAction(ActionType action) {
253         if (ActionType.CREATE.equals(action)) {
254             return CSpaceAction.CREATE;
255         } else if (ActionType.READ.equals(action)) {
256             return CSpaceAction.READ;
257         } else if (ActionType.UPDATE.equals(action)) {
258             return CSpaceAction.UPDATE;
259         } else if (ActionType.DELETE.equals(action)) {
260             return CSpaceAction.DELETE;
261         } else if (ActionType.SEARCH.equals(action)) {
262             return CSpaceAction.SEARCH;
263         } else if (ActionType.ADMIN.equals(action)) {
264             return CSpaceAction.ADMIN;
265         } else if (ActionType.START.equals(action)) {
266             return CSpaceAction.START;
267         } else if (ActionType.STOP.equals(action)) {
268             return CSpaceAction.STOP;
269         }
270         throw new IllegalArgumentException("action = " + action.toString());
271     }
272 }