Merge "Refactor, fix code formatting and add unittests"
[dcaegen2/platform.git] / mod2 / auth-service / src / main / java / org / onap / dcaegen2 / platform / mod / security / services / UserDetailsServiceImpl.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
7  *  *  ================================================================================
8  *  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  *  you may not use this file except in compliance with the License.
10  *  *  You may obtain a copy of the License at
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
14  *  *  Unless required by applicable law or agreed to in writing, software
15  *  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  *  See the License for the specific language governing permissions and
18  *  *  limitations under the License.
19  *  *  ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.onap.dcaegen2.platform.mod.security.services;
24
25 import org.onap.dcaegen2.platform.mod.controllers.AuthController;
26 import org.onap.dcaegen2.platform.mod.exceptions.UserNotFoundException;
27 import org.onap.dcaegen2.platform.mod.exceptions.IllegalUserOperationException;
28 import org.onap.dcaegen2.platform.mod.models.ModUser;
29 import org.onap.dcaegen2.platform.mod.models.Role;
30 import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
31 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
32 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
33 import lombok.Setter;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.security.core.userdetails.UserDetails;
36 import org.springframework.security.core.userdetails.UserDetailsService;
37 import org.springframework.security.core.userdetails.UsernameNotFoundException;
38 import org.springframework.security.crypto.password.PasswordEncoder;
39 import org.springframework.stereotype.Service;
40
41 import java.util.Set;
42
43 /**
44  * @author
45  * @date 09/08/2020
46  * User Details Service
47  */
48 @Service
49 public class UserDetailsServiceImpl implements UserDetailsService {
50
51     @Autowired
52     @Setter
53     UserRepository userRepository;
54
55     @Autowired
56     PasswordEncoder passwordEncoder;
57
58     @Autowired
59     AuthController authController;
60
61     @Autowired
62     private JwtUtils jwtUtils;
63
64     @Override
65     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
66         ModUser user = userRepository.findByUsername(username)
67                 .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));
68
69         return UserDetailsImpl.build(user);
70     }
71
72     public ModUser adminUpdateUser(String username, UpdateUserRequest userRequest, String token) {
73         return updateUserProfile(username, userRequest);
74     }
75
76     public ModUser userUpdateOwnProfile(String username, UpdateUserRequest userRequest, String token) {
77         String usernameFromToken = jwtUtils.getUserNameFromJwtToken(token.substring(7));
78         if (usernameFromToken.equals(username)) {
79             return updateUserProfile(username, userRequest);
80         } else {
81             throw new IllegalUserOperationException("Permission denied to update user profile of " + username);
82         }
83     }
84
85     private ModUser updateUserProfile(String username, UpdateUserRequest userRequest) {
86         ModUser modUser = userRepository.findByUsername(username).orElseThrow(() -> new UserNotFoundException(String.format("User %s not found", username)));
87         modUser = updateUserFields(modUser, userRequest);
88         return userRepository.save(modUser);
89     }
90
91     private ModUser updateUserFields(ModUser modUser, UpdateUserRequest userRequest) {
92         if (userRequest.getFullName() != null) modUser.setFullName(userRequest.getFullName());
93         if (userRequest.getPassword() != null) modUser.setPassword(passwordEncoder.encode(userRequest.getPassword()));
94         if (userRequest.getRoles() != null) {
95             Set<Role> roles = authController.createRoles(userRequest.getRoles());
96             modUser.setRoles(roles);
97         }
98         return modUser;
99     }
100 }