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.authorization.storage.AuthorizationDelegate;
27 import org.collectionspace.services.client.PermissionClient;
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.core.Context;
49 import javax.ws.rs.core.Response;
50 import javax.ws.rs.core.UriBuilder;
51 import javax.ws.rs.core.UriInfo;
53 @Path(PermissionClient.SERVICE_PATH)
54 @Consumes("application/xml")
55 @Produces("application/xml")
56 public class PermissionResource extends SecurityResourceBase {
58 final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
59 final StorageClient storageClient = new JpaStorageClientImpl();
62 protected String getVersionString() {
63 return "$LastChangedRevision: 1165 $";
67 public String getServiceName() {
68 return PermissionClient.SERVICE_NAME;
72 public Class<Permission> getCommonPartClass() {
73 return Permission.class;
77 public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
78 return RemoteServiceContextFactory.get();
82 public StorageClient getStorageClient(ServiceContext ctx) {
83 //FIXME use ctx to identify storage client
88 public Response createPermission(Permission input) {
94 public Permission getPermission(@PathParam("csid") String csid) {
95 return (Permission)get(csid, Permission.class);
99 @Produces("application/xml")
100 public PermissionsList getPermissionList(@Context UriInfo ui) {
101 return (PermissionsList)getList(ui, Permission.class);
106 public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
107 return (Permission)update(csid, theUpdate, Permission.class);
112 public Response deletePermission(@PathParam("csid") String csid) {
113 logger.debug("deletePermission with csid=" + csid);
114 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
116 //FIXME ideally the following two ops should be in the same tx CSPACE-658
117 //delete all relationships for this permission
118 PermissionRoleSubResource subResource =
119 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
120 subResource.deletePermissionRole(csid, SubjectType.ROLE);
121 //NOTE for delete permissions in the authz provider
122 //at the PermissionRoleSubResource/DocHandler level, there is no visibility
123 //if permission is deleted, so do it here, however,
124 //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
125 //the ACL might be needed for other ACEs roles...
126 AuthorizationDelegate.deletePermissions(csid);
128 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
129 getStorageClient(ctx).delete(ctx, csid);
130 return Response.status(HttpResponseCodes.SC_OK).build();
131 } catch (Exception e) {
132 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
137 @Path("{csid}/permroles")
138 public Response createPermissionRole(@QueryParam("_method") String method,
139 @PathParam("csid") String permCsid,
140 PermissionRole input) {
141 if (method != null) {
142 if ("delete".equalsIgnoreCase(method)) {
143 return deletePermissionRole(permCsid, input);
146 logger.debug("createPermissionRole with permCsid=" + permCsid);
147 ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
149 PermissionRoleSubResource subResource =
150 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
151 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
152 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
153 path.path(permCsid + "/permroles/" + permrolecsid);
154 Response response = Response.created(path.build()).build();
156 } catch (Exception e) {
157 throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
162 @Path("{csid}/permroles/{id}")
163 public PermissionRoleRel getPermissionRole(
164 @PathParam("csid") String permCsid,
165 @PathParam("id") String permrolecsid) {
166 logger.debug("getPermissionRole with permCsid=" + permCsid);
167 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
168 PermissionRoleRel result = null;
170 PermissionRoleSubResource subResource =
171 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
172 //get relationships for a permission
173 result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
174 } catch (Exception e) {
175 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
177 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
182 @Path("{csid}/permroles")
183 public PermissionRole getPermissionRole(
184 @PathParam("csid") String permCsid) {
185 logger.debug("getPermissionRole with permCsid=" + permCsid);
186 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
187 PermissionRole result = null;
189 PermissionRoleSubResource subResource =
190 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
191 //get relationships for a permission
192 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
193 } catch (Exception e) {
194 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
196 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
200 public Response deletePermissionRole(String permCsid, PermissionRole input) {
201 logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
202 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
204 PermissionRoleSubResource subResource =
205 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
206 //delete all relationships for a permission
207 subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
208 return Response.status(HttpResponseCodes.SC_OK).build();
209 } catch (Exception e) {
210 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
215 @Path("{csid}/permroles")
216 public Response deletePermissionRole(
217 @PathParam("csid") String permCsid) {
218 logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
219 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
221 PermissionRoleSubResource subResource =
222 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
223 //delete all relationships for a permission
224 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
225 return Response.status(HttpResponseCodes.SC_OK).build();
226 } catch (Exception e) {
227 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);