renaming/constant instead of literal/unused items
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / auth / AAIUser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.auth;
23
24 import java.util.*;
25
26 public class AAIUser {
27
28     private String username;
29
30     private boolean isWildcard = false;
31     private Set<String> roles;
32     private Map<String, Set<String>> aaiFunctionToHttpMethod;
33
34     public AAIUser(String username) {
35         this(username, false);
36     }
37
38     public AAIUser(String username, boolean isWildcard) {
39         this.username = username;
40         this.roles = new HashSet<>();
41         this.aaiFunctionToHttpMethod = new HashMap<>();
42         this.isWildcard = isWildcard;
43     }
44
45     public boolean isWildcard() {
46         return isWildcard;
47     }
48
49     public String getUsername() {
50         return username;
51     }
52
53     public void addRole(String role) {
54         this.roles.add(role);
55     }
56
57     public boolean hasRole(String role) {
58         return this.roles.contains(role);
59     }
60
61     public void setUserAccess(String aaiMethod, String... httpMethods) {
62         for (String httpMethod : httpMethods) {
63             this.addUserAccess(aaiMethod, httpMethod);
64         }
65     }
66
67     private void addUserAccess(String aaiMethod, String httpMethod) {
68         Set<String> httpMethods = new HashSet<>();
69         if (this.aaiFunctionToHttpMethod.containsKey(aaiMethod)) {
70             httpMethods = this.aaiFunctionToHttpMethod.get(aaiMethod);
71         }
72         httpMethods.add(httpMethod);
73         this.aaiFunctionToHttpMethod.put(aaiMethod, httpMethods);
74     }
75
76     public boolean hasAccess(String aaiMethod, String httpMethod) {
77         return this.aaiFunctionToHttpMethod.getOrDefault(aaiMethod, Collections.emptySet()).contains(httpMethod);
78     }
79
80 }