Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / model / EcompUserDetails.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.model;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.onap.portalsdk.core.restful.domain.EcompRole;
28 import org.onap.portalsdk.core.restful.domain.EcompUser;
29 import org.springframework.security.core.GrantedAuthority;
30 import org.springframework.security.core.authority.SimpleGrantedAuthority;
31 import org.springframework.security.core.userdetails.UserDetails;
32
33 public class EcompUserDetails implements UserDetails {
34
35         private static final long serialVersionUID = 1L;
36         // use transient per Sonar warning
37         private final transient EcompUser ecompUser;
38
39         // This is the default Spring role-name prefix.
40         private static final String ROLEP = "ROLE_";
41
42         public EcompUserDetails(EcompUser ecompUser) {
43                 this.ecompUser = ecompUser;
44         }
45
46         /*
47          * Gets a list of authorities (roles) for this user. To keep Spring happy, every
48          * item has prefix ROLE_.
49          */
50         public Collection<? extends GrantedAuthority> getAuthorities() {
51                 List<GrantedAuthority> roleList = new ArrayList<>();
52                 Iterator<EcompRole> roleIter = ecompUser.getRoles().iterator();
53                 while (roleIter.hasNext()) {
54                         EcompRole role = roleIter.next();
55                         // Add the prefix if the ONAP portal doesn't supply it.
56                         final String roleName = role.getName().startsWith(ROLEP) ? role.getName() : ROLEP + role.getName();
57                         roleList.add(new SimpleGrantedAuthority(roleName));
58                 }
59                 return roleList;
60         }
61
62         public String getPassword() {
63                 return null;
64         }
65
66         public String getUsername() {
67                 return ecompUser.getLoginId();
68         }
69
70         public boolean isAccountNonExpired() {
71                 return true;
72         }
73
74         public boolean isAccountNonLocked() {
75                 return true;
76         }
77
78         public boolean isCredentialsNonExpired() {
79                 return true;
80         }
81
82         public boolean isEnabled() {
83                 return ecompUser.isActive();
84         }
85
86         @Override
87         public String toString() {
88                 return this.getClass().getSimpleName() + "[ecompUser=" + ecompUser + ", isAccountNonExpired="
89                                 + isAccountNonExpired() + ", isAccountNonLocked=" + isAccountNonLocked() + ", isCredentialsNonExpired="
90                                 + isCredentialsNonExpired() + "]";
91         }
92
93 }