fix shiro logs looping issue
[aaf/cadi.git] / shiro / src / main / java / org / onap / aaf / cadi / shiro / AAFAuthorizationInfo.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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 package org.onap.aaf.cadi.shiro;
22
23 import java.security.Principal;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.apache.shiro.authz.AuthorizationInfo;
29 import org.apache.shiro.authz.Permission;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * We treat "roles" and "permissions" in a similar way for first pass.
37  * 
38  * @author JonathanGathman
39  *
40  */
41 public class AAFAuthorizationInfo implements AuthorizationInfo {
42         private static final long serialVersionUID = -4805388954462426018L;
43         
44         final static  Logger logger =  LoggerFactory.getLogger(AAFAuthorizationInfo.class);
45         
46         private Access access;
47         private Principal bait;
48         private List<org.onap.aaf.cadi.Permission> pond;
49         private ArrayList<String> sPerms;
50         private ArrayList<Permission> oPerms;
51
52         public AAFAuthorizationInfo(Access access, Principal bait, List<org.onap.aaf.cadi.Permission> pond) {
53                 this.access = access;
54                 this.bait = bait;
55                 this.pond = pond;
56                 sPerms=null;
57                 oPerms=null;
58
59         }
60         
61         public Principal principal() {
62                 return bait;
63         }
64         
65         @Override
66         public Collection<Permission> getObjectPermissions() {
67 //              logger.info("AAFAuthorizationInfo.getObjectPermissions");
68                 synchronized(bait) {
69                         if(oPerms == null) {
70                                 oPerms = new ArrayList<Permission>(); 
71                                 for(final org.onap.aaf.cadi.Permission p : pond) {
72                                         oPerms.add(new AAFShiroPermission(p));
73                                 }
74                         }
75                 }
76                 
77                 
78                 return oPerms;
79         }
80
81         @Override
82         public Collection<String> getRoles() {
83 //              logger.info("AAFAuthorizationInfo.getRoles");
84                 // Until we decide to make Roles available, tie into String based permissions.
85                 return getStringPermissions();
86         }
87
88         @Override
89         public Collection<String> getStringPermissions() {
90        
91 //              logger.info("AAFAuthorizationInfo.getStringPermissions");
92                 synchronized(bait) {
93                         if(sPerms == null) {
94                                 sPerms = new ArrayList<String>(); 
95                                 for(org.onap.aaf.cadi.Permission p : pond) {
96                                         sPerms.add(p.getKey().replace("|",":"));
97                                         logger.info("the user has  " +p.getKey());
98                                         
99                                 }
100                         }
101                 }
102                 return sPerms;
103         }
104
105 }