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.authentication.AuthN;
32 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
33 //import org.collectionspace.services.authorization.AccountRolesList;
34 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
35 import org.collectionspace.services.authorization.AccountRole;
36 import org.collectionspace.services.authorization.AccountRoleRel;
37 import org.collectionspace.services.authorization.Role;
38 import org.collectionspace.services.authorization.RoleValue;
39 import org.collectionspace.services.authorization.SubjectType;
41 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
42 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
43 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.context.ServiceContextFactory;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.collectionspace.services.common.storage.StorageClient;
48 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
49 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
50 import org.collectionspace.services.common.context.ServiceContextProperties;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * AccountRoleSubResource is used to manage account-role relationship
59 public class AccountRoleSubResource
60 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
61 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
63 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
64 final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
65 //this service is never exposed as standalone RESTful service...just use unique
66 //service name to identify binding
67 /** The service name. */
68 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
70 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
71 /** The storage client. */
72 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
76 * @param serviceName qualified service path
78 public AccountRoleSubResource(String serviceName) {
79 this.serviceName = serviceName;
83 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
86 protected String getVersionString() {
87 /** The last change revision. */
88 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
89 return lastChangeRevision;
93 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
96 public String getServiceName() {
101 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
104 public Class<AccountRole> getCommonPartClass() {
105 return AccountRole.class;
109 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
112 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
113 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
114 return RemoteServiceContextFactory.get();
118 * Creates the service context.
120 * @param input the input
121 * @param subject the subject
123 * @return the service context< account role, account role>
125 * @throws Exception the exception
127 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
128 SubjectType subject) throws Exception {
129 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
130 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
131 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
132 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
133 //subject name is necessary to indicate if role or account is a subject
134 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
136 //set context for the relationship query
137 if (subject == SubjectType.ROLE) {
138 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
139 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
140 } else if (subject == SubjectType.ACCOUNT) {
141 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
142 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
149 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
152 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
153 //FIXME use ctx to identify storage client
154 return storageClient;
158 * createAccountRole creates one or more account-role relationships
159 * between object (account/role) and subject (role/account)
165 public String createAccountRole(AccountRole input, SubjectType subject)
169 // We need to associate every new account with the Spring Security Admin role so we can make
170 // changes to the Spring Security ACL tables. The Spring Security Admin role has NO CollectionSpace
171 // specific permissions. It is an internal/private role that service consumers and end-users NEVER see.
174 //Preserve the original incoming list of roles
175 List<RoleValue> inputRoleValues = input.getRole();
177 //Change the role list to be just the Spring role
178 List<RoleValue> springRoles = new ArrayList<RoleValue>();
179 input.setRole(springRoles);
180 RoleValue springAdminRole = new RoleValue();
181 springRoles.add(springAdminRole);
182 springAdminRole.setRoleId(AuthN.ROLE_SPRING_ADMIN_ID);
183 springAdminRole.setRoleName(AuthN.ROLE_SPRING_ADMIN_NAME);
185 // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
186 // we'll just ignore.
188 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
189 DocumentHandler handler = createDocumentHandler(ctx);
190 getStorageClient(ctx).create(ctx, handler);
191 } catch (PersistenceException e) {
192 //If we get this exception, it means that the role relationship already exists, so
193 //we can just ignore this exception.
194 if (logger.isTraceEnabled() == true) {
195 logger.trace(AuthN.ROLE_SPRING_ADMIN_NAME +
196 " relationship already exists for account: " +
197 input.getAccount().get(0).getAccountId(), e);
202 // Now we'll add the account relationships for the original incoming roles.
204 input.setRole(inputRoleValues);
205 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
206 DocumentHandler handler = createDocumentHandler(ctx);
207 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
213 * getAccountRole retrieves account-role relationships using given
214 * csid of object (account/role) and subject (role/account)
220 public AccountRole getAccountRole(
221 String csid, SubjectType subject) throws Exception {
223 if (logger.isDebugEnabled()) {
224 logger.debug("getAccountRole with csid=" + csid);
226 AccountRole result = null;
227 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
228 DocumentHandler handler = createDocumentHandler(ctx);
229 getStorageClient(ctx).get(ctx, csid, handler);
230 result = (AccountRole) ctx.getOutput();
236 * Gets the account role.
238 * @param csid the csid
239 * @param subject the subject
240 * @param accountRoleCsid the account role csid
241 * @return the account role
242 * @throws Exception the exception
244 public AccountRoleRel getAccountRoleRel(String csid,
246 String accountRoleCsid) throws Exception {
248 if (logger.isDebugEnabled()) {
249 logger.debug("getAccountRole with csid=" + csid);
251 // AccountRolesList result = new AccountRolesList();
252 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
253 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
254 handler.setAccountRoleCsid(accountRoleCsid);
255 //getStorageClient(ctx).get(ctx, csid, handler);
256 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
257 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
258 // AccountRoleListItem listItem = new AccountRoleListItem();
260 // listItem.setCsid(accountRoleRel.getHjid().toString());
261 // listItem.setRoleId(accountRoleRel.getRoleId());
262 // listItem.setRoleName(accountRoleRel.getRoleName());
263 // add item to result list
264 // result = (AccountRolesList) ctx.getOutput();
266 return accountRoleRel;
270 * X_delete account role.
272 * @param csid the csid
273 * @param subject the subject
274 * @throws Exception the exception
276 public void x_deleteAccountRole(String csid,
277 SubjectType subject) throws Exception {
279 if (logger.isDebugEnabled()) {
280 logger.debug("deleteAccountRole with csid=" + csid);
282 AccountRole toDelete = getAccountRole(csid, subject);
283 deleteAccountRole(csid, subject, toDelete);
287 * deleteAccountRole deletes all account-role relationships using given
288 * csid of object (account/role) and subject (role/account)
289 * @param csid of the object
294 public void deleteAccountRole(String csid,
295 SubjectType subject) throws Exception {
297 if (logger.isDebugEnabled()) {
298 logger.debug("deleteAccountRole with csid=" + csid);
300 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
301 getStorageClient(ctx).delete(ctx, csid);
305 * deleteAccountRole deletes given account-role relationships using given
306 * csid of object (account/role) and subject (role/account)
307 * @param csid of the object
309 * @param input with account role relationships to delete
313 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
316 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
317 DocumentHandler handler = createDocumentHandler(ctx);
318 getStorageClient(ctx).delete(ctx, csid, handler);