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 @Produces("application/xml")
101 public RolesList getRoleList(@Context UriInfo ui) {
102 return (RolesList)getList(ui, Role.class);
107 public Role updateRole(@PathParam("csid") String csid, Role theUpdate) {
109 Role role = (Role)get(csid, Role.class);
110 // If marked as metadata immutable, do not update
111 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
113 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
114 throw new WebApplicationException(response);
116 return (Role)update(csid, theUpdate, Role.class);
117 } catch (Exception e) {
118 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);
124 public Response deleteRole(@PathParam("csid") String csid) {
125 logger.debug("deleteRole with csid=" + csid);
126 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "deleteRole ");
129 Role role = (Role)get(csid, Role.class);
130 // If marked as metadata immutable, do not delete
131 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
133 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
136 //FIXME ideally the following three operations should be in the same tx CSPACE-658
137 //delete all relationships for this permission
138 PermissionRoleSubResource permRoleResource =
139 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
140 permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
141 //delete all the account/role relationships associate with this role
142 AccountRoleSubResource accountRoleResource =
143 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
144 accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
145 //finally, delete the role itself
146 ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
147 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
148 return Response.status(HttpResponseCodes.SC_OK).build();
149 } catch (Exception e) {
150 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
155 @Path("{csid}/permroles")
156 public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
157 PermissionRole input) {
158 if (method != null) {
159 if ("delete".equalsIgnoreCase(method)) {
160 return deleteRolePermission(roleCsid, input);
163 logger.debug("createRolePermission with roleCsid=" + roleCsid);
164 ensureCSID(roleCsid, ServiceMessages.PUT_FAILED + "permroles role ");
166 Role role = (Role)get(roleCsid, Role.class);
167 // If marked as metadata immutable, do not delete
168 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
170 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
173 PermissionRoleSubResource subResource =
174 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
175 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
176 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
177 path.path(roleCsid + "/permroles/" + permrolecsid);
178 Response response = Response.created(path.build()).build();
180 } catch (Exception e) {
181 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
186 @Path("{csid}/permroles")
187 public PermissionRole getRolePermission(
188 @PathParam("csid") String roleCsid) {
189 logger.debug("getRolePermission with roleCsid=" + roleCsid);
190 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
191 PermissionRole result = null;
193 PermissionRoleSubResource subResource =
194 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
195 //get relationships for a role
196 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
197 } catch (Exception e) {
198 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
200 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
205 @Path("{csid}/permroles/{id}")
206 public PermissionRoleRel getRolePermission(
207 @PathParam("csid") String roleCsid,
208 @PathParam("id") String permrolecsid) {
209 logger.debug("getRolePermission with roleCsid=" + roleCsid);
210 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
211 PermissionRoleRel result = null;
213 PermissionRoleSubResource subResource =
214 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
215 //get relationships for a role
216 result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
217 } catch (Exception e) {
218 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
220 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
224 public Response deleteRolePermission(String roleCsid, PermissionRole input) {
225 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
226 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
228 Role role = (Role)get(roleCsid, Role.class);
229 // If marked as metadata immutable, do not delete
230 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
232 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
235 PermissionRoleSubResource subResource =
236 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
237 //delete all relationships for a permission
238 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
239 return Response.status(HttpResponseCodes.SC_OK).build();
240 } catch (Exception e) {
241 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
246 @Path("{csid}/permroles")
247 public Response deleteRolePermission(
248 @PathParam("csid") String roleCsid) {
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);
263 return Response.status(HttpResponseCodes.SC_OK).build();
264 } catch (Exception e) {
265 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);