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 2010 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.
25 * To change this template, choose Tools | Templates
26 * and open the template in the editor.
28 package org.collectionspace.services.account.storage.csidp;
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;
42 * UserStorageClient manages persistence for CS IdP
43 * Note: this class is always used by the AccountStorageClient which provides
44 * access to entity manager
47 public class UserStorageClient {
49 private final Logger logger = LoggerFactory.getLogger(UserStorageClient.class);
52 * create user with given userId and password
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());
66 * getUser get user for given userId
67 * @param em EntityManager
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();
76 String msg = "could not find user with userId=" + userId;
78 throw new DocumentNotFoundException(msg);
84 * updateUser for given userId
85 * @param entity manager
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));
98 em.persist(userFound);
103 * delete deletes user with given userId
104 * @param em entity manager
106 * @throws Exception if user for given userId not found
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();
123 String msg = "could not find user with username=" + userId;
125 throw new DocumentNotFoundException(msg);
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());
133 SecurityUtils.validatePassword(new String(password));
134 } catch (Exception e) {
135 throw new BadRequestException(e.getMessage());
137 String secEncPasswd = SecurityUtils.createPasswordHash(
138 userId, new String(password));