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