]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1661b78ff982e544ab7aae258c88a5b719059770
[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.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29
30 import org.collectionspace.services.authorization.PermissionRole;
31 import org.collectionspace.services.authorization.PermissionRoleSubResource;
32 import org.collectionspace.services.authorization.PermissionValue;
33 import org.collectionspace.services.authorization.Role;
34 import org.collectionspace.services.authorization.RoleValue;
35 import org.collectionspace.services.authorization.RolesList;
36 import org.collectionspace.services.authorization.SubjectType;
37
38 import org.collectionspace.services.client.PermissionRoleFactory;
39 import org.collectionspace.services.client.RoleClient;
40 import org.collectionspace.services.client.RoleFactory;
41 import org.collectionspace.services.common.api.Tools;
42 import org.collectionspace.services.common.document.BadRequestException;
43 import org.collectionspace.services.common.document.DocumentFilter;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.document.JaxbUtils;
46 import org.collectionspace.services.common.security.SecurityUtils;
47 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Document handler for Role
54  * @author 
55  */
56 public class RoleDocumentHandler
57                 extends JpaDocumentHandler<Role, RolesList, Role, List> {
58     private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
59     private Role role;
60     private RolesList rolesList;
61
62     @Override
63     public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
64         String id = UUID.randomUUID().toString();
65         Role role = wrapDoc.getWrappedObject();
66         
67         // Synthesize the display name if it was not passed in.
68         String displayName = role.getDisplayName();
69         boolean displayNameEmpty = true;
70         if (displayName != null) {
71                 displayNameEmpty = displayName.trim().isEmpty();        
72         }
73         if (displayNameEmpty == true) {
74                 role.setDisplayName(role.getRoleName());
75         }
76         
77         setTenant(role);
78         role.setRoleName(RoleClient.getBackendRoleName(role.getRoleName(), role.getTenantId()));
79         role.setCsid(id);
80         // We do not allow creation of locked roles through the services.
81         role.setMetadataProtection(null);
82         role.setPermsProtection(null);
83     }
84     
85     @Override
86     public void completeCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
87         Role role = wrapDoc.getWrappedObject();
88         List<PermissionValue> permValueList = role.getPermission();
89         if (permValueList != null && permValueList.size() > 0) {
90                 // create and persist a permrole instance
91                 // The caller of this method needs to ensure a valid and active EM (EntityManager) instance is in the Service context
92                 RoleValue roleValue = RoleFactory.createRoleValueInstance(role);
93                 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(SubjectType.PERMISSION, roleValue,
94                                 permValueList, true, true);
95             PermissionRoleSubResource subResource =
96                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
97             String permrolecsid = subResource.createPermissionRole(permRole, SubjectType.PERMISSION);
98         }
99     }
100
101     @Override
102         public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
103                 Role roleFound = wrapDoc.getWrappedObject();
104                 Role roleReceived = getCommonPart();
105                 // If marked as metadata immutable, do not do update
106                 if (!RoleClient.IMMUTABLE.equals(roleFound.getMetadataProtection())) {
107                         roleReceived
108                                         .setRoleName(RoleClient.getBackendRoleName(roleReceived.getRoleName(), roleFound.getTenantId()));
109                         merge(roleReceived, roleFound);
110                 }
111                 //
112                 // Update perms is supplied.
113                 //
114                 List<PermissionValue> permValueList = roleReceived.getPermission();
115                 if (permValueList != null) {
116             PermissionRoleSubResource subResource =
117                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
118             //
119             // First, delete the existing permroles
120             //
121             subResource.deletePermissionRole(roleFound.getCsid(), SubjectType.PERMISSION);
122             //
123             // Next, create the new permroles
124             //
125                 RoleValue roleValue = RoleFactory.createRoleValueInstance(roleFound);
126                 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(SubjectType.PERMISSION, roleValue,
127                                 permValueList, true, true);            
128             subResource.createPermissionRole(permRole, SubjectType.PERMISSION);
129             //
130             // Finally, set the updated perm list in the result
131             //
132             PermissionRole newPermRole = subResource.getPermissionRole(roleFound.getCsid(), SubjectType.PERMISSION);
133             roleFound.setPermission(newPermRole.getPermission());
134                 }
135         }
136
137     /**
138      * Merge fields manually from 'from' to the 'to' role
139      * -this method is created due to inefficiency of JPA EM merge
140      * @param from
141      * @param to
142      * @return merged role
143      */
144     private Role merge(Role from, Role to) throws Exception {
145         // A role's name cannot be changed
146         if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
147             String msg = "Role name cannot be changed " + to.getRoleName();
148             logger.error(msg);
149             throw new BadRequestException(msg);
150         }
151         
152         if (from.getDisplayName() != null && !from.getDisplayName().trim().isEmpty() ) {
153                 to.setDisplayName(from.getDisplayName());
154         }
155         if (from.getRoleGroup() != null && !from.getRoleGroup().trim().isEmpty()) {
156             to.setRoleGroup(from.getRoleGroup());
157         }
158         if (from.getDescription() != null && !from.getDescription().trim().isEmpty()) {
159             to.setDescription(from.getDescription());
160         }
161
162         if (logger.isDebugEnabled()) {
163                 org.collectionspace.services.authorization.ObjectFactory objectFactory =
164                         new org.collectionspace.services.authorization.ObjectFactory();
165             logger.debug("Merged role on update=" + JaxbUtils.toString(objectFactory.createRole(to), Role.class));
166         }
167         
168         return to;
169     }
170
171     @Override
172     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
173         Role upAcc = wrapDoc.getWrappedObject();
174         getServiceContext().setOutput(upAcc);
175         sanitize(upAcc);
176     }
177
178     @Override
179     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
180         setCommonPart(extractCommonPart(wrapDoc));
181         sanitize(getCommonPart());
182         getServiceContext().setOutput(role);
183     }
184
185     @Override
186     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
187         RolesList rolesList = extractCommonPartList(wrapDoc);
188         setCommonPartList(rolesList);
189         getServiceContext().setOutput(getCommonPartList());
190     }
191
192     @Override
193     public Role extractCommonPart(
194             DocumentWrapper<Role> wrapDoc)
195             throws Exception {
196         Role role = wrapDoc.getWrappedObject();
197         
198         String includePermsQueryParamValue = (String) getServiceContext().getQueryParams().getFirst(RoleClient.INCLUDE_PERMS_QP);
199         boolean includePerms = Tools.isTrue(includePermsQueryParamValue);
200         if (includePerms) {
201                 PermissionRoleSubResource permRoleResource =
202                         new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
203                 PermissionRole permRole = permRoleResource.getPermissionRole(role.getCsid(), SubjectType.PERMISSION);
204                 role.setPermission(permRole.getPermission());
205         }
206     
207         return role;
208     }
209
210     @Override
211     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
212             throws Exception {
213         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
214     }
215
216     @Override
217     public RolesList extractCommonPartList(
218             DocumentWrapper<List> wrapDoc)
219             throws Exception {
220
221         RolesList rolesList = new RolesList();
222         List<Role> list = new ArrayList<Role>();
223         rolesList.setRole(list);
224         for (Object obj : wrapDoc.getWrappedObject()) {
225             Role role = (Role) obj;
226             sanitize(role);
227             list.add(role);
228         }
229         return rolesList;
230     }
231
232     @Override
233     public Role getCommonPart() {
234         return role;
235     }
236
237     @Override
238     public void setCommonPart(Role role) {
239         this.role = role;
240     }
241
242     @Override
243     public RolesList getCommonPartList() {
244         return rolesList;
245     }
246
247     @Override
248     public void setCommonPartList(RolesList rolesList) {
249         this.rolesList = rolesList;
250     }
251
252     @Override
253     public String getQProperty(
254             String prop) {
255         return null;
256     }
257
258     @Override
259     public DocumentFilter createDocumentFilter() {
260         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
261         return filter;
262     }
263
264     /**
265      * sanitize removes data not needed to be sent to the consumer
266      * @param roleFound
267      */
268     private void sanitize(Role role) {
269         if (!SecurityUtils.isCSpaceAdmin()) {
270             // role.setTenantId(null); // REM - There's no reason for hiding the tenant ID is there?
271         }
272     }
273
274     private void setTenant(Role role) {
275         //set tenant only if not available from input
276         if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
277             role.setTenantId(getServiceContext().getTenantId());
278         }
279     }
280 }