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.perms.Permission;
27 import org.collectionspace.services.authorization.perms.PermissionsList;
28 import org.collectionspace.services.authorization.storage.AuthorizationDelegate;
29 import org.collectionspace.services.client.PayloadOutputPart;
30 import org.collectionspace.services.client.PermissionClient;
31 import org.collectionspace.services.common.SecurityResourceBase;
32 import org.collectionspace.services.common.ServiceMessages;
33 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.context.ServiceContextFactory;
36 import org.collectionspace.services.common.storage.StorageClient;
37 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
38 import org.jboss.resteasy.util.HttpResponseCodes;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import javax.ws.rs.Consumes;
43 import javax.ws.rs.DELETE;
44 import javax.ws.rs.GET;
45 import javax.ws.rs.POST;
46 import javax.ws.rs.PUT;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.Produces;
50 import javax.ws.rs.QueryParam;
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(PermissionClient.SERVICE_PATH)
57 @Consumes("application/xml")
58 @Produces("application/xml")
59 public class PermissionResource extends SecurityResourceBase {
61 final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
62 final StorageClient storageClient = new JpaStorageClientImpl();
65 protected String getVersionString() {
66 return "$LastChangedRevision: 1165 $";
70 public String getServiceName() {
71 return PermissionClient.SERVICE_NAME;
75 public Class<Permission> getCommonPartClass() {
76 return Permission.class;
80 public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
81 return RemoteServiceContextFactory.get();
85 public StorageClient getStorageClient(ServiceContext ctx) {
86 //FIXME use ctx to identify storage client
91 public Response createPermission(Permission input) {
97 public Permission getPermission(@PathParam("csid") String csid) {
98 return (Permission)get(csid, Permission.class);
102 @Produces("application/xml")
103 public PermissionsList getPermissionList(@Context UriInfo ui) {
104 PermissionsList result = (PermissionsList)getList(ui, Permission.class);
105 PayloadOutputPart ppo = new PayloadOutputPart(PermissionsList.class.getName(), result);
106 System.out.println(ppo.asXML());
113 public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
114 return (Permission)update(csid, theUpdate, Permission.class);
119 public Response deletePermission(@PathParam("csid") String csid) {
120 logger.debug("deletePermission with csid=" + csid);
121 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
123 //FIXME ideally the following two ops should be in the same tx CSPACE-658
124 //delete all relationships for this permission
125 PermissionRoleSubResource subResource =
126 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
127 subResource.deletePermissionRole(csid, SubjectType.ROLE);
128 //NOTE for delete permissions in the authz provider
129 //at the PermissionRoleSubResource/DocHandler level, there is no visibility
130 //if permission is deleted, so do it here, however,
131 //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
132 //the ACL might be needed for other ACEs roles...
133 AuthorizationDelegate.deletePermissions(csid);
135 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
136 getStorageClient(ctx).delete(ctx, csid);
137 return Response.status(HttpResponseCodes.SC_OK).build();
138 } catch (Exception e) {
139 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
144 @Path("{csid}/permroles")
145 public Response createPermissionRole(@QueryParam("_method") String method,
146 @PathParam("csid") String permCsid,
147 PermissionRole input) {
148 if (method != null) {
149 if ("delete".equalsIgnoreCase(method)) {
150 return deletePermissionRole(permCsid, input);
153 logger.debug("createPermissionRole with permCsid=" + permCsid);
154 ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
156 PermissionRoleSubResource subResource =
157 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
158 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
159 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
160 path.path(permCsid + "/permroles/" + permrolecsid);
161 Response response = Response.created(path.build()).build();
163 } catch (Exception e) {
164 throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
169 @Path("{csid}/permroles/{id}")
170 public PermissionRoleRel getPermissionRole(
171 @PathParam("csid") String permCsid,
172 @PathParam("id") String permrolecsid) {
173 logger.debug("getPermissionRole with permCsid=" + permCsid);
174 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
175 PermissionRoleRel result = null;
177 PermissionRoleSubResource subResource =
178 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
179 //get relationships for a permission
180 result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
181 } catch (Exception e) {
182 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
184 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
189 @Path("{csid}/permroles")
190 public PermissionRole getPermissionRole(
191 @PathParam("csid") String permCsid) {
192 logger.debug("getPermissionRole with permCsid=" + permCsid);
193 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
194 PermissionRole result = null;
196 PermissionRoleSubResource subResource =
197 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
198 //get relationships for a permission
199 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
200 } catch (Exception e) {
201 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
203 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
207 public Response deletePermissionRole(String permCsid, PermissionRole input) {
208 logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
209 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
211 PermissionRoleSubResource subResource =
212 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
213 //delete all relationships for a permission
214 subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
215 return Response.status(HttpResponseCodes.SC_OK).build();
216 } catch (Exception e) {
217 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
222 @Path("{csid}/permroles")
223 public Response deletePermissionRole(
224 @PathParam("csid") String permCsid) {
225 logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
226 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
228 PermissionRoleSubResource subResource =
229 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
230 //delete all relationships for a permission
231 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
232 return Response.status(HttpResponseCodes.SC_OK).build();
233 } catch (Exception e) {
234 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);