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.core.Context;
51 import javax.ws.rs.core.Response;
52 import javax.ws.rs.core.UriBuilder;
53 import javax.ws.rs.core.UriInfo;
55 @Path(RoleClient.SERVICE_PATH)
56 @Consumes("application/xml")
57 @Produces("application/xml")
58 public class RoleResource extends SecurityResourceBase {
60 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
61 final StorageClient storageClient = new JpaStorageClientImpl();
64 protected String getVersionString() {
65 return "$LastChangedRevision: 1165 $";
69 public String getServiceName() {
70 return RoleClient.SERVICE_NAME;
74 public Class<RoleResource> getCommonPartClass() {
75 return RoleResource.class;
79 public ServiceContextFactory getServiceContextFactory() {
80 return RemoteServiceContextFactory.get();
84 public StorageClient getStorageClient(ServiceContext ctx) {
85 //FIXME use ctx to identify storage client
90 public Response createRole(Role input) {
96 public Role getRole(@PathParam("csid") String csid) {
97 return (Role)get(csid, Role.class);
101 * Get a list of accounts associated with this role.
104 @Path("{csid}/accountroles")
105 public AccountRole getRoleAccounts(
106 @PathParam("csid") String accCsid) {
107 logger.debug("getAccountRole with accCsid=" + accCsid);
108 ensureCSID(accCsid, ServiceMessages.GET_FAILED+ "accountroles role ");
109 AccountRole result = null;
111 AccountRoleSubResource subResource =
112 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
113 //get relationships for a role
114 result = subResource.getAccountRole(accCsid, SubjectType.ACCOUNT);
115 } catch (Exception e) {
116 throw bigReThrow(e, ServiceMessages.GET_FAILED, accCsid);
118 checkResult(result, accCsid, ServiceMessages.GET_FAILED);
123 @Produces("application/xml")
124 public RolesList getRoleList(@Context UriInfo ui) {
125 return (RolesList)getList(ui, Role.class);
130 public Role updateRole(@PathParam("csid") String csid, Role theUpdate) {
132 Role role = (Role)get(csid, Role.class);
133 // If marked as metadata immutable, do not update
134 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
136 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
137 throw new CSWebApplicationException(response);
139 return (Role)update(csid, theUpdate, Role.class);
140 } catch (Exception e) {
141 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);
147 public Response deleteRole(@PathParam("csid") String csid) {
148 logger.debug("deleteRole with csid=" + csid);
149 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "deleteRole ");
152 Role role = (Role)get(csid, Role.class);
153 // If marked as metadata immutable, do not delete
154 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
156 Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
159 //FIXME ideally the following three operations should be in the same tx CSPACE-658
160 //delete all relationships for this permission
161 PermissionRoleSubResource permRoleResource =
162 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
163 permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
164 //delete all the account/role relationships associate with this role
165 AccountRoleSubResource accountRoleResource =
166 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
167 accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
168 //finally, delete the role itself
169 ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
170 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
171 return Response.status(HttpResponseCodes.SC_OK).build();
172 } catch (Exception e) {
173 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
178 @Path("{csid}/permroles")
179 public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
180 PermissionRole input) {
181 if (method != null) {
182 if ("delete".equalsIgnoreCase(method)) {
183 return deleteRolePermission(roleCsid, input);
186 logger.debug("createRolePermission with roleCsid=" + roleCsid);
187 ensureCSID(roleCsid, ServiceMessages.PUT_FAILED + "permroles role ");
189 Role role = (Role)get(roleCsid, Role.class);
190 // If marked as metadata immutable, do not delete
191 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
193 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
196 PermissionRoleSubResource subResource =
197 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
198 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
199 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
200 path.path(roleCsid + "/permroles/" + permrolecsid);
201 Response response = Response.created(path.build()).build();
203 } catch (Exception e) {
204 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
209 @Path("{csid}/permroles")
210 public PermissionRole getRolePermission(
211 @PathParam("csid") String roleCsid) {
212 logger.debug("getRolePermission with roleCsid=" + roleCsid);
213 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
214 PermissionRole result = null;
216 PermissionRoleSubResource subResource =
217 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
218 //get relationships for a role
219 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
220 } catch (Exception e) {
221 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
223 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
228 @Path("{csid}/permroles/{id}")
229 public PermissionRoleRel getRolePermission(
230 @PathParam("csid") String roleCsid,
231 @PathParam("id") String permrolecsid) {
232 logger.debug("getRolePermission with roleCsid=" + roleCsid);
233 ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
234 PermissionRoleRel result = null;
236 PermissionRoleSubResource subResource =
237 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
238 //get relationships for a role
239 result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
240 } catch (Exception e) {
241 throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
243 checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
247 public Response deleteRolePermission(String roleCsid, PermissionRole input) {
248 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
249 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
251 Role role = (Role)get(roleCsid, Role.class);
252 // If marked as metadata immutable, do not delete
253 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
255 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
258 PermissionRoleSubResource subResource =
259 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
260 //delete all relationships for a permission
261 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
262 return Response.status(HttpResponseCodes.SC_OK).build();
263 } catch (Exception e) {
264 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
269 @Path("{csid}/permroles")
270 public Response deleteRolePermission(
271 @PathParam("csid") String roleCsid) {
272 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
273 ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
275 Role role = (Role)get(roleCsid, Role.class);
276 // If marked as metadata immutable, do not delete
277 if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
279 Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
282 PermissionRoleSubResource subResource =
283 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
284 //delete all relationships for a permission
285 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
286 return Response.status(HttpResponseCodes.SC_OK).build();
287 } catch (Exception e) {
288 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);