3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dcaegen2.platform.mod.security.services;
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;
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;
46 * User Details Service
49 public class UserDetailsServiceImpl implements UserDetailsService {
53 UserRepository userRepository;
56 PasswordEncoder passwordEncoder;
59 AuthController authController;
62 private JwtUtils jwtUtils;
65 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
66 ModUser user = userRepository.findByUsername(username)
67 .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));
69 return UserDetailsImpl.build(user);
72 public ModUser adminUpdateUser(String username, UpdateUserRequest userRequest, String token) {
73 return updateUserProfile(username, userRequest);
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);
81 throw new IllegalUserOperationException("Permission denied to update user profile of " + username);
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);
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);