]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
13637c6520479bfc52b9b749596fbee224bf5a7e
[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
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.setRoleName(RoleClient.getBackendRoleName(roleReceived.getRoleName(),
108                                 roleFound.getTenantId()));
109                 merge(roleReceived, roleFound);
110         }
111     }
112
113     /**
114      * Merge fields manually from 'from' to the 'to' role
115      * -this method is created due to inefficiency of JPA EM merge
116      * @param from
117      * @param to
118      * @return merged role
119      */
120     private Role merge(Role from, Role to) throws Exception {
121         // A role's name cannot be changed
122         if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
123             String msg = "Role name cannot be changed " + to.getRoleName();
124             logger.error(msg);
125             throw new BadRequestException(msg);
126         }
127         
128         if (from.getDisplayName() != null && !from.getDisplayName().trim().isEmpty() ) {
129                 to.setDisplayName(from.getDisplayName());
130         }
131         if (from.getRoleGroup() != null && !from.getRoleGroup().trim().isEmpty()) {
132             to.setRoleGroup(from.getRoleGroup());
133         }
134         if (from.getDescription() != null && !from.getDescription().trim().isEmpty()) {
135             to.setDescription(from.getDescription());
136         }
137
138         if (logger.isDebugEnabled()) {
139                 org.collectionspace.services.authorization.ObjectFactory objectFactory =
140                         new org.collectionspace.services.authorization.ObjectFactory();
141             logger.debug("Merged role on update=" + JaxbUtils.toString(objectFactory.createRole(to), Role.class));
142         }
143         
144         return to;
145     }
146
147     @Override
148     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
149         Role upAcc = wrapDoc.getWrappedObject();
150         getServiceContext().setOutput(upAcc);
151         sanitize(upAcc);
152     }
153
154     @Override
155     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
156         setCommonPart(extractCommonPart(wrapDoc));
157         sanitize(getCommonPart());
158         getServiceContext().setOutput(role);
159     }
160
161     @Override
162     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
163         RolesList rolesList = extractCommonPartList(wrapDoc);
164         setCommonPartList(rolesList);
165         getServiceContext().setOutput(getCommonPartList());
166     }
167
168     @Override
169     public Role extractCommonPart(
170             DocumentWrapper<Role> wrapDoc)
171             throws Exception {
172         return wrapDoc.getWrappedObject();
173     }
174
175     @Override
176     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
177             throws Exception {
178         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
179     }
180
181     @Override
182     public RolesList extractCommonPartList(
183             DocumentWrapper<List> wrapDoc)
184             throws Exception {
185
186         RolesList rolesList = new RolesList();
187         List<Role> list = new ArrayList<Role>();
188         rolesList.setRole(list);
189         for (Object obj : wrapDoc.getWrappedObject()) {
190             Role role = (Role) obj;
191             sanitize(role);
192             list.add(role);
193         }
194         return rolesList;
195     }
196
197     @Override
198     public Role getCommonPart() {
199         return role;
200     }
201
202     @Override
203     public void setCommonPart(Role role) {
204         this.role = role;
205     }
206
207     @Override
208     public RolesList getCommonPartList() {
209         return rolesList;
210     }
211
212     @Override
213     public void setCommonPartList(RolesList rolesList) {
214         this.rolesList = rolesList;
215     }
216
217     @Override
218     public String getQProperty(
219             String prop) {
220         return null;
221     }
222
223     @Override
224     public DocumentFilter createDocumentFilter() {
225         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
226         return filter;
227     }
228
229     /**
230      * sanitize removes data not needed to be sent to the consumer
231      * @param roleFound
232      */
233     private void sanitize(Role role) {
234         if (!SecurityUtils.isCSpaceAdmin()) {
235             role.setTenantId(null); // REM - See no reason for hiding the tenant ID?
236         }
237     }
238
239     private void setTenant(Role role) {
240         //set tenant only if not available from input
241         if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
242             role.setTenantId(getServiceContext().getTenantId());
243         }
244     }
245 }