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