]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5526957ce290a8626400c9b30570a40784a9d7fd
[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.Role;
31 import org.collectionspace.services.authorization.RolesList;
32
33 import org.collectionspace.services.client.RoleClient;
34 import org.collectionspace.services.common.document.BadRequestException;
35 import org.collectionspace.services.common.document.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.document.JaxbUtils;
38 import org.collectionspace.services.common.security.SecurityUtils;
39 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Document handler for Role
46  * @author 
47  */
48 public class RoleDocumentHandler
49                 extends JpaDocumentHandler<Role, RolesList, Role, List> {
50     private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
51     private Role role;
52     private RolesList rolesList;
53
54     @Override
55     public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
56         String id = UUID.randomUUID().toString();
57         Role role = wrapDoc.getWrappedObject();
58         
59         // Synthesize the display name if it was not passed in.
60         String displayName = role.getDisplayName();
61         boolean displayNameEmpty = true;
62         if (displayName != null) {
63                 displayNameEmpty = displayName.trim().isEmpty();        
64         }
65         if (displayNameEmpty == true) {
66                 role.setDisplayName(role.getRoleName());
67         }
68         
69         setTenant(role);
70         role.setRoleName(fixRoleName(role.getRoleName(),
71                         role.getTenantId()));
72         role.setCsid(id);
73         // We do not allow creation of locked roles through the services.
74         role.setMetadataProtection(null);
75         role.setPermsProtection(null);
76     }
77
78     @Override
79     public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
80         Role roleFound = wrapDoc.getWrappedObject();
81         Role roleReceived = getCommonPart();
82         // If marked as metadata immutable, do not do update
83         if(!RoleClient.IMMUTABLE.equals(roleFound.getMetadataProtection())) {
84                 roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName(),
85                                 roleFound.getTenantId()));
86                 merge(roleReceived, roleFound);
87         }
88     }
89
90     /**
91      * merge manually merges the from from to the to role
92      * -this method is created due to inefficiency of JPA EM merge
93      * @param from
94      * @param to
95      * @return merged role
96      */
97     private Role merge(Role from, Role to) throws Exception {
98         //role name cannot be changed
99         if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
100             String msg = "Role name cannot be changed " + to.getRoleName();
101             logger.error(msg);
102             throw new BadRequestException(msg);
103         }
104         if (from.getDisplayName() != null) {
105                 to.setDisplayName(from.getDisplayName());
106         }
107         if (from.getRoleGroup() != null) {
108             to.setRoleGroup(from.getRoleGroup());
109         }
110         if (from.getDescription() != null) {
111             to.setDescription(from.getDescription());
112         }
113         // Note that we do not allow update of locks
114         if (logger.isDebugEnabled()) {
115                 org.collectionspace.services.authorization.ObjectFactory objectFactory =
116                         new org.collectionspace.services.authorization.ObjectFactory();
117             logger.debug("merged role=" + JaxbUtils.toString(objectFactory.createRole(to) ,Role.class));
118         }
119         return to;
120     }
121
122     @Override
123     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
124         Role upAcc = wrapDoc.getWrappedObject();
125         getServiceContext().setOutput(upAcc);
126         sanitize(upAcc);
127     }
128
129     @Override
130     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
131         setCommonPart(extractCommonPart(wrapDoc));
132         sanitize(getCommonPart());
133         getServiceContext().setOutput(role);
134     }
135
136     @Override
137     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
138         RolesList rolesList = extractCommonPartList(wrapDoc);
139         setCommonPartList(rolesList);
140         getServiceContext().setOutput(getCommonPartList());
141     }
142
143     @Override
144     public Role extractCommonPart(
145             DocumentWrapper<Role> wrapDoc)
146             throws Exception {
147         return wrapDoc.getWrappedObject();
148     }
149
150     @Override
151     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
152             throws Exception {
153         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
154     }
155
156     @Override
157     public RolesList extractCommonPartList(
158             DocumentWrapper<List> wrapDoc)
159             throws Exception {
160
161         RolesList rolesList = new RolesList();
162         List<Role> list = new ArrayList<Role>();
163         rolesList.setRole(list);
164         for (Object obj : wrapDoc.getWrappedObject()) {
165             Role role = (Role) obj;
166             sanitize(role);
167             list.add(role);
168         }
169         return rolesList;
170     }
171
172     @Override
173     public Role getCommonPart() {
174         return role;
175     }
176
177     @Override
178     public void setCommonPart(Role role) {
179         this.role = role;
180     }
181
182     @Override
183     public RolesList getCommonPartList() {
184         return rolesList;
185     }
186
187     @Override
188     public void setCommonPartList(RolesList rolesList) {
189         this.rolesList = rolesList;
190     }
191
192     @Override
193     public String getQProperty(
194             String prop) {
195         return null;
196     }
197
198     @Override
199     public DocumentFilter createDocumentFilter() {
200         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
201         return filter;
202     }
203
204     /**
205      * sanitize removes data not needed to be sent to the consumer
206      * @param roleFound
207      */
208     private void sanitize(Role role) {
209         if (!SecurityUtils.isCSpaceAdmin()) {
210             role.setTenantId(null);
211         }
212     }
213
214     private String fixRoleName(String role, String tenantId) {
215         String roleName = role.toUpperCase();
216         String rolePrefix = "ROLE_" + tenantId + "_";
217         if (!roleName.startsWith(rolePrefix)) {
218             roleName = rolePrefix + roleName;
219         }
220         return roleName;
221     }
222
223     private void setTenant(Role role) {
224         //set tenant only if not available from input
225         if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
226             role.setTenantId(getServiceContext().getTenantId());
227         }
228     }
229 }