2012-02-12 02:55:42 -03:00
|
|
|
/*
|
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.
|
|
|
|
*/
|
2012-02-12 02:55:42 -03:00
|
|
|
|
|
|
|
package com.wisemapping.dao;
|
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
import com.wisemapping.model.AccessAuditory;
|
2012-06-09 15:49:19 -03:00
|
|
|
import com.wisemapping.model.Collaboration;
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.model.Collaborator;
|
2012-02-12 02:55:42 -03:00
|
|
|
import com.wisemapping.model.User;
|
2021-12-24 18:03:23 -08:00
|
|
|
import com.wisemapping.security.DefaultPasswordEncoderFactories;
|
|
|
|
import com.wisemapping.security.LegacyPasswordEncoder;
|
2013-03-24 19:15:37 -03:00
|
|
|
import org.hibernate.ObjectNotFoundException;
|
2012-02-21 14:22:43 -03:00
|
|
|
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;
|
2012-02-12 02:55:42 -03:00
|
|
|
|
2022-02-17 17:34:22 -08:00
|
|
|
import java.util.ArrayList;
|
2012-02-12 02:55:42 -03:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class UserManagerImpl
|
|
|
|
extends HibernateDaoSupport
|
|
|
|
implements UserManager {
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
2012-06-13 23:04:29 -03:00
|
|
|
public void setEncoder(PasswordEncoder passwordEncoder) {
|
2012-03-14 01:49:05 -03:00
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
}
|
2012-02-12 02:55:42 -03:00
|
|
|
|
2020-11-07 11:56:38 -08:00
|
|
|
@SuppressWarnings("unchecked")
|
2012-02-12 02:55:42 -03:00
|
|
|
public List<User> getAllUsers() {
|
2020-11-07 11:56:38 -08:00
|
|
|
return currentSession().createQuery("from com.wisemapping.model.User user").list();
|
2012-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
|
|
|
|
@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();
|
2012-02-12 02:55:42 -03:00
|
|
|
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);
|
2012-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
return user;
|
2020-11-07 11:56:38 -08:00
|
|
|
|
2012-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@Override
|
2012-02-21 14:22:43 -03:00
|
|
|
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();
|
2012-02-12 02:55:42 -03:00
|
|
|
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);
|
2012-02-12 02:55:42 -03:00
|
|
|
} else {
|
|
|
|
cola = null;
|
|
|
|
}
|
|
|
|
return cola;
|
|
|
|
}
|
|
|
|
|
2013-03-24 19:15:37 -03:00
|
|
|
@Nullable
|
2022-01-16 20:01:56 -08:00
|
|
|
@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;
|
2012-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@Override
|
2012-02-12 02:55:42 -03:00
|
|
|
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()));
|
2012-02-12 02:55:42 -03:00
|
|
|
getHibernateTemplate().saveOrUpdate(user);
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@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";
|
2012-02-12 02:55:42 -03:00
|
|
|
|
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);
|
2012-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-02-12 02:55:42 -03:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@Override
|
2014-01-25 12:31:14 -03:00
|
|
|
public void removeUser(@NotNull final User user) {
|
2012-02-21 17:41:51 -03:00
|
|
|
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-02-12 02:55:42 -03:00
|
|
|
}
|
|
|
|
|
2012-06-20 13:28:45 -03:00
|
|
|
public void updateUser(@NotNull User user) {
|
2012-02-12 02:55:42 -03:00
|
|
|
assert user != null : "user is null";
|
2021-12-24 18:03:23 -08:00
|
|
|
|
|
|
|
// 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))) {
|
2021-12-24 18:03:23 -08:00
|
|
|
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
|
|
|
}
|
|
|
|
|
2012-02-12 02:55:42 -03:00
|
|
|
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
|
|
|
|
2012-02-12 02:55:42 -03:00
|
|
|
assert users.size() == 1 : "More than one user with the same username!";
|
|
|
|
user = (User) users.get(0);
|
|
|
|
} else {
|
|
|
|
user = null;
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|