Frontend EI Coordinator
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / model / EcompUserDetails.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
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 package org.oransc.portal.nonrtric.controlpanel.model;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.onap.portalsdk.core.restful.domain.EcompRole;
29 import org.onap.portalsdk.core.restful.domain.EcompUser;
30 import org.springframework.security.core.GrantedAuthority;
31 import org.springframework.security.core.authority.SimpleGrantedAuthority;
32 import org.springframework.security.core.userdetails.UserDetails;
33
34 public class EcompUserDetails implements UserDetails {
35
36     private static final long serialVersionUID = 1L;
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     @Override
51     public Collection<? extends GrantedAuthority> getAuthorities() {
52         List<GrantedAuthority> roleList = new ArrayList<>();
53         Iterator<EcompRole> roleIter = ecompUser.getRoles().iterator();
54         while (roleIter.hasNext()) {
55             EcompRole role = roleIter.next();
56             // Add the prefix if the ONAP portal doesn't supply it.
57             final String roleName = role.getName().startsWith(ROLEP) ? role.getName() : ROLEP + role.getName();
58             roleList.add(new SimpleGrantedAuthority(roleName));
59         }
60         return roleList;
61     }
62
63     @Override
64     public String getPassword() {
65         return null;
66     }
67
68     @Override
69     public String getUsername() {
70         return ecompUser.getLoginId();
71     }
72
73     @Override
74     public boolean isAccountNonExpired() {
75         return true;
76     }
77
78     @Override
79     public boolean isAccountNonLocked() {
80         return true;
81     }
82
83     @Override
84     public boolean isCredentialsNonExpired() {
85         return true;
86     }
87
88     @Override
89     public boolean isEnabled() {
90         return ecompUser.isActive();
91     }
92
93 }