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