+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi;\r
-\r
-import java.security.Principal;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.concurrent.ConcurrentHashMap;\r
-\r
-import org.onap.aaf.cadi.lur.LocalPermission;\r
-\r
-/**\r
- * Class to hold info from the User Perspective.\r
- * \r
- *\r
- */\r
-public final class User<PERM extends Permission> {\r
- private static Map<String,Permission> NULL_MAP = new HashMap<String,Permission>();\r
- public Principal principal;\r
- Map<String, Permission> perms ;\r
- long permExpires;\r
- private final long interval;\r
- int count;\r
- \r
- // Note: This should only be used for Local RBAC (in memory)\r
- public User(Principal principal) {\r
- this.principal = principal;\r
- perms = NULL_MAP;\r
- permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...\r
- interval = 0L;\r
- count = 0;\r
- }\r
-\r
- public User(Principal principal, long expireInterval) {\r
- this.principal = principal;\r
- perms = NULL_MAP;\r
- expireInterval = Math.max(expireInterval, 0); // avoid < 1\r
- interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));\r
- permExpires = 0;\r
- count = 0;\r
- }\r
- \r
- public void renewPerm() {\r
- permExpires = System.currentTimeMillis()+interval;\r
- }\r
- \r
- public long permExpires() {\r
- return permExpires;\r
- }\r
- \r
- public boolean permExpired() {\r
- return System.currentTimeMillis() > permExpires;\r
- }\r
-\r
- public boolean noPerms() {\r
- return perms==null || perms.values().size()==0; \r
- }\r
- \r
- public void setNoPerms() {\r
- perms=NULL_MAP;\r
- permExpires = System.currentTimeMillis() + interval;\r
- }\r
-\r
- public boolean permsUnloaded() {\r
- return perms==null;\r
- }\r
-\r
- public synchronized void incCount() {\r
- ++count;\r
- }\r
- \r
- public synchronized void resetCount() {\r
- count=0;\r
- }\r
- \r
- public Map<String,Permission> newMap() {\r
- return new ConcurrentHashMap<String,Permission>();\r
- }\r
-\r
- public void add(LocalPermission permission) {\r
- if(perms==NULL_MAP)perms=newMap();\r
- perms.put(permission.getKey(),permission);\r
- }\r
-\r
- public void add(Map<String, Permission> newMap, PERM permission) {\r
- newMap.put(permission.getKey(),permission);\r
- }\r
-\r
- public void setMap(Map<String, Permission> newMap) {\r
- perms = newMap;\r
- }\r
-\r
- public boolean contains(Permission perm) {\r
- for (Permission p : perms.values()) {\r
- if (p.match(perm)) return true;\r
- }\r
- return false;\r
- }\r
- \r
- public void copyPermsTo(List<Permission> sink) {\r
- sink.addAll(perms.values());\r
- }\r
- \r
- public String toString() {\r
- StringBuilder sb = new StringBuilder();\r
- sb.append(principal.getName());\r
- sb.append('|');\r
- boolean first = true;\r
- synchronized(perms) {\r
- for(Permission gp : perms.values()) {\r
- if(first) {\r
- first = false;\r
- sb.append(':');\r
- } else {\r
- sb.append(',');\r
- }\r
- sb.append(gp.getKey());\r
- }\r
- }\r
- return sb.toString();\r
- }\r
-\r
-}\r