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.jboss.resteasy.util.HttpResponseCodes;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import javax.ws.rs.Consumes;
40 import javax.ws.rs.DELETE;
41 import javax.ws.rs.GET;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.PUT;
44 import javax.ws.rs.Path;
45 import javax.ws.rs.PathParam;
46 import javax.ws.rs.Produces;
47 import javax.ws.rs.QueryParam;
48 import javax.ws.rs.WebApplicationException;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.Response;
51 import javax.ws.rs.core.UriBuilder;
52 import javax.ws.rs.core.UriInfo;
54 @Path(RoleClient.SERVICE_PATH)
55 @Consumes("application/xml")
56 @Produces("application/xml")
57 public class RoleResource extends SecurityResourceBase {
59 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
60 final StorageClient storageClient = new JpaStorageClientImpl();
63 protected String getVersionString() {
64 return "$LastChangedRevision: 1165 $";
68 public String getServiceName() {
69 return RoleClient.SERVICE_NAME;
73 public Class<RoleResource> getCommonPartClass() {
74 return RoleResource.class;
78 public ServiceContextFactory getServiceContextFactory() {
79 return RemoteServiceContextFactory.get();
83 public StorageClient getStorageClient(ServiceContext ctx) {
84 //FIXME use ctx to identify storage client
89 public Response createRole(Role input) {
95 public Role getRole(@PathParam("csid") String csid) {
96 return (Role)get(csid, Role.class);
100 * Get a list of accounts associated with this role.
103 @Path("{csid}/accountroles")
104 public AccountRole getRoleAccounts(
105 @PathParam("csid") String accCsid) {
106 logger.debug("getAccountRole with accCsid=" + accCsid);
107 ensureCSID(accCsid, ServiceMessages.GET_FAILED+ "accountroles role ");
108 AccountRole result = null;
110 AccountRoleSubResource subResource =
111 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
112 //get relationships for a role
113 result = subResource.getAccountRole(accCsid, SubjectType.ACCOUNT);
114 } catch (Exception e) {
115 throw bigReThrow(e, ServiceMessages.GET_FAILED, accCsid);
117 checkResult(result, accCsid, ServiceMessages.GET_FAILED);
122 @Produces("application/xml")
123 public RolesList getRoleList(@Context UriInfo ui) {
124 return (RolesList)getList(ui, Role.class);
129 public Role updateRole(@PathParam("csid") String csid, Role theUpdate) {
131 Role role = (Role)get(csid, Role.class);
132 // If marked as metadata immutable, do not update
133 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
135 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
136 throw new WebApplicationException(response);
138 return (Role)update(csid, theUpdate, Role.class);
139 } catch (Exception e) {
140 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);
146 public Response deleteRole(@PathParam("csid") String csid) {
147 logger.debug("deleteRole with csid=" + csid);
148 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "deleteRole ");
151 Role role = (Role)get(csid, Role.class);
152 // If marked as metadata immutable, do not delete
153 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
155 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
158 //FIXME ideally the following three operations should be in the same tx CSPACE-658
159 //delete all relationships for this permission
160 PermissionRoleSubResource permRoleResource =
161 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
162 permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
163 //delete all the account/role relationships associate with this role
164 AccountRoleSubResource accountRoleResource =
165 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
166 accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
167 //finally, delete the role itself
168 ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
169 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
170 return Response.status(HttpResponseCodes.SC_OK).build();
171 } catch (Exception e) {
172 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
177 @Path("{csid}/permroles")
178 public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
179 PermissionRole input) {
180 if (method != null) {
181 if ("delete".equalsIgnoreCase(method)) {
182 return deleteRolePermission(roleCsid, input);
185 logger.debug("createRolePermission with roleCsid=" + roleCsid);
186 ensureCSID(roleCsid, ServiceMessages.PUT_FAILED + "permroles role ");
188 Role role = (Role)get(roleCsid, Role.class);
189 // If marked as metadata immutable, do not delete
190 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
192 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
195 PermissionRoleSubResource subResource =
196 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
197 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
198 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
199 path.path(roleCsid + "/permroles/" + permrolecsid);
200 Response response = Response.created(path.build()).build();
202 } catch (Exception e) {
203 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
208 @Path("{csid}/permroles")
209 public PermissionRole getRolePermission(
210 @PathParam("csid") String roleCsid) {
211 logger.debug("getRolePermission with roleCsid=" + roleCsid);
212 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
213 PermissionRole result = null;
215 PermissionRoleSubResource subResource =
216 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
217 //get relationships for a role
218 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
219 } catch (Exception e) {
220 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
222 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
227 @Path("{csid}/permroles/{id}")
228 public PermissionRoleRel getRolePermission(
229 @PathParam("csid") String roleCsid,
230 @PathParam("id") String permrolecsid) {
231 logger.debug("getRolePermission with roleCsid=" + roleCsid);
232 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
233 PermissionRoleRel result = null;
235 PermissionRoleSubResource subResource =
236 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
237 //get relationships for a role
238 result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
239 } catch (Exception e) {
240 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
242 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
246 public Response deleteRolePermission(String roleCsid, PermissionRole input) {
247 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
248 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
250 Role role = (Role)get(roleCsid, Role.class);
251 // If marked as metadata immutable, do not delete
252 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
254 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
257 PermissionRoleSubResource subResource =
258 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
259 //delete all relationships for a permission
260 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
261 return Response.status(HttpResponseCodes.SC_OK).build();
262 } catch (Exception e) {
263 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
268 @Path("{csid}/permroles")
269 public Response deleteRolePermission(
270 @PathParam("csid") String roleCsid) {
271 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
272 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
274 Role role = (Role)get(roleCsid, Role.class);
275 // If marked as metadata immutable, do not delete
276 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
278 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
281 PermissionRoleSubResource subResource =
282 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
283 //delete all relationships for a permission
284 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
285 return Response.status(HttpResponseCodes.SC_OK).build();
286 } catch (Exception e) {
287 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);