summaryrefslogtreecommitdiffstats
path: root/mod2/auth-service/src/main/java/org/onap/dcaegen2/platform/mod/security/services/UserDetailsServiceImpl.java
blob: 5c13e11609f2e4953618fe6301c5a9171b468867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *
 *  * ============LICENSE_START=======================================================
 *  *  org.onap.dcae
 *  *  ================================================================================
 *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
 *  *  ================================================================================
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  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.
 *  *  ============LICENSE_END=========================================================
 *
 */

package org.onap.dcaegen2.platform.mod.security.services;

import org.onap.dcaegen2.platform.mod.controllers.AuthController;
import org.onap.dcaegen2.platform.mod.exceptions.UserNotFoundException;
import org.onap.dcaegen2.platform.mod.exceptions.IllegalUserOperationException;
import org.onap.dcaegen2.platform.mod.models.ModUser;
import org.onap.dcaegen2.platform.mod.models.Role;
import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author
 * @date 09/08/2020
 * User Details Service
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    @Setter
    UserRepository userRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    AuthController authController;

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ModUser user = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));

        return UserDetailsImpl.build(user);
    }

    public ModUser adminUpdateUser(String username, UpdateUserRequest userRequest, String token) {
        return updateUserProfile(username, userRequest);
    }

    public ModUser userUpdateOwnProfile(String username, UpdateUserRequest userRequest, String token) {
        String usernameFromToken = jwtUtils.getUserNameFromJwtToken(token.substring(7));
        if (usernameFromToken.equals(username)) {
            return updateUserProfile(username, userRequest);
        } else {
            throw new IllegalUserOperationException("Permission denied to update user profile of " + username);
        }
    }

    private ModUser updateUserProfile(String username, UpdateUserRequest userRequest) {
        ModUser modUser = userRepository.findByUsername(username).orElseThrow(() -> new UserNotFoundException(String.format("User %s not found", username)));
        modUser = updateUserFields(modUser, userRequest);
        return userRepository.save(modUser);
    }

    private ModUser updateUserFields(ModUser modUser, UpdateUserRequest userRequest) {
        if (userRequest.getFullName() != null) modUser.setFullName(userRequest.getFullName());
        if (userRequest.getPassword() != null) modUser.setPassword(passwordEncoder.encode(userRequest.getPassword()));
        if (userRequest.getRoles() != null) {
            Set<Role> roles = authController.createRoles(userRequest.getRoles());
            modUser.setRoles(roles);
        }
        return modUser;
    }
}