]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2a3806f14c65552e0b948b6b15e98c335b2b27d7
[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             //using r not rv ensures we're getting the "ROLE" prefix/qualified name
91             // This needs to use the qualified name, not the display name
92             String[] roles = {r.getRoleName()};
93             for (PermissionValue pv : pr.getPermissions()) {
94                 Permission p = getPermission(pv.getPermissionId());
95                 if (p == null) {
96                     String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
97                     logger.error(msg);
98                     //TODO: would be nice contiue to still send 400 back
99                     continue;
100                 }
101                 CSpaceResource[] resources = getResources(p);
102                 for (CSpaceResource res : resources) {
103                     boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
104                     authz.addPermissions(res, roles, grant);
105                 }
106             }
107         }
108     }
109
110     /**
111      * deletePermissions delete all permissions associated with given permission role
112      * @param ctx
113      * @param pr permissionrole
114      * @throws Exception
115      */
116     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
117             throws Exception {
118         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
119         AuthZ authz = AuthZ.get();
120         if (subject.equals(SubjectType.ROLE)) {
121             PermissionValue pv = pr.getPermissions().get(0);
122             Permission p = getPermission(pv.getPermissionId());
123             if (p == null) {
124                 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
125                 logger.error(msg);
126                 throw new DocumentNotFoundException(msg);
127             }
128             CSpaceResource[] resources = getResources(p);
129             String[] roles = getRoles(pr.getRoles());
130             for (CSpaceResource res : resources) {
131                 authz.deletePermissions(res, roles);
132             }
133         } else if (SubjectType.PERMISSION.equals(subject)) {
134             RoleValue rv = pr.getRoles().get(0);
135             Role r = getRole(rv.getRoleId());
136             if (r == null) {
137                 String msg = "deletePermissions: No role found for id=" + rv.getRoleId();
138                 logger.error(msg);
139                 throw new DocumentNotFoundException(msg);
140             }
141             //using r not rv ensures we're getting the "ROLE" prefix/qualified name
142             // This needs to use the qualified name, not the display name
143             String[] roles = {r.getRoleName()}; 
144             for (PermissionValue pv : pr.getPermissions()) {
145                 Permission p = getPermission(pv.getPermissionId());
146                 if (p == null) {
147                     String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
148                     logger.error(msg);
149                     //TODO: would be nice contiue to still send 400 back
150                     continue;
151                 }
152                 CSpaceResource[] resources = getResources(p);
153                 for (CSpaceResource res : resources) {
154                     authz.deletePermissions(res, roles);
155                 }
156             }
157         }
158     }
159
160     /**
161      * deletePermissions delete permissions associated with given permission id
162      * @param permCsid
163      * @throws Exception
164      */
165     //Non-javadoc comment : this is a very dangerous operation as it deletes
166     //the Spring ACL instead of ACE(s) that is associated with each role
167     //the ACL might be needed for other ACEs (including those for ROLE_ADMINISTRATOR,
168     //ROLE_TENANT_ADMINISTRATOR, etc.)...
169     static public void deletePermissions(String permCsid) throws Exception {
170         Permission p = getPermission(permCsid);
171         if (p == null) {
172             String msg = "deletePermissions: No permission found for id=" + permCsid;
173             logger.error(msg);
174             throw new DocumentNotFoundException(msg);
175         }
176         CSpaceResource[] resources = getResources(p);
177         AuthZ authz = AuthZ.get();
178
179         for (CSpaceResource res : resources) {
180             try {
181                 authz.deletePermissions(res);
182             } catch (PermissionException pe) {
183                 //perms are created downthere only if roles are related to the permissions
184                 logger.info("no permissions found in authz service provider for "
185                         + "permCsid=" + permCsid + " res=" + res.getId());
186             }
187         }
188     }
189
190     /**
191      * getRoles get roles (string) array from given RoleValue list
192      * @param rvl rolevalue list
193      * @return string array with role names
194      * @see RoleValue
195      */
196     private static String[] getRoles(List<RoleValue> rvl)
197                 throws DocumentNotFoundException {
198         List<String> rvls = new ArrayList<String>();
199         for (RoleValue rv : rvl) {
200             Role r = getRole(rv.getRoleId());
201             if (r == null) {
202                 String msg = "getRoles: No role found for id=" + rv.getRoleId();
203                 logger.error(msg);
204                 //TODO: would be nice contiue to still send 400 back
205                 continue;
206             }
207             rvls.add(r.getRoleName());
208         }
209         return rvls.toArray(new String[0]);
210     }
211
212     /**
213      * getResources from given PermissionValue
214      * @param permisison csid
215      * @return array of CSpaceResource
216      * @see PermissionValue
217      * @see CSpaceResource
218      */
219     private static CSpaceResource[] getResources(Permission p) {
220         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
221
222         for (PermissionAction pa : p.getActions()) {
223             CSpaceResource res = null;
224             if (p.getTenantId() == null) {
225                 res = new URIResourceImpl(p.getResourceName(),
226                         getAction(pa.getName()));
227             } else {
228                 res = new URIResourceImpl(p.getTenantId(), p.getResourceName(),
229                         getAction(pa.getName()));
230             }
231             rl.add(res);
232         }
233         return rl.toArray(new CSpaceResource[0]);
234     }
235
236     private static Permission getPermission(String permCsid)
237                 throws DocumentNotFoundException {
238         Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
239                 Permission.class);
240         return p;
241     }
242
243     private static Role getRole(String roleCsid)
244                 throws DocumentNotFoundException {
245         Role r = (Role) JpaStorageUtils.getEntity(roleCsid,
246                 Role.class);
247         return r;
248     }
249
250     /**
251      * getAction is a convenience method to get corresponding action for
252      * given ActionType
253      * @param action
254      * @return
255      */
256     public static CSpaceAction getAction(ActionType action) {
257         if (ActionType.CREATE.equals(action)) {
258             return CSpaceAction.CREATE;
259         } else if (ActionType.READ.equals(action)) {
260             return CSpaceAction.READ;
261         } else if (ActionType.UPDATE.equals(action)) {
262             return CSpaceAction.UPDATE;
263         } else if (ActionType.DELETE.equals(action)) {
264             return CSpaceAction.DELETE;
265         } else if (ActionType.SEARCH.equals(action)) {
266             return CSpaceAction.SEARCH;
267         } else if (ActionType.ADMIN.equals(action)) {
268             return CSpaceAction.ADMIN;
269         } else if (ActionType.START.equals(action)) {
270             return CSpaceAction.START;
271         } else if (ActionType.STOP.equals(action)) {
272             return CSpaceAction.STOP;
273         }
274         throw new IllegalArgumentException("action = " + action.toString());
275     }
276 }