Merge "adding catalog-service for mod2"
[dcaegen2/platform.git] / mod2 / auth-service / src / main / java / org / onap / dcaegen2 / platform / mod / security / services / UserDetailsImpl.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.models.ModUser;
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27 import lombok.EqualsAndHashCode;
28 import org.springframework.security.core.GrantedAuthority;
29 import org.springframework.security.core.authority.SimpleGrantedAuthority;
30 import org.springframework.security.core.userdetails.UserDetails;
31
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.stream.Collectors;
35
36 /**
37  * @author
38  * @date 09/08/2020
39  * User Details Implementation
40  */
41 @EqualsAndHashCode
42 public class UserDetailsImpl implements UserDetails{
43
44     private static final long serialVersionUID = 1L;
45
46     private String id;
47
48     private String username;
49
50     private String fullName;
51
52     @JsonIgnore
53     private String password;
54
55     private Collection<? extends GrantedAuthority> authorities;
56
57     public UserDetailsImpl(String id, String username, String fullName, String password, Collection<?
58             extends GrantedAuthority> authorities) {
59         this.id = id;
60         this.username = username;
61         this.fullName = fullName;
62         this.password = password;
63         this.authorities = authorities;
64     }
65
66     public static UserDetails build(ModUser user) {
67         List<GrantedAuthority> authorities = user.getRoles().stream()
68                 .map(role -> new SimpleGrantedAuthority(role.getName()))
69                 .collect(Collectors.toList());
70
71         return new UserDetailsImpl(
72                 user.get_id(),
73                 user.getUsername(),
74                 user.getFullName(),
75                 user.getPassword(),
76                 authorities
77         );
78     }
79
80     public String getId() {
81         return id;
82     }
83
84     @Override
85     public Collection<? extends GrantedAuthority> getAuthorities() {
86         return authorities;
87     }
88
89     public List<String> getAuthoritiesAsList(){
90         return  authorities.stream().map(GrantedAuthority::getAuthority)
91                     .collect(Collectors.toList());
92     }
93
94     @Override
95     public String getPassword() {
96         return password;
97     }
98
99     @Override
100     public String getUsername() {
101         return username;
102     }
103
104     public String getFullName() {
105         return fullName;
106     }
107
108     @Override
109     public boolean isAccountNonExpired() {
110         return true;
111     }
112
113     @Override
114     public boolean isAccountNonLocked() {
115         return true;
116     }
117
118     @Override
119     public boolean isCredentialsNonExpired() {
120         return true;
121     }
122
123     @Override
124     public boolean isEnabled() {
125         return true;
126     }
127 }