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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization;
26 import org.collectionspace.services.account.AccountRoleSubResource;
27 import org.collectionspace.services.client.RoleClient;
28 import org.collectionspace.services.common.SecurityResourceBase;
29 import org.collectionspace.services.common.ServiceMessages;
30 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.context.ServiceContextFactory;
33 import org.collectionspace.services.common.storage.StorageClient;
34 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
35 import org.collectionspace.services.common.CSWebApplicationException;
37 import org.jboss.resteasy.util.HttpResponseCodes;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import javax.ws.rs.Consumes;
42 import javax.ws.rs.DELETE;
43 import javax.ws.rs.GET;
44 import javax.ws.rs.POST;
45 import javax.ws.rs.PUT;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.QueryParam;
50 import javax.ws.rs.WebApplicationException;
51 import javax.ws.rs.core.Context;
52 import javax.ws.rs.core.Response;
53 import javax.ws.rs.core.UriBuilder;
54 import javax.ws.rs.core.UriInfo;
56 @Path(RoleClient.SERVICE_PATH)
57 @Consumes("application/xml")
58 @Produces("application/xml")
59 public class RoleResource extends SecurityResourceBase {
61 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
62 final StorageClient storageClient = new JpaStorageClientImpl();
65 protected String getVersionString() {
66 return "$LastChangedRevision: 1165 $";
70 public String getServiceName() {
71 return RoleClient.SERVICE_NAME;
75 public Class<RoleResource> getCommonPartClass() {
76 return RoleResource.class;
80 public ServiceContextFactory getServiceContextFactory() {
81 return RemoteServiceContextFactory.get();
85 public StorageClient getStorageClient(ServiceContext ctx) {
86 //FIXME use ctx to identify storage client
91 public Response createRole(Role input) {
97 public Role getRole(@PathParam("csid") String csid) {
98 return (Role)get(csid, Role.class);
102 * Get a list of accounts associated with this role.
105 @Path("{csid}/accountroles")
106 public AccountRole getRoleAccounts(
107 @PathParam("csid") String accCsid) {
108 logger.debug("getAccountRole with accCsid=" + accCsid);
109 ensureCSID(accCsid, ServiceMessages.GET_FAILED+ "accountroles role ");
110 AccountRole result = null;
112 AccountRoleSubResource subResource =
113 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
114 //get relationships for a role
115 result = subResource.getAccountRole(accCsid, SubjectType.ACCOUNT);
116 } catch (Exception e) {
117 throw bigReThrow(e, ServiceMessages.GET_FAILED, accCsid);
119 checkResult(result, accCsid, ServiceMessages.GET_FAILED);
124 @Produces("application/xml")
125 public RolesList getRoleList(@Context UriInfo ui) {
126 return (RolesList)getList(ui, Role.class);
131 public Role updateRole(@PathParam("csid") String csid, Role theUpdate) {
133 Role role = (Role)get(csid, Role.class);
134 // If marked as metadata immutable, do not update
135 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
137 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
138 throw new CSWebApplicationException(response);
140 return (Role)update(csid, theUpdate, Role.class);
141 } catch (Exception e) {
142 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);
148 public Response deleteRole(@PathParam("csid") String csid) {
149 logger.debug("deleteRole with csid=" + csid);
150 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "deleteRole ");
153 Role role = (Role)get(csid, Role.class);
154 // If marked as metadata immutable, do not delete
155 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
157 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
160 //FIXME ideally the following three operations should be in the same tx CSPACE-658
161 //delete all relationships for this permission
162 PermissionRoleSubResource permRoleResource =
163 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
164 permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
165 //delete all the account/role relationships associate with this role
166 AccountRoleSubResource accountRoleResource =
167 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
168 accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
169 //finally, delete the role itself
170 ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
171 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
172 return Response.status(HttpResponseCodes.SC_OK).build();
173 } catch (Exception e) {
174 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
179 @Path("{csid}/permroles")
180 public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
181 PermissionRole input) {
182 if (method != null) {
183 if ("delete".equalsIgnoreCase(method)) {
184 return deleteRolePermission(roleCsid, input);
187 logger.debug("createRolePermission with roleCsid=" + roleCsid);
188 ensureCSID(roleCsid, ServiceMessages.PUT_FAILED + "permroles role ");
190 Role role = (Role)get(roleCsid, Role.class);
191 // If marked as metadata immutable, do not delete
192 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
194 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
197 PermissionRoleSubResource subResource =
198 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
199 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
200 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
201 path.path(roleCsid + "/permroles/" + permrolecsid);
202 Response response = Response.created(path.build()).build();
204 } catch (Exception e) {
205 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
210 @Path("{csid}/permroles")
211 public PermissionRole getRolePermission(
212 @PathParam("csid") String roleCsid) {
213 logger.debug("getRolePermission with roleCsid=" + roleCsid);
214 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
215 PermissionRole result = null;
217 PermissionRoleSubResource subResource =
218 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
219 //get relationships for a role
220 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
221 } catch (Exception e) {
222 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
224 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
229 @Path("{csid}/permroles/{id}")
230 public PermissionRoleRel getRolePermission(
231 @PathParam("csid") String roleCsid,
232 @PathParam("id") String permrolecsid) {
233 logger.debug("getRolePermission with roleCsid=" + roleCsid);
234 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
235 PermissionRoleRel result = null;
237 PermissionRoleSubResource subResource =
238 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
239 //get relationships for a role
240 result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
241 } catch (Exception e) {
242 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
244 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
248 public Response deleteRolePermission(String roleCsid, PermissionRole input) {
249 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
250 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
252 Role role = (Role)get(roleCsid, Role.class);
253 // If marked as metadata immutable, do not delete
254 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
256 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
259 PermissionRoleSubResource subResource =
260 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
261 //delete all relationships for a permission
262 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
263 return Response.status(HttpResponseCodes.SC_OK).build();
264 } catch (Exception e) {
265 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
270 @Path("{csid}/permroles")
271 public Response deleteRolePermission(
272 @PathParam("csid") String roleCsid) {
273 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
274 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
276 Role role = (Role)get(roleCsid, Role.class);
277 // If marked as metadata immutable, do not delete
278 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
280 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
283 PermissionRoleSubResource subResource =
284 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
285 //delete all relationships for a permission
286 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
287 return Response.status(HttpResponseCodes.SC_OK).build();
288 } catch (Exception e) {
289 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);