]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a4d7deabb1dd93cfcd4180b4cd490ef90f947814
[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.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionException;
35 import org.collectionspace.services.authorization.PermissionNotFoundException;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionValue;
38 import org.collectionspace.services.authorization.RoleValue;
39 import org.collectionspace.services.authorization.SubjectType;
40 import org.collectionspace.services.authorization.URIResourceImpl;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * AuthorizationDelegate delegates permissions management to the underlying authorization
48  * service from the RESTful service layer. The authorization service for example
49  * might manage permissions with the help of a provider (e.g. Spring Security ACL)
50  * @author
51  */
52 public class AuthorizationDelegate {
53
54     private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
55
56     /**
57      * addPermissions add permissions represented given PermissionRole
58      * @param ctx
59      * @param pr permission role
60      * @throws Exception
61      * @see PermissionRole
62      */
63     static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
64         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
65         AuthZ authz = AuthZ.get();
66         if (subject.equals(SubjectType.ROLE)) {
67             PermissionValue pv = pr.getPermissions().get(0);
68             CSpaceResource[] resources = getResources(pv.getPermissionId());
69             String[] roles = getRoles(pr.getRoles());
70             for (CSpaceResource res : resources) {
71                 authz.addPermissions(res, roles);
72             }
73         } else if (SubjectType.PERMISSION.equals(subject)) {
74             RoleValue rv = pr.getRoles().get(0);
75             String[] roles = {rv.getRoleName()};
76             for (PermissionValue pv : pr.getPermissions()) {
77                 CSpaceResource[] resources = getResources(pv.getPermissionId());
78                 for (CSpaceResource res : resources) {
79                     authz.addPermissions(res, roles);
80                 }
81             }
82         }
83     }
84
85     /**
86      * deletePermissions delete all permissions associated with given permission role
87      * @param ctx
88      * @param pr permissionrole
89      * @throws Exception
90      */
91     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
92             throws Exception {
93         PermissionValue pv = pr.getPermissions().get(0);
94         deletePermissions(pv);
95     }
96
97     /**
98      * deletePermissions delete permissions associated with given PermissionValue
99      * @param pv permission value
100      * @throws Exception
101      * @see PermissionValue
102      */
103     static void deletePermissions(PermissionValue pv)
104             throws Exception {
105         deletePermissions(pv.getPermissionId());
106     }
107
108     /**
109      * deletePermissions delete permissions associated with given permission id
110      * @param permCsid
111      * @throws Exception
112      */
113     static public void deletePermissions(String permCsid) throws Exception {
114         CSpaceResource[] resources = getResources(permCsid);
115         AuthZ authz = AuthZ.get();
116
117         for (CSpaceResource res : resources) {
118             try {
119                 authz.deletePermissions(res);
120             } catch (PermissionException pe) {
121                 //perms are created downthere only if roles are related to the permissions
122                 logger.info("no permissions found in authz service provider for " +
123                         "permCsid=" + permCsid + " res=" + res.getId());
124             }
125         }
126     }
127
128
129
130     /**
131      * getRoles get roles (string) array from given RoleValue list
132      * @param rvl rolevalue list
133      * @return string array with role names
134      * @see RoleValue
135      */
136     private static String[] getRoles(List<RoleValue> rvl) {
137         List<String> rvls = new ArrayList<String>();
138         for (RoleValue rv : rvl) {
139             //assumption: rolename is relationship metadata is mandatory
140             if (rv.getRoleName() != null) {
141                 rvls.add(rv.getRoleName());
142             }
143         }
144         return rvls.toArray(new String[0]);
145     }
146
147     /**
148      * getResources from given PermissionValue
149      * @param permisison csid
150      * @return array of CSpaceResource
151      * @see PermissionValue
152      * @see CSpaceResource
153      */
154     private static CSpaceResource[] getResources(String permCsid) {
155         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
156         Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
157                 Permission.class);
158         if (p != null) {
159             for (PermissionAction pa : p.getActions()) {
160
161                 CSpaceResource res = new URIResourceImpl(p.getResourceName(),
162                         getAction(pa.getName()));
163                 rl.add(res);
164             }
165         }
166         return rl.toArray(new CSpaceResource[0]);
167     }
168
169     /**
170      * getAction is a convenience method to get corresponding action for
171      * given ActionType
172      * @param action
173      * @return
174      */
175     public static CSpaceAction getAction(ActionType action) {
176         if (ActionType.CREATE.equals(action)) {
177             return CSpaceAction.CREATE;
178         } else if (ActionType.READ.equals(action)) {
179             return CSpaceAction.READ;
180         } else if (ActionType.UPDATE.equals(action)) {
181             return CSpaceAction.UPDATE;
182         } else if (ActionType.DELETE.equals(action)) {
183             return CSpaceAction.DELETE;
184         } else if (ActionType.SEARCH.equals(action)) {
185             return CSpaceAction.SEARCH;
186         } else if (ActionType.ADMIN.equals(action)) {
187             return CSpaceAction.ADMIN;
188         } else if (ActionType.START.equals(action)) {
189             return CSpaceAction.START;
190         } else if (ActionType.STOP.equals(action)) {
191             return CSpaceAction.STOP;
192         }
193         throw new IllegalArgumentException("action = " + action.toString());
194     }
195 }