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.FooPermissionsList;
27 import org.collectionspace.services.authorization.perms.Permission;
28 import org.collectionspace.services.authorization.perms.PermissionsList;
29 import org.collectionspace.services.authorization.storage.AuthorizationDelegate;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.common.SecurityResourceBase;
33 import org.collectionspace.services.common.ServiceMessages;
34 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.context.ServiceContextFactory;
37 import org.collectionspace.services.common.storage.StorageClient;
38 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
39 import org.jboss.resteasy.util.HttpResponseCodes;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import javax.ws.rs.Consumes;
44 import javax.ws.rs.DELETE;
45 import javax.ws.rs.GET;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.PUT;
48 import javax.ws.rs.Path;
49 import javax.ws.rs.PathParam;
50 import javax.ws.rs.Produces;
51 import javax.ws.rs.QueryParam;
52 import javax.ws.rs.core.Context;
53 import javax.ws.rs.core.Response;
54 import javax.ws.rs.core.UriBuilder;
55 import javax.ws.rs.core.UriInfo;
57 @Path(PermissionClient.SERVICE_PATH)
58 @Consumes("application/xml")
59 @Produces("application/xml")
60 public class PermissionResource extends SecurityResourceBase {
62 final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
63 final StorageClient storageClient = new JpaStorageClientImpl();
66 protected String getVersionString() {
67 return "$LastChangedRevision: 1165 $";
71 public String getServiceName() {
72 return PermissionClient.SERVICE_NAME;
76 public Class<Permission> getCommonPartClass() {
77 return Permission.class;
81 public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
82 return RemoteServiceContextFactory.get();
86 public StorageClient getStorageClient(ServiceContext ctx) {
87 //FIXME use ctx to identify storage client
92 public Response createPermission(Permission input) {
98 public Permission getPermission(@PathParam("csid") String csid) {
99 return (Permission)get(csid, Permission.class);
103 @Produces("application/xml")
104 public PermissionsList getPermissionList(@Context UriInfo ui) {
105 PermissionsList result = (PermissionsList)getList(ui, Permission.class);
106 PayloadOutputPart ppo = new PayloadOutputPart(PermissionsList.class.getName(), result);
107 System.out.println(ppo.asXML());
114 @Produces("application/xml")
115 public FooPermissionsList getPermissionFooList(@Context UriInfo ui) {
116 FooPermissionsList result = new FooPermissionsList();
122 public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
123 return (Permission)update(csid, theUpdate, Permission.class);
128 public Response deletePermission(@PathParam("csid") String csid) {
129 logger.debug("deletePermission with csid=" + csid);
130 ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
132 //FIXME ideally the following two ops should be in the same tx CSPACE-658
133 //delete all relationships for this permission
134 PermissionRoleSubResource subResource =
135 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
136 subResource.deletePermissionRole(csid, SubjectType.ROLE);
137 //NOTE for delete permissions in the authz provider
138 //at the PermissionRoleSubResource/DocHandler level, there is no visibility
139 //if permission is deleted, so do it here, however,
140 //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
141 //the ACL might be needed for other ACEs roles...
142 AuthorizationDelegate.deletePermissions(csid);
144 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
145 getStorageClient(ctx).delete(ctx, csid);
146 return Response.status(HttpResponseCodes.SC_OK).build();
147 } catch (Exception e) {
148 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
153 @Path("{csid}/permroles")
154 public Response createPermissionRole(@QueryParam("_method") String method,
155 @PathParam("csid") String permCsid,
156 PermissionRole input) {
157 if (method != null) {
158 if ("delete".equalsIgnoreCase(method)) {
159 return deletePermissionRole(permCsid, input);
162 logger.debug("createPermissionRole with permCsid=" + permCsid);
163 ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
165 PermissionRoleSubResource subResource =
166 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
167 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
168 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
169 path.path(permCsid + "/permroles/" + permrolecsid);
170 Response response = Response.created(path.build()).build();
172 } catch (Exception e) {
173 throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
178 @Path("{csid}/permroles/{id}")
179 public PermissionRoleRel getPermissionRole(
180 @PathParam("csid") String permCsid,
181 @PathParam("id") String permrolecsid) {
182 logger.debug("getPermissionRole with permCsid=" + permCsid);
183 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
184 PermissionRoleRel result = null;
186 PermissionRoleSubResource subResource =
187 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
188 //get relationships for a permission
189 result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
190 } catch (Exception e) {
191 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
193 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
198 @Path("{csid}/permroles")
199 public PermissionRole getPermissionRole(
200 @PathParam("csid") String permCsid) {
201 logger.debug("getPermissionRole with permCsid=" + permCsid);
202 ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
203 PermissionRole result = null;
205 PermissionRoleSubResource subResource =
206 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
207 //get relationships for a permission
208 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
209 } catch (Exception e) {
210 throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
212 checkResult(result, permCsid, ServiceMessages.GET_FAILED);
216 public Response deletePermissionRole(String permCsid, PermissionRole input) {
217 logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
218 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
220 PermissionRoleSubResource subResource =
221 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
222 //delete all relationships for a permission
223 subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
224 return Response.status(HttpResponseCodes.SC_OK).build();
225 } catch (Exception e) {
226 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
231 @Path("{csid}/permroles")
232 public Response deletePermissionRole(
233 @PathParam("csid") String permCsid) {
234 logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
235 ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
237 PermissionRoleSubResource subResource =
238 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
239 //delete all relationships for a permission
240 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
241 return Response.status(HttpResponseCodes.SC_OK).build();
242 } catch (Exception e) {
243 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);