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