d0f7456e43b9b0f37348f107ea1a3e600cf846fe
[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  * 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.onap.aai.auth;
21
22 import java.util.*;
23
24 public class AAIUser {
25
26         private String username;
27
28         private boolean isWildcard = false;
29         private Set<String> roles;
30         private Map<String, Set<String>> aaiFunctionToHttpMethod;
31
32         public AAIUser(String username) {
33                 this(username, false);
34         }
35
36         public AAIUser(String username, boolean isWildcard) {
37                 this.username = username;
38                 this.roles = new HashSet<>();
39                 this.aaiFunctionToHttpMethod = new HashMap<>();
40                 this.isWildcard = isWildcard;
41         }
42
43         public boolean isWildcard() {
44                 return isWildcard;
45         }
46
47         public String getUsername() {
48                 return username;
49         }
50
51         public void addRole(String role) {
52                 this.roles.add(role);
53         }
54
55         public boolean hasRole(String role) {
56                 return this.roles.contains(role);
57         }
58
59         public void setUserAccess(String aaiMethod, String... httpMethods) {
60                 for (String httpMethod : httpMethods) {
61                         this.addUserAccess(aaiMethod, httpMethod);
62                 }
63         }
64
65         private void addUserAccess(String aaiMethod, String httpMethod) {
66                 Set<String> httpMethods = new HashSet<>();
67                 if (this.aaiFunctionToHttpMethod.containsKey(aaiMethod)) {
68                         httpMethods = this.aaiFunctionToHttpMethod.get(aaiMethod);
69                 }
70                 httpMethods.add(httpMethod);
71                 this.aaiFunctionToHttpMethod.put(aaiMethod, httpMethods);
72         }
73
74         public boolean hasAccess(String aaiMethod, String httpMethod) {
75                 return this.aaiFunctionToHttpMethod.getOrDefault(aaiMethod, Collections.EMPTY_SET).contains(httpMethod);
76         }
77
78 }