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