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.account;
26 import java.util.List;
27 import java.util.ArrayList;
29 import javax.persistence.PersistenceException;
31 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
32 //import org.collectionspace.services.authorization.AccountRolesList;
33 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
34 import org.collectionspace.services.authorization.AccountRole;
35 import org.collectionspace.services.authorization.AccountValue;
36 import org.collectionspace.services.authorization.AccountRoleRel;
37 import org.collectionspace.services.authorization.Permission;
38 import org.collectionspace.services.authorization.Role;
39 import org.collectionspace.services.authorization.RoleValue;
40 import org.collectionspace.services.authorization.SubjectType;
42 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
43 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
44 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.context.ServiceContextFactory;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.collectionspace.services.common.storage.StorageClient;
49 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
50 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
51 import org.collectionspace.services.common.context.ServiceContextProperties;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * AccountRoleSubResource is used to manage account-role relationship
60 public class AccountRoleSubResource
61 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
62 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
64 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
65 final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
66 //this service is never exposed as standalone RESTful service...just use unique
67 //service name to identify binding
68 /** The service name. */
69 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
71 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
72 /** The storage client. */
73 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
77 * @param serviceName qualified service path
79 public AccountRoleSubResource(String serviceName) {
80 this.serviceName = serviceName;
84 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
87 protected String getVersionString() {
88 /** The last change revision. */
89 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
90 return lastChangeRevision;
94 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
97 public String getServiceName() {
102 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
105 public Class<AccountRole> getCommonPartClass() {
106 return AccountRole.class;
110 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
113 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
114 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
115 return RemoteServiceContextFactory.get();
119 * Creates the service context.
121 * @param input the input
122 * @param subject the subject
124 * @return the service context< account role, account role>
126 * @throws Exception the exception
128 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
129 SubjectType subject) throws Exception {
130 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
131 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
132 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
133 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
134 //subject name is necessary to indicate if role or account is a subject
135 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
137 //set context for the relationship query
138 if (subject == SubjectType.ROLE) {
139 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
140 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
141 } else if (subject == SubjectType.ACCOUNT) {
142 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
143 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
150 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
153 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
154 //FIXME use ctx to identify storage client
155 return storageClient;
159 * createAccountRole creates one or more account-role relationships
160 * between object (account/role) and subject (role/account)
166 public String createAccountRole(AccountRole input, SubjectType subject)
170 // We need to associate every new account with the Spring Security Admin role so we can make
171 // changes to the Spring Security ACL tables. The Spring Security Admin role has NO CollectionSpace
172 // specific permissions. It is an internal/private role that service consumers and end-users NEVER see.
175 //Preserve the original incoming list of roles
176 List<RoleValue> inputRoleValues = input.getRoles();
178 //Change the role list to be just the Spring role
179 List<RoleValue> springRoles = new ArrayList<RoleValue>();
180 input.setRoles(springRoles);
181 RoleValue springAdminRole = new RoleValue();
182 springRoles.add(springAdminRole);
183 springAdminRole.setRoleId(AuthorizationCommon.ROLE_SPRING_ADMIN_ID);
184 springAdminRole.setRoleName(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME);
186 // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
187 // we'll just ignore.
189 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
190 DocumentHandler handler = createDocumentHandler(ctx);
191 getStorageClient(ctx).create(ctx, handler);
192 } catch (PersistenceException e) {
193 //If we get this exception, it means that the role relationship already exists, so
194 //we can just ignore this exception.
195 if (logger.isTraceEnabled() == true) {
196 logger.trace(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME +
197 " relationship already exists for account: " +
198 input.getAccounts().get(0).getAccountId(), e);
203 // Now we'll add the account relationships for the original incoming roles.
205 input.setRoles(inputRoleValues);
206 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
207 DocumentHandler handler = createDocumentHandler(ctx);
208 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
214 * getAccountRole retrieves account-role relationships using given
215 * csid of object (account/role) and subject (role/account)
221 public AccountRole getAccountRole(
222 String csid, SubjectType subject) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug("getAccountRole with csid=" + csid);
227 AccountRole result = null;
228 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
229 DocumentHandler handler = createDocumentHandler(ctx);
230 getStorageClient(ctx).get(ctx, csid, handler);
231 result = (AccountRole) ctx.getOutput();
237 * Gets the account role.
239 * @param csid the csid
240 * @param subject the subject
241 * @param accountRoleCsid the account role csid
242 * @return the account role
243 * @throws Exception the exception
245 public AccountRoleRel getAccountRoleRel(String csid,
247 String accountRoleCsid) throws Exception {
249 if (logger.isDebugEnabled()) {
250 logger.debug("getAccountRole with csid=" + csid);
252 // AccountRolesList result = new AccountRolesList();
253 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
254 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
255 handler.setAccountRoleCsid(accountRoleCsid);
256 //getStorageClient(ctx).get(ctx, csid, handler);
257 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
258 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
259 // AccountRoleListItem listItem = new AccountRoleListItem();
261 // listItem.setCsid(accountRoleRel.getHjid().toString());
262 // listItem.setRoleId(accountRoleRel.getRoleId());
263 // listItem.setRoleName(accountRoleRel.getRoleName());
264 // add item to result list
265 // result = (AccountRolesList) ctx.getOutput();
267 return accountRoleRel;
271 * X_delete account role.
273 * @param csid the csid
274 * @param subject the subject
275 * @throws Exception the exception
277 public void x_deleteAccountRole(String csid,
278 SubjectType subject) throws Exception {
280 if (logger.isDebugEnabled()) {
281 logger.debug("deleteAccountRole with csid=" + csid);
283 AccountRole toDelete = getAccountRole(csid, subject);
284 deleteAccountRole(csid, subject, toDelete);
288 * deleteAccountRole deletes all account-role relationships using given
289 * csid of object (account/role) and subject (role/account)
290 * @param csid of the object
295 public void deleteAccountRole(String csid,
296 SubjectType subject) throws Exception {
298 if (logger.isDebugEnabled()) {
299 logger.debug("deleteAccountRole with csid=" + csid);
301 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
302 getStorageClient(ctx).delete(ctx, csid);
306 * deleteAccountRole deletes given account-role relationships using given
307 * csid of object (account/role) and subject (role/account)
308 * @param csid of the object
310 * @param input with account role relationships to delete
314 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
317 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
318 DocumentHandler handler = createDocumentHandler(ctx);
319 getStorageClient(ctx).delete(ctx, csid, handler);