Files
wisemapping-open-source/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java

190 lines
6.3 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;
2023-10-30 05:34:45 +00:00
import com.wisemapping.model.*;
import com.wisemapping.security.DefaultPasswordEncoderFactories;
import com.wisemapping.security.LegacyPasswordEncoder;
2024-01-13 18:31:21 -08:00
import jakarta.persistence.EntityManager;
2023-12-04 20:00:06 -08:00
import jakarta.persistence.EntityManagerFactory;
2024-01-13 18:31:21 -08:00
import jakarta.persistence.TypedQuery;
2013-03-24 19:15:37 -03:00
import org.hibernate.ObjectNotFoundException;
2023-10-30 05:34:45 +00:00
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.SelectionQuery;
import org.jetbrains.annotations.NotNull;
2013-03-24 19:15:37 -03:00
import org.jetbrains.annotations.Nullable;
2023-11-19 08:05:38 -08:00
import org.springframework.beans.factory.annotation.Autowired;
2020-11-07 11:56:38 -08:00
import org.springframework.security.crypto.password.PasswordEncoder;
2023-10-30 05:34:45 +00:00
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
2023-10-30 05:34:45 +00:00
@Repository
public class UserManagerImpl
implements UserManager {
2023-12-04 20:00:06 -08:00
@Autowired
private EntityManagerFactory entityManagerFactory;
2024-01-13 18:31:21 -08:00
@Autowired
private EntityManager entityManager;
2023-11-19 08:05:38 -08:00
@Autowired
private PasswordEncoder passwordEncoder;
2024-01-13 18:31:21 -08:00
public UserManagerImpl() {
}
2012-06-13 23:04:29 -03:00
public void setEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
public List<User> getAllUsers() {
2024-01-13 18:31:21 -08:00
return entityManager.createQuery("from com.wisemapping.model.User user", User.class).getResultList();
2023-10-30 05:34:45 +00:00
}
private Session getSession() {
2023-12-04 20:00:06 -08:00
return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();
}
@Override
@Nullable
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
2024-01-13 18:31:21 -08:00
TypedQuery<User> query = entityManager.createQuery("from com.wisemapping.model.User colaborator where email=:email",User.class);
2020-11-07 11:56:38 -08:00
query.setParameter("email", email);
2023-10-30 05:34:45 +00:00
final List<User> users = query.getResultList();
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) {
2023-10-30 05:34:45 +00:00
final Collaborator result;
Session session = getSession();
final SelectionQuery<Collaborator> query = session.createSelectionQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email", Collaborator.class);
2020-11-07 11:56:38 -08:00
query.setParameter("email", email);
2023-10-30 05:34:45 +00:00
final List<Collaborator> cols = query.getResultList();
if (cols != null && !cols.isEmpty()) {
assert cols.size() == 1 : "More than one colaborator with the same email!";
2023-10-30 05:34:45 +00:00
result = cols.get(0);
} else {
2023-10-30 05:34:45 +00:00
result = null;
}
2023-10-30 05:34:45 +00:00
return result;
}
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 {
2023-10-30 05:34:45 +00:00
user = getSession().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";
2022-12-13 02:36:58 +00:00
if (!AuthenticationType.GOOGLE_OAUTH2.equals(user.getAuthenticationType())) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
} else {
user.setPassword("");
}
2023-10-30 05:34:45 +00:00
getSession().persist(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 ...
2023-10-30 05:34:45 +00:00
final Session session = getSession();
2022-09-27 21:46:42 -07:00
collaborator.setEmail(collaborator.getEmail() + "_toRemove");
2023-10-30 05:34:45 +00:00
session.merge(collaborator);
session.flush();
2022-02-17 17:34:22 -08:00
// Save all new...
2022-02-17 17:36:46 -08:00
this.createUser(user);
2022-02-16 18:05:26 -08:00
2022-09-27 21:46:42 -07:00
// Update mindmap ...
final Set<Collaboration> collaborations = new CopyOnWriteArraySet<>(collaborator.getCollaborations());
2022-09-27 21:46:42 -07:00
for (Collaboration collabs : collaborations) {
collabs.setCollaborator(user);
}
// Delete old user ...
2023-10-30 05:34:45 +00:00
session.remove(collaborator);
return user;
}
@Override
2014-01-25 12:31:14 -03:00
public void removeUser(@NotNull final User user) {
2023-10-30 05:34:45 +00:00
getSession().remove(user);
}
2012-06-23 16:15:59 -03:00
public void auditLogin(@NotNull AccessAuditory accessAuditory) {
assert accessAuditory != null : "accessAuditory is null";
2023-10-30 05:34:45 +00:00
getSession().persist(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()));
}
2023-10-30 05:34:45 +00:00
getSession().merge(user);
}
public User getUserByActivationCode(long code) {
final User user;
2020-11-07 11:56:38 -08:00
2023-10-30 05:34:45 +00:00
final SelectionQuery<User> query = getSession().createSelectionQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode", User.class);
2020-11-07 11:56:38 -08:00
query.setParameter("activationCode", code);
2023-10-30 05:34:45 +00:00
final List<User> users = query.getResultList();
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!";
2023-10-30 05:34:45 +00:00
user = users.get(0);
} else {
user = null;
}
return user;
}
}