2012-02-12 06:55:42 +01:00
|
|
|
/*
|
2012-10-05 01:48:01 +02:00
|
|
|
* Copyright [2012] [wisemapping]
|
2012-02-12 06:55:42 +01:00
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.wisemapping.dao;
|
|
|
|
|
2012-06-09 20:49:19 +02:00
|
|
|
import com.wisemapping.model.Collaboration;
|
2012-02-21 18:22:43 +01:00
|
|
|
import com.wisemapping.model.Collaborator;
|
2012-02-12 06:55:42 +01:00
|
|
|
import com.wisemapping.model.User;
|
2012-06-23 21:15:59 +02:00
|
|
|
import com.wisemapping.model.AccessAuditory;
|
2013-03-24 23:15:37 +01:00
|
|
|
import org.hibernate.ObjectNotFoundException;
|
2012-02-21 18:22:43 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2013-03-24 23:15:37 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2012-02-12 06:55:42 +01:00
|
|
|
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
|
2012-03-14 05:49:05 +01:00
|
|
|
import org.springframework.security.authentication.encoding.PasswordEncoder;
|
2012-02-12 06:55:42 +01:00
|
|
|
//import org.acegisecurity.providers.encoding.PasswordEncoder;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class UserManagerImpl
|
|
|
|
extends HibernateDaoSupport
|
|
|
|
implements UserManager {
|
|
|
|
|
2012-03-14 05:49:05 +01:00
|
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
2012-06-14 04:04:29 +02:00
|
|
|
public void setEncoder(PasswordEncoder passwordEncoder) {
|
2012-03-14 05:49:05 +01:00
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
}
|
2012-02-12 06:55:42 +01:00
|
|
|
|
|
|
|
public List<User> getAllUsers() {
|
|
|
|
return getHibernateTemplate().find("from com.wisemapping.model.User user");
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:41:51 +01:00
|
|
|
|
|
|
|
@Override
|
2012-06-14 04:04:29 +02:00
|
|
|
public User getUserBy(@NotNull final String email) {
|
|
|
|
User user = null;
|
|
|
|
final List<User> users = getHibernateTemplate().find("from com.wisemapping.model.User colaborator where email=?", email);
|
2012-02-12 06:55:42 +01:00
|
|
|
if (users != null && !users.isEmpty()) {
|
|
|
|
assert users.size() == 1 : "More than one user with the same email!";
|
2012-06-14 04:04:29 +02:00
|
|
|
user = users.get(0);
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:41:51 +01:00
|
|
|
@Override
|
2012-02-21 18:22:43 +01:00
|
|
|
public Collaborator getCollaboratorBy(final String email) {
|
|
|
|
final Collaborator cola;
|
|
|
|
final List cols = getHibernateTemplate().find("from com.wisemapping.model.Collaborator colaborator where email=?", email);
|
2012-02-12 06:55:42 +01:00
|
|
|
if (cols != null && !cols.isEmpty()) {
|
|
|
|
assert cols.size() == 1 : "More than one colaborator with the same email!";
|
2012-02-21 18:22:43 +01:00
|
|
|
cola = (Collaborator) cols.get(0);
|
2012-02-12 06:55:42 +01:00
|
|
|
} else {
|
|
|
|
cola = null;
|
|
|
|
}
|
|
|
|
return cola;
|
|
|
|
}
|
|
|
|
|
2013-03-24 23:15:37 +01:00
|
|
|
@Nullable
|
2012-02-21 21:41:51 +01:00
|
|
|
public User getUserBy(long id) {
|
2013-03-24 23:15:37 +01:00
|
|
|
User user = null;
|
|
|
|
try{
|
|
|
|
user = getHibernateTemplate().get(User.class, id);
|
|
|
|
} catch (ObjectNotFoundException e){
|
|
|
|
// Ignore ...
|
|
|
|
}
|
|
|
|
return user;
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:41:51 +01:00
|
|
|
@Override
|
2012-02-12 06:55:42 +01:00
|
|
|
public void createUser(User user) {
|
|
|
|
assert user != null : "Trying to store a null user";
|
2012-06-14 04:04:29 +02:00
|
|
|
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
|
2012-02-12 06:55:42 +01:00
|
|
|
getHibernateTemplate().saveOrUpdate(user);
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:41:51 +01:00
|
|
|
@Override
|
|
|
|
public User createUser(@NotNull User user, @NotNull Collaborator col) {
|
2012-06-14 04:04:29 +02:00
|
|
|
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
|
2012-02-12 06:55:42 +01:00
|
|
|
assert user != null : "Trying to store a null user";
|
|
|
|
|
2012-06-09 20:49:19 +02:00
|
|
|
final Set<Collaboration> set = col.getCollaborations();
|
|
|
|
for (Collaboration collaboration : set) {
|
|
|
|
Collaboration newMapUser = new Collaboration();
|
|
|
|
newMapUser.setRoleId(collaboration.getRole().ordinal());
|
|
|
|
newMapUser.setMindMap(collaboration.getMindMap());
|
2012-02-21 18:22:43 +01:00
|
|
|
newMapUser.setCollaborator(user);
|
2012-06-12 16:23:47 +02:00
|
|
|
user.addCollaboration(newMapUser);
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getHibernateTemplate().delete(col);
|
|
|
|
getHibernateTemplate().flush();
|
|
|
|
getHibernateTemplate().saveOrUpdate(user);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:41:51 +01:00
|
|
|
@Override
|
|
|
|
public void deleteUser(@NotNull User user) {
|
|
|
|
final Collaborator collaborator = this.getCollaboratorBy(user.getEmail());
|
|
|
|
getHibernateTemplate().delete(collaborator);
|
|
|
|
getHibernateTemplate().delete(user);
|
|
|
|
getHibernateTemplate().flush();
|
|
|
|
}
|
|
|
|
|
2012-06-23 21:15:59 +02:00
|
|
|
public void auditLogin(@NotNull AccessAuditory accessAuditory) {
|
|
|
|
assert accessAuditory != null : "accessAuditory is null";
|
|
|
|
getHibernateTemplate().save(accessAuditory);
|
2012-02-12 06:55:42 +01:00
|
|
|
}
|
|
|
|
|
2012-06-20 18:28:45 +02:00
|
|
|
public void updateUser(@NotNull User user) {
|
2012-02-12 06:55:42 +01:00
|
|
|
assert user != null : "user is null";
|
2012-06-14 04:04:29 +02:00
|
|
|
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
|
2012-02-12 06:55:42 +01:00
|
|
|
getHibernateTemplate().update(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public User getUserByActivationCode(long code) {
|
|
|
|
final User user;
|
|
|
|
final List users = getHibernateTemplate().find("from com.wisemapping.model.User user where activationCode=?", code);
|
|
|
|
if (users != null && !users.isEmpty()) {
|
|
|
|
assert users.size() == 1 : "More than one user with the same username!";
|
|
|
|
user = (User) users.get(0);
|
|
|
|
} else {
|
|
|
|
user = null;
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|