View Javadoc
1   package de.dlr.shepard.auth.users.services;
2   
3   import de.dlr.shepard.auth.security.AuthenticationContext;
4   import de.dlr.shepard.auth.users.daos.UserDAO;
5   import de.dlr.shepard.auth.users.entities.User;
6   import de.dlr.shepard.common.exceptions.InvalidAuthException;
7   import de.dlr.shepard.common.exceptions.InvalidPathException;
8   import de.dlr.shepard.common.exceptions.InvalidRequestException;
9   import io.quarkus.logging.Log;
10  import jakarta.enterprise.context.RequestScoped;
11  import jakarta.inject.Inject;
12  import java.util.Optional;
13  
14  @RequestScoped
15  public class UserService {
16  
17    @Inject
18    UserDAO userDAO;
19  
20    @Inject
21    AuthenticationContext authenticationContext;
22  
23    /**
24     * Update a user in Neo4J. The user is created if it does not exist.
25     *
26     * @param user The user to be updated
27     * @return The updated user
28     */
29    public User createOrUpdateUser(User user) {
30      Optional<User> oldUserOptional = getUserOptional(user.getUsername());
31      if (oldUserOptional.isEmpty()) {
32        Log.infof("The user %s does not exist, creating...", user.getUsername());
33        return userDAO.createOrUpdate(user);
34      }
35      User oldUser = oldUserOptional.get();
36  
37      String firstName = user.getFirstName() != null ? user.getFirstName() : oldUser.getFirstName();
38      String lastName = user.getLastName() != null ? user.getLastName() : oldUser.getLastName();
39      String email = user.getEmail() != null ? user.getEmail() : oldUser.getEmail();
40  
41      if (
42        !firstName.equals(oldUser.getFirstName()) ||
43        !lastName.equals(oldUser.getLastName()) ||
44        !email.equals(oldUser.getEmail())
45      ) {
46        oldUser.setFirstName(firstName);
47        oldUser.setLastName(lastName);
48        oldUser.setEmail(email);
49        Log.infof("Update user %s", oldUser);
50        return userDAO.createOrUpdate(oldUser);
51      }
52  
53      return oldUser;
54    }
55  
56    /**
57     * Returns the user with the given name.
58     *
59     * @param username of the user to be returned.
60     * @return The requested user.
61     * @throws InvalidPathException if the user does not exist
62     */
63    public User getUser(String username) {
64      return getUserOptional(username).orElseThrow(() ->
65        new InvalidPathException(String.format("User with name %s not found", username))
66      );
67    }
68  
69    /**
70     * Returns the user with the given name if present
71     *
72     * @param username of the user to be returned
73     * @return An optional containing the user if it exists
74     */
75    public Optional<User> getUserOptional(String username) {
76      return Optional.ofNullable(userDAO.find(username));
77    }
78  
79    /**
80     * @return the user object for the user sending the request
81     */
82    public User getCurrentUser() {
83      User currentUser = userDAO.find(authenticationContext.getCurrentUserName());
84  
85      if (currentUser == null) {
86        String errorMsg = "Could not determine current user";
87        Log.error(errorMsg);
88        throw new InvalidRequestException(errorMsg);
89      }
90      return currentUser;
91    }
92  
93    /**
94     * @throws InvalidAuthException if the username does not match the user making the request
95     */
96    public void assertCurrentUserEquals(String username) {
97      if (!authenticationContext.getCurrentUserName().equals(username)) {
98        throw new InvalidAuthException("The requested action is forbidden by the permission policies");
99      }
100   }
101 }