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.Query;
33 import org.collectionspace.services.authentication.User;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.BadRequestException;
36 import org.collectionspace.services.common.document.DocumentNotFoundException;
37 import org.collectionspace.services.common.document.JaxbUtils;
38 import org.collectionspace.services.common.document.TransactionException;
39 import org.collectionspace.services.common.security.SecurityUtils;
40 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * UserStorageClient manages persistence for CS IdP
47 * Note: this class is always used by the AccountStorageClient which provides
48 * access to entity manager
51 public class UserStorageClient {
53 private final Logger logger = LoggerFactory.getLogger(UserStorageClient.class);
56 * create user with given userId and password
61 public User create(String userId, byte[] password) throws Exception {
62 User user = new User();
63 user.setUsername(userId);
64 user.setPasswd(getEncPassword(userId, password));
65 user.setCreatedAtItem(new Date());
70 * getUser get user for given userId
71 * @param em EntityManager
74 public User get(JPATransactionContext jpaTransactionContext, String userId) throws DocumentNotFoundException {
75 User userFound = (User) jpaTransactionContext.find(User.class, userId);
76 if (userFound == null) {
77 String msg = "Could not find user with userId=" + userId;
79 throw new DocumentNotFoundException(msg);
85 @SuppressWarnings("rawtypes")
86 public User get(ServiceContext ctx, String userId) throws DocumentNotFoundException, TransactionException {
87 User userFound = null;
89 JPATransactionContext jpaConnectionContext = (JPATransactionContext)ctx.openConnection();
91 userFound = (User) jpaConnectionContext.find(User.class, userId);
92 if (userFound == null) {
93 String msg = "could not find user with userId=" + userId;
95 throw new DocumentNotFoundException(msg);
98 ctx.closeConnection();
105 * updateUser for given userId
106 * @param entity manager
110 public void update(JPATransactionContext jpaTransactionContext, String userId, byte[] password)
111 throws DocumentNotFoundException, Exception {
112 User userFound = get(jpaTransactionContext, userId);
113 if (userFound != null) {
114 userFound.setPasswd(getEncPassword(userId, password));
115 userFound.setUpdatedAtItem(new Date());
116 if (logger.isDebugEnabled()) {
117 logger.debug("updated user=" + JaxbUtils.toString(userFound, User.class));
119 jpaTransactionContext.persist(userFound);
124 * delete deletes user with given userId
125 * @param em entity manager
127 * @throws Exception if user for given userId not found
129 public void delete(JPATransactionContext jpaTransactionContext, String userId)
130 throws DocumentNotFoundException, Exception {
131 //if userid gives any indication about the id provider, it should
132 //be used to avoid the following approach
133 StringBuilder usrDelStr = new StringBuilder("DELETE FROM ");
134 usrDelStr.append(User.class.getCanonicalName());
135 usrDelStr.append(" WHERE username = :username");
136 //TODO: add tenant id
137 Query usrDel = jpaTransactionContext.createQuery(usrDelStr.toString());
138 usrDel.setParameter("username", userId);
139 int usrDelCount = usrDel.executeUpdate();
140 if (usrDelCount != 1) {
141 String msg = "could not find user with username=" + userId;
143 throw new DocumentNotFoundException(msg);
147 private String getEncPassword(String userId, byte[] password) throws BadRequestException {
148 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
149 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
151 SecurityUtils.validatePassword(new String(password));
152 } catch (Exception e) {
153 throw new BadRequestException(e.getMessage());
155 String secEncPasswd = SecurityUtils.createPasswordHash(
156 userId, new String(password));