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