[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / User.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi;\r
25 \r
26 import java.security.Principal;\r
27 import java.util.HashMap;\r
28 import java.util.List;\r
29 import java.util.Map;\r
30 import java.util.concurrent.ConcurrentHashMap;\r
31 \r
32 import com.att.cadi.lur.LocalPermission;\r
33 \r
34 /**\r
35  * Class to hold info from the User Perspective.\r
36  * \r
37  *\r
38  */\r
39 public final class User<PERM extends Permission> {\r
40         private static Map<String,Permission> NULL_MAP = new HashMap<String,Permission>();\r
41         public Principal principal;\r
42         Map<String, Permission> perms ;\r
43         long permExpires;\r
44         private final long interval;\r
45         int count;\r
46         \r
47         // Note: This should only be used for Local RBAC (in memory)\r
48         public User(Principal principal) {\r
49                 this.principal = principal;\r
50                 perms = NULL_MAP;\r
51                 permExpires = Long.MAX_VALUE; // Never.  Well, until 64 bits of millis since 1970 expires...\r
52                 interval = 0L;\r
53                 count = 0;\r
54         }\r
55 \r
56         public User(Principal principal, long expireInterval) {\r
57                 this.principal = principal;\r
58                 perms = NULL_MAP;\r
59                 expireInterval = Math.max(expireInterval, 0); // avoid < 1\r
60                 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));\r
61                 permExpires = 0;\r
62                 count = 0;\r
63         }\r
64         \r
65         public void renewPerm() {\r
66                 permExpires = System.currentTimeMillis()+interval;\r
67         }\r
68         \r
69         public long permExpires() {\r
70                 return permExpires;\r
71         }\r
72         \r
73         public boolean permExpired() {\r
74                 return System.currentTimeMillis() > permExpires;\r
75         }\r
76 \r
77         public boolean noPerms() {\r
78                 return perms==null || perms.values().size()==0; \r
79         }\r
80         \r
81         public void setNoPerms() {\r
82                 perms=NULL_MAP;\r
83                 permExpires = System.currentTimeMillis() + interval;\r
84         }\r
85 \r
86         public boolean permsUnloaded() {\r
87                 return perms==null;\r
88         }\r
89 \r
90         public synchronized void incCount() {\r
91                 ++count;\r
92         }\r
93         \r
94         public synchronized void resetCount() {\r
95                 count=0;\r
96         }\r
97         \r
98         public Map<String,Permission> newMap() {\r
99                 return new ConcurrentHashMap<String,Permission>();\r
100         }\r
101 \r
102         public void add(LocalPermission permission) {\r
103                 if(perms==NULL_MAP)perms=newMap();\r
104                 perms.put(permission.getKey(),permission);\r
105         }\r
106 \r
107         public void add(Map<String, Permission> newMap, PERM permission) {\r
108                 newMap.put(permission.getKey(),permission);\r
109         }\r
110 \r
111         public void setMap(Map<String, Permission> newMap) {\r
112                 perms = newMap;\r
113         }\r
114 \r
115         public boolean contains(Permission perm) {\r
116                 for (Permission p : perms.values()) {\r
117                         if (p.match(perm)) return true;\r
118                 }\r
119                 return false;\r
120         }\r
121         \r
122         public void copyPermsTo(List<Permission> sink) {\r
123                 sink.addAll(perms.values());\r
124         }\r
125         \r
126         public String toString() {\r
127                 StringBuilder sb = new StringBuilder();\r
128                 sb.append(principal.getName());\r
129                 sb.append('|');\r
130                 boolean first = true;\r
131                 synchronized(perms) {\r
132                         for(Permission gp : perms.values()) {\r
133                                 if(first) {\r
134                                         first = false;\r
135                                         sb.append(':');\r
136                                 } else {\r
137                                         sb.append(',');\r
138                                 }\r
139                                 sb.append(gp.getKey());\r
140                         }\r
141                 }\r
142                 return sb.toString();\r
143         }\r
144 \r
145 }\r