]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2345ec102ac3e694c69cc25261385854ab850195
[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 2010 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 /*
25  * To change this template, choose Tools | Templates
26  * and open the template in the editor.
27  */
28 package org.collectionspace.services.account.storage.csidp;
29
30 import java.util.Date;
31 import javax.persistence.EntityManager;
32 import javax.persistence.Query;
33 import org.collectionspace.services.authentication.User;
34 import org.collectionspace.services.common.document.BadRequestException;
35 import org.collectionspace.services.common.document.DocumentNotFoundException;
36 import org.collectionspace.services.common.document.JaxbUtils;
37 import org.collectionspace.services.common.security.SecurityUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * UserStorageClient manages persistence for CS IdP
43  * Note: this class is always used by the AccountStorageClient which provides
44  * access to entity manager
45  * @author
46  */
47 public class UserStorageClient {
48
49     private final Logger logger = LoggerFactory.getLogger(UserStorageClient.class);
50
51     /**
52      * create user with given userId and password
53      * @param userId
54      * @param password
55      * @return user
56      */
57     public User create(String userId, byte[] password) throws Exception {
58         User user = new User();
59         user.setUsername(userId);
60         user.setPasswd(getEncPassword(userId, password));
61         user.setCreatedAtItem(new Date());
62         return user;
63     }
64
65     /**
66      * getUser get user for given userId
67      * @param em EntityManager
68      * @param userId
69      */
70     public User get(EntityManager em, String userId) throws DocumentNotFoundException {
71         User userFound = em.find(User.class, userId);
72         if (userFound == null) {
73             if (em != null && em.getTransaction().isActive()) {
74                 em.getTransaction().rollback();
75             }
76             String msg = "could not find user with userId=" + userId;
77             logger.error(msg);
78             throw new DocumentNotFoundException(msg);
79         }
80         return userFound;
81     }
82
83     /**
84      * updateUser for given userId
85      * @param entity manager
86      * @param userId
87      * @param password
88      */
89     public void update(EntityManager em, String userId, byte[] password)
90             throws DocumentNotFoundException, Exception {
91         User userFound = get(em, userId);
92         if (userFound != null) {
93             userFound.setPasswd(getEncPassword(userId, password));
94             userFound.setUpdatedAtItem(new Date());
95             if (logger.isDebugEnabled()) {
96                 logger.debug("updated user=" + JaxbUtils.toString(userFound, User.class));
97             }
98             em.persist(userFound);
99         }
100     }
101
102     /**
103      * delete deletes user with given userId
104      * @param em entity manager
105      * @param userId
106      * @throws Exception if user for given userId not found
107      */
108     public void delete(EntityManager em, String userId)
109             throws DocumentNotFoundException, Exception {
110         //if userid gives any indication about the id provider, it should
111         //be used to avoid the following approach
112         StringBuilder usrDelStr = new StringBuilder("DELETE FROM ");
113         usrDelStr.append(User.class.getCanonicalName());
114         usrDelStr.append(" WHERE username = :username");
115         //TODO: add tenant id
116         Query usrDel = em.createQuery(usrDelStr.toString());
117         usrDel.setParameter("username", userId);
118         int usrDelCount = usrDel.executeUpdate();
119         if (usrDelCount != 1) {
120             if (em != null && em.getTransaction().isActive()) {
121                 em.getTransaction().rollback();
122             }
123             String msg = "could not find user with username=" + userId;
124             logger.error(msg);
125             throw new DocumentNotFoundException(msg);
126         }
127     }
128
129     private String getEncPassword(String userId, byte[] password) throws BadRequestException {
130         //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
131         //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
132         try {
133             SecurityUtils.validatePassword(new String(password));
134         } catch (Exception e) {
135             throw new BadRequestException(e.getMessage());
136         }
137         String secEncPasswd = SecurityUtils.createPasswordHash(
138                 userId, new String(password));
139         return secEncPasswd;
140     }
141 }