]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c2a53b686b274fb3ead4685b2c68be45c95c120b
[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.HashMap;
27 import java.util.List;
28
29 import org.collectionspace.services.common.document.DocumentNotFoundException;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.context.ServiceContextProperties;
32 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
33 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
34
35 import org.collectionspace.services.authorization.perms.Permission;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionRoleRel;
38 import org.collectionspace.services.authorization.PermissionValue;
39 import org.collectionspace.services.authorization.Role;
40 import org.collectionspace.services.authorization.RoleValue;
41 import org.collectionspace.services.authorization.SubjectType;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 // TODO: Auto-generated Javadoc
47 /**
48  * The Class PermissionRoleUtil.
49  *
50  * @author
51  */
52 public class PermissionRoleUtil {
53
54     final Logger logger = LoggerFactory.getLogger(PermissionRoleUtil.class);
55
56     /**
57      * Gets the relation subject.
58      *
59      * @param ctx the ctx
60      * @return the relation subject
61      */
62     static SubjectType getRelationSubject(ServiceContext ctx) {
63         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
64         if (o == null) {
65             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
66                     + " property is missing in context "
67                     + ctx.toString());
68         }
69         return (SubjectType) o;
70     }
71
72     /**
73      * Gets the relation subject.
74      *
75      * @param ctx the ctx
76      * @param pr the pr
77      * @return the relation subject
78      */
79     static SubjectType getRelationSubject(ServiceContext ctx, PermissionRole pr) {
80         SubjectType subject = pr.getSubject();
81         if (subject == null) {
82             //it is not required to give subject as URI determines the subject
83             subject = getRelationSubject(ctx);
84         }
85         return subject;
86     }
87
88     /**
89      * buildPermissionRoleRel builds persistent relationship entities from given
90      * permissionrole.
91      *
92      * @param pr permissionrole
93      * @param subject the subject
94      * @param prrl persistent entities built are inserted into this list
95      * @param toDelete the to delete
96      */
97     static public void buildPermissionRoleRel(PermissionRole pr,
98                 SubjectType subject,
99                 List<PermissionRoleRel> prrl,
100                 boolean handleDelete)
101                         throws DocumentNotFoundException {
102         if (subject.equals(SubjectType.ROLE)) {
103                 List<PermissionValue> permissionValues = pr.getPermission();
104                 if (permissionValues != null && permissionValues.size() > 0) {
105                     PermissionValue pv = permissionValues.get(0);
106                     for (RoleValue rv : pr.getRole()) {
107                         PermissionRoleRel prr = buildPermissonRoleRel(pv, rv, subject, handleDelete);
108                         prrl.add(prr);
109                     }
110                 }
111         } else if (subject.equals(SubjectType.PERMISSION)) {
112                 List<RoleValue> roleValues = pr.getRole();
113                 if (roleValues != null && roleValues.size() > 0) {
114                     RoleValue rv = roleValues.get(0);
115                     for (PermissionValue pv : pr.getPermission()) {
116                         PermissionRoleRel prr = buildPermissonRoleRel(pv, rv, subject, handleDelete);
117                         prrl.add(prr);
118                     }
119                 }
120         }
121     }
122
123     /**
124      * Builds a permisson role relationship for either 'create' or 'delete'
125      *
126      * @param pv the pv (currently using only the ID)
127      * @param rv the rv (currently using only the ID)
128      * @param handleDelete the handle delete
129      * @return the permission role rel
130      */
131     static private PermissionRoleRel buildPermissonRoleRel(PermissionValue permissionValue,
132                 RoleValue roleValue,
133                 SubjectType subject,
134                 boolean handleDelete)
135                         throws DocumentNotFoundException {
136
137         PermissionRoleRel result = null;
138         
139         //
140         // Ensure we can find both the Permission and Role to relate.
141         // FIXME: REM - This is a workaround until the Import utility creates Perm/Role relationships
142         // correctly.  The import utility should create and store the permissions and roles BEFORE creating the relationships
143         //
144         PermissionValue pv = permissionValue;
145         try {
146                 Permission permission = (Permission)JpaStorageUtils.getEntity(pv.getPermissionId(),
147                                 Permission.class);
148                 if (permission != null) {
149                         // If the permission already exists, then use it to fill our the relation record
150                         pv = JpaRelationshipStorageClient.createPermissionValue(permission);
151                 }
152         } catch (DocumentNotFoundException e) {
153                 // ignore this exception, pv is set to permissionValue;
154         }
155         //
156         // Ensure we can find both the Permission and Role to relate.
157         // FIXME: REM - This is a workaround until the Import utility creates Perm/Role relationships
158         // correctly.  The import utility should create and store the permissions and roles BEFORE creating the relationships
159         //
160         RoleValue rv = roleValue;
161         try {
162                 Role role = (Role)JpaStorageUtils.getEntity(rv.getRoleId(),
163                                 Role.class);
164                 if (role != null) {
165                         // If the role already exists, then use it to fill out the relation record
166                         rv = JpaRelationshipStorageClient.createRoleValue(role);
167                 }
168         } catch (DocumentNotFoundException e) {
169                 // ignore this exception, rv is set to roleValue
170         }
171         
172         result = new PermissionRoleRel();
173         result.setPermissionId(pv.getPermissionId());
174         result.setPermissionResource(pv.getResourceName());
175         result.setActionGroup(pv.getActionGroup());
176         result.setRoleId(rv.getRoleId());
177         result.setRoleName(rv.getRoleName());
178         //
179         // For 'delete' we need to set the hjid of the existing relstionship
180         //
181         String relationshipId = null;
182         if (subject.equals(SubjectType.ROLE) == true) {
183                 relationshipId = roleValue.getRoleRelationshipId();
184         } else if (subject.equals(SubjectType.PERMISSION) == true) {
185                 relationshipId = permissionValue.getPermRelationshipId();
186         }
187         if (relationshipId != null && handleDelete == true) {
188                 result.setHjid(Long.parseLong(relationshipId));  // set this so we can convince JPA to del the relation
189         }
190         
191         return result;
192     }
193
194     /**
195      * Checks if is invalid tenant.
196      *
197      * @param tenantId the tenant id
198      * @param msgBldr the msg bldr
199      * @return true, if is invalid tenant
200      */
201     static boolean isInvalidTenant(String tenantId, StringBuilder msgBldr) {
202         boolean invalid = false;
203
204         if (tenantId == null || tenantId.isEmpty()) {
205             invalid = true;
206             msgBldr.append("\n tenant : tenantId is missing");
207         }
208         String whereClause = "where id = :id";
209         HashMap<String, Object> params = new HashMap<String, Object>();
210         params.put("id", tenantId);
211
212         Object tenantFound = JpaStorageUtils.getEntity(
213                 "org.collectionspace.services.account.Tenant", whereClause, params);
214         if (tenantFound == null) {
215             invalid = true;
216             msgBldr.append("\n tenant : tenantId=" + tenantId
217                     + " not found");
218         }
219         return invalid;
220     }
221 }