175 lines
5.9 KiB
Java
Raw Normal View History

/*
2022-03-17 18:47:34 -03:00
* Copyright [2022] [wisemapping]
2020-11-07 11:56:38 -08: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;
import com.wisemapping.model.AccessAuditory;
2012-06-09 15:49:19 -03:00
import com.wisemapping.model.Collaboration;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import com.wisemapping.security.DefaultPasswordEncoderFactories;
import com.wisemapping.security.LegacyPasswordEncoder;
2013-03-24 19:15:37 -03:00
import org.hibernate.ObjectNotFoundException;
import org.jetbrains.annotations.NotNull;
2013-03-24 19:15:37 -03:00
import org.jetbrains.annotations.Nullable;
2022-02-17 17:34:22 -08:00
import org.springframework.orm.hibernate5.HibernateTemplate;
2020-11-07 11:56:38 -08:00
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.security.crypto.password.PasswordEncoder;
2022-02-17 17:34:22 -08:00
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class UserManagerImpl
extends HibernateDaoSupport
implements UserManager {
private PasswordEncoder passwordEncoder;
2012-06-13 23:04:29 -03:00
public void setEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
2020-11-07 11:56:38 -08:00
@SuppressWarnings("unchecked")
public List<User> getAllUsers() {
2020-11-07 11:56:38 -08:00
return currentSession().createQuery("from com.wisemapping.model.User user").list();
}
@Override
2012-06-13 23:04:29 -03:00
public User getUserBy(@NotNull final String email) {
User user = null;
2020-11-07 11:56:38 -08:00
var query = currentSession().createQuery("from com.wisemapping.model.User colaborator where email=:email");
query.setParameter("email", email);
final List<User> users = query.list();
if (users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same email!";
2012-06-13 23:04:29 -03:00
user = users.get(0);
}
return user;
2020-11-07 11:56:38 -08:00
}
@Override
public Collaborator getCollaboratorBy(final String email) {
final Collaborator cola;
2020-11-07 11:56:38 -08:00
var query = currentSession().createQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email");
query.setParameter("email", email);
final List<User> cols = query.list();
if (cols != null && !cols.isEmpty()) {
assert cols.size() == 1 : "More than one colaborator with the same email!";
2020-11-07 11:56:38 -08:00
cola = cols.get(0);
} else {
cola = null;
}
return cola;
}
2013-03-24 19:15:37 -03:00
@Nullable
@Override
public User getUserBy(int id) {
2013-03-24 19:15:37 -03:00
User user = null;
2020-11-07 11:56:38 -08:00
try {
2013-03-24 19:15:37 -03:00
user = getHibernateTemplate().get(User.class, id);
2020-11-07 11:56:38 -08:00
} catch (ObjectNotFoundException e) {
2013-03-24 19:15:37 -03:00
// Ignore ...
}
return user;
}
@Override
public void createUser(User user) {
assert user != null : "Trying to store a null user";
2020-11-07 11:56:38 -08:00
user.setPassword(passwordEncoder.encode(user.getPassword()));
getHibernateTemplate().saveOrUpdate(user);
}
@Override
2022-02-16 18:05:26 -08:00
public User createUser(@NotNull User user, @NotNull Collaborator collaborator) {
2022-02-17 17:34:22 -08:00
assert user != null : "Trying to store a null user";
2022-02-16 18:29:28 -08:00
// Migrate from previous temporal collab to new user ...
2022-02-17 17:34:22 -08:00
List<Collaboration> newCollabs = new ArrayList<>();
2022-02-16 18:05:26 -08:00
final Set<Collaboration> collaborations = collaborator.getCollaborations();
2022-02-16 18:29:28 -08:00
for (Collaboration oldCollab : collaborations) {
Collaboration newCollab = new Collaboration();
newCollab.setRoleId(oldCollab.getRole().ordinal());
newCollab.setMindMap(oldCollab.getMindMap());
newCollab.setCollaborator(user);
user.addCollaboration(newCollab);
2022-02-17 17:34:22 -08:00
newCollabs.add(newCollab);
}
2022-02-17 17:34:22 -08:00
// Delete old collaboration
final HibernateTemplate template = getHibernateTemplate();
collaborations.forEach(c -> template.delete(c));
template.delete(collaborator);
2022-02-17 18:11:54 -08:00
template.flush();
2022-02-17 17:34:22 -08:00
// Save all new...
2022-02-17 17:36:46 -08:00
this.createUser(user);
newCollabs.forEach(c -> template.saveOrUpdate(c));
2022-02-16 18:05:26 -08:00
return user;
}
@Override
2014-01-25 12:31:14 -03:00
public void removeUser(@NotNull final User user) {
getHibernateTemplate().delete(user);
}
2012-06-23 16:15:59 -03:00
public void auditLogin(@NotNull AccessAuditory accessAuditory) {
assert accessAuditory != null : "accessAuditory is null";
getHibernateTemplate().save(accessAuditory);
}
2012-06-20 13:28:45 -03:00
public void updateUser(@NotNull User user) {
assert user != null : "user is null";
// Does the password need to be encrypted ?
final String password = user.getPassword();
2022-02-16 18:29:28 -08:00
if (password != null && (!password.startsWith(LegacyPasswordEncoder.ENC_PREFIX) && !password.startsWith("{" + DefaultPasswordEncoderFactories.ENCODING_ID))) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
}
getHibernateTemplate().update(user);
}
public User getUserByActivationCode(long code) {
final User user;
2020-11-07 11:56:38 -08:00
var query = currentSession().createQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode");
query.setParameter("activationCode", code);
final List users = query.list();
2022-02-16 18:29:28 -08:00
if (users != null && !users.isEmpty()) {
2020-11-07 11:56:38 -08:00
assert users.size() == 1 : "More than one user with the same username!";
user = (User) users.get(0);
} else {
user = null;
}
return user;
}
}