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 if(logger.isTraceEnabled()) {
106 PayloadOutputPart ppo = new PayloadOutputPart(PermissionsList.class.getName(), result);
107 System.out.println(ppo.asXML());
115 public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
116 return (Permission)update(csid, theUpdate, Permission.class);
121 public Response deletePermission(@PathParam("csid") String csid) {
122 logger.debug("deletePermission with csid=" + csid);
123 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
125 //FIXME ideally the following two ops should be in the same tx CSPACE-658
126 //delete all relationships for this permission
127 PermissionRoleSubResource subResource =
128 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
129 subResource.deletePermissionRole(csid, SubjectType.ROLE);
130 //NOTE for delete permissions in the authz provider
131 //at the PermissionRoleSubResource/DocHandler level, there is no visibility
132 //if permission is deleted, so do it here, however,
133 //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
134 //the ACL might be needed for other ACEs roles...
135 AuthorizationDelegate.deletePermissions(csid);
137 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
138 getStorageClient(ctx).delete(ctx, csid);
139 return Response.status(HttpResponseCodes.SC_OK).build();
140 } catch (Exception e) {
141 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
146 @Path("{csid}/permroles")
147 public Response createPermissionRole(@QueryParam("_method") String method,
148 @PathParam("csid") String permCsid,
149 PermissionRole input) {
150 if (method != null) {
151 if ("delete".equalsIgnoreCase(method)) {
152 return deletePermissionRole(permCsid, input);
155 logger.debug("createPermissionRole with permCsid=" + permCsid);
156 ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
158 PermissionRoleSubResource subResource =
159 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
160 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
161 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
162 path.path(permCsid + "/permroles/" + permrolecsid);
163 Response response = Response.created(path.build()).build();
165 } catch (Exception e) {
166 throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
171 @Path("{csid}/permroles/{id}")
172 public PermissionRoleRel getPermissionRole(
173 @PathParam("csid") String permCsid,
174 @PathParam("id") String permrolecsid) {
175 logger.debug("getPermissionRole with permCsid=" + permCsid);
176 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
177 PermissionRoleRel result = null;
179 PermissionRoleSubResource subResource =
180 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
181 //get relationships for a permission
182 result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
183 } catch (Exception e) {
184 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
186 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
191 @Path("{csid}/permroles")
192 public PermissionRole getPermissionRole(
193 @PathParam("csid") String permCsid) {
194 logger.debug("getPermissionRole with permCsid=" + permCsid);
195 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
196 PermissionRole result = null;
198 PermissionRoleSubResource subResource =
199 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
200 //get relationships for a permission
201 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
202 } catch (Exception e) {
203 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
205 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
209 public Response deletePermissionRole(String permCsid, PermissionRole input) {
210 logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
211 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
213 PermissionRoleSubResource subResource =
214 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
215 //delete all relationships for a permission
216 subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
217 return Response.status(HttpResponseCodes.SC_OK).build();
218 } catch (Exception e) {
219 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
224 @Path("{csid}/permroles")
225 public Response deletePermissionRole(
226 @PathParam("csid") String permCsid) {
227 logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
228 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
230 PermissionRoleSubResource subResource =
231 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
232 //delete all relationships for a permission
233 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
234 return Response.status(HttpResponseCodes.SC_OK).build();
235 } catch (Exception e) {
236 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);