]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
811df57b699ce202bd86e39ddbea1ac170651c1c
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.account;
25
26 import java.util.List;
27 import java.util.ArrayList;
28
29 import javax.persistence.PersistenceException;
30
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;
40
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;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * AccountRoleSubResource is used to manage account-role relationship
57  * @author
58  */
59 public class AccountRoleSubResource
60 //        extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
61         extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
62         
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;
69     /** The logger. */
70     final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
71     /** The storage client. */
72     final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
73
74     /**
75      *
76      * @param serviceName qualified service path
77      */
78     public AccountRoleSubResource(String serviceName) {
79         this.serviceName = serviceName;
80     }
81
82     /* (non-Javadoc)
83      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
84      */
85     @Override
86     protected String getVersionString() {
87         /** The last change revision. */
88         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
89         return lastChangeRevision;
90     }
91
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
94      */
95     @Override
96     public String getServiceName() {
97         return serviceName;
98     }
99
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
102      */
103     @Override
104     public Class<AccountRole> getCommonPartClass() {
105         return AccountRole.class;
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
110      */
111     @Override
112     public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
113 //    public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
114         return RemoteServiceContextFactory.get();
115     }
116
117     /**
118      * Creates the service context.
119      * 
120      * @param input the input
121      * @param subject the subject
122      * 
123      * @return the service context< account role, account role>
124      * 
125      * @throws Exception the exception
126      */
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);
135         
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");
143         }
144         
145         return ctx;
146     }
147
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
150      */
151     @Override
152     public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
153         //FIXME use ctx to identify storage client
154         return storageClient;
155     }
156
157     /**
158      * createAccountRole creates one or more account-role relationships
159      * between object (account/role) and subject (role/account)
160      * @param input
161      * @param subject
162      * @return
163      * @throws Exception
164      */
165     public String createAccountRole(AccountRole input, SubjectType subject)
166             throws Exception {
167         
168         //
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.
172         //
173         
174         //Preserve the original incoming list of roles
175         List<RoleValue> inputRoleValues = input.getRole();
176
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);
184
185         // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
186         // we'll just ignore.
187         try {
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);
198                 }
199         }
200         
201         //
202         // Now we'll add the account relationships for the original incoming roles.
203         //
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);
208         
209         return bogusCsid;
210     }
211
212     /**
213      * getAccountRole retrieves account-role relationships using given
214      * csid of object (account/role) and subject (role/account)
215      * @param csid
216      * @param subject
217      * @return
218      * @throws Exception
219      */
220     public AccountRole getAccountRole(
221             String csid, SubjectType subject) throws Exception {
222
223         if (logger.isDebugEnabled()) {
224             logger.debug("getAccountRole with csid=" + csid);
225         }
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();
231
232         return result;
233     }
234
235     /**
236      * Gets the account role.
237      *
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
243      */
244     public AccountRoleRel getAccountRoleRel(String csid,
245                 SubjectType subject,
246                 String accountRoleCsid) throws Exception {
247
248         if (logger.isDebugEnabled()) {
249             logger.debug("getAccountRole with csid=" + csid);
250         }
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();
259 //        // fill the item
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();
265         
266         return accountRoleRel;
267     }
268
269     /**
270      * X_delete account role.
271      *
272      * @param csid the csid
273      * @param subject the subject
274      * @throws Exception the exception
275      */
276     public void x_deleteAccountRole(String csid,
277             SubjectType subject) throws Exception {
278
279         if (logger.isDebugEnabled()) {
280             logger.debug("deleteAccountRole with csid=" + csid);
281         }
282         AccountRole toDelete = getAccountRole(csid, subject);
283         deleteAccountRole(csid, subject, toDelete);
284     }
285     
286     /**
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
290      * @param subject
291      * @return
292      * @throws Exception
293      */
294     public void deleteAccountRole(String csid,
295             SubjectType subject) throws Exception {
296
297         if (logger.isDebugEnabled()) {
298             logger.debug("deleteAccountRole with csid=" + csid);
299         }
300         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
301         getStorageClient(ctx).delete(ctx, csid);
302     }
303
304     /**
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
308      * @param subject
309      * @param input with account role relationships to delete
310      * @return
311      * @throws Exception
312      */
313     public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
314             throws Exception {
315
316         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
317         DocumentHandler handler = createDocumentHandler(ctx);
318         getStorageClient(ctx).delete(ctx, csid, handler);
319     }
320 }