1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi;
\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
32 import com.att.cadi.lur.LocalPermission;
\r
35 * Class to hold info from the User Perspective.
\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
44 private final long interval;
\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
51 permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...
\r
56 public User(Principal principal, long expireInterval) {
\r
57 this.principal = principal;
\r
59 expireInterval = Math.max(expireInterval, 0); // avoid < 1
\r
60 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
\r
65 public void renewPerm() {
\r
66 permExpires = System.currentTimeMillis()+interval;
\r
69 public long permExpires() {
\r
73 public boolean permExpired() {
\r
74 return System.currentTimeMillis() > permExpires;
\r
77 public boolean noPerms() {
\r
78 return perms==null || perms.values().size()==0;
\r
81 public void setNoPerms() {
\r
83 permExpires = System.currentTimeMillis() + interval;
\r
86 public boolean permsUnloaded() {
\r
90 public synchronized void incCount() {
\r
94 public synchronized void resetCount() {
\r
98 public Map<String,Permission> newMap() {
\r
99 return new ConcurrentHashMap<String,Permission>();
\r
102 public void add(LocalPermission permission) {
\r
103 if(perms==NULL_MAP)perms=newMap();
\r
104 perms.put(permission.getKey(),permission);
\r
107 public void add(Map<String, Permission> newMap, PERM permission) {
\r
108 newMap.put(permission.getKey(),permission);
\r
111 public void setMap(Map<String, Permission> newMap) {
\r
115 public boolean contains(Permission perm) {
\r
116 for (Permission p : perms.values()) {
\r
117 if (p.match(perm)) return true;
\r
122 public void copyPermsTo(List<Permission> sink) {
\r
123 sink.addAll(perms.values());
\r
126 public String toString() {
\r
127 StringBuilder sb = new StringBuilder();
\r
128 sb.append(principal.getName());
\r
130 boolean first = true;
\r
131 synchronized(perms) {
\r
132 for(Permission gp : perms.values()) {
\r
139 sb.append(gp.getKey());
\r
142 return sb.toString();
\r