Collection syntax change because of Sonar
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / User.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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  */
21
22 package org.onap.aaf.cadi;
23
24 import java.security.Principal;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.onap.aaf.cadi.lur.LocalPermission;
31
32 /**
33  * Class to hold info from the User Perspective.
34  * 
35  * @author Jonathan
36  *
37  */
38 public final class User<PERM extends Permission> {
39         private static final Map<String,Permission> NULL_MAP = new HashMap<>();
40         public String name;
41         private byte[] cred;
42         public Principal principal;
43         Map<String, Permission> perms ;
44         long permExpires;
45         private final long interval;
46         int count;
47         
48         // Note: This should only be used for Local RBAC (in memory)
49         public User(Principal principal) {
50                 this.principal = principal;
51                 name = principal.getName();
52                 perms = NULL_MAP;
53                 permExpires = Long.MAX_VALUE; // Never.  Well, until 64 bits of millis since 1970 expires...
54                 interval = 0L;
55                 count = 0;
56         }
57
58         public User(String name, byte[] cred) {
59                 this.principal = null;
60                 this.name = name;
61                 this.cred = cred;
62                 perms = NULL_MAP;
63                 permExpires = Long.MAX_VALUE; // Never.  Well, until 64 bits of millis since 1970 expires...
64                 interval = 0L;
65                 count = 0;
66         }
67
68         public User(Principal principal, long expireInterval) {
69                 this.principal = principal;
70                 this.name = principal.getName();
71                 perms = NULL_MAP;
72                 expireInterval = Math.max(expireInterval, 0); // avoid < 1
73                 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
74                 count = 0;
75                 renewPerm();
76                 renewPerm();
77         }
78
79         public User(String name, byte[] cred, long expireInterval) {
80                 this.principal = null;
81                 this.name = name;
82                 this.cred = cred;
83                 perms = NULL_MAP;
84                 expireInterval = Math.max(expireInterval, 0); // avoid < 1
85                 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
86                 count = 0;
87                 renewPerm();
88         }
89         
90         public void renewPerm() {
91                 permExpires = System.currentTimeMillis()+interval;
92         }
93         
94         public long permExpires() {
95                 return permExpires;
96         }
97         
98         public boolean permExpired() {
99                 return System.currentTimeMillis() > permExpires;
100         }
101
102         public boolean noPerms() {
103                 return perms==null || perms==NULL_MAP || perms.values().size()==0; 
104         }
105         
106         public synchronized void setNoPerms() {
107                 perms=NULL_MAP;
108                 renewPerm();
109         }
110
111         public boolean permsUnloaded() {
112                 return perms==null || perms==NULL_MAP;
113         }
114
115         public synchronized void incCount() {
116                 ++count;
117         }
118         
119         public synchronized void resetCount() {
120                 count=0;
121         }
122         
123         public Map<String,Permission> newMap() {
124                 return new ConcurrentHashMap<>();
125         }
126
127         public void add(LocalPermission permission) {
128                 if(perms==NULL_MAP) {
129                         perms=newMap();
130                 }
131                 perms.put(permission.getKey(),permission);
132         }
133
134         public void add(Map<String, Permission> newMap, PERM permission) {
135                 newMap.put(permission.getKey(),permission);
136         }
137
138         public synchronized void setMap(Map<String, Permission> newMap) {
139                 perms = newMap;
140                 renewPerm();
141         }
142
143         public boolean contains(Permission perm) {
144                 for (Permission p : perms.values()) {
145                         if (p.match(perm)) return true;
146                 }
147                 return false;
148         }
149         
150         public void copyPermsTo(List<Permission> sink) {
151                 sink.addAll(perms.values());
152         }
153         
154         public String toString() {
155                 StringBuilder sb = new StringBuilder();
156                 sb.append(principal.getName());
157                 sb.append('|');
158                 boolean first = true;
159                 synchronized(perms) {
160                         for(Permission gp : perms.values()) {
161                                 if(first) {
162                                         first = false;
163                                         sb.append(':');
164                                 } else {
165                                         sb.append(',');
166                                 }
167                                 sb.append(gp.getKey());
168                         }
169                 }
170                 return sb.toString();
171         }
172
173         public byte[] getCred() {
174                 return cred;
175         }
176
177 }