76336700c632b51ab2920d7369f01d43c8efab2e
[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
33 /**
34  * We treat "roles" and "permissions" in a similar way for first pass.
35  * 
36  * @author JonathanGathman
37  *
38  */
39 public class AAFAuthorizationInfo implements AuthorizationInfo {
40         private static final long serialVersionUID = -4805388954462426018L;
41         private Access access;
42         private Principal bait;
43         private List<org.onap.aaf.cadi.Permission> pond;
44         private ArrayList<String> sPerms;
45         private ArrayList<Permission> oPerms;
46
47         public AAFAuthorizationInfo(Access access, Principal bait, List<org.onap.aaf.cadi.Permission> pond) {
48                 this.access = access;
49                 this.bait = bait;
50                 this.pond = pond;
51                 sPerms=null;
52                 oPerms=null;
53         }
54         
55         public Principal principal() {
56                 return bait;
57         }
58         
59         @Override
60         public Collection<Permission> getObjectPermissions() {
61 //              access.log(Level.DEBUG, "AAFAuthorizationInfo.getObjectPermissions");
62                 synchronized(bait) {
63                         if(oPerms == null) {
64                                 oPerms = new ArrayList<Permission>(); 
65                                 for(final org.onap.aaf.cadi.Permission p : pond) {
66                                         oPerms.add(new AAFShiroPermission(p));
67                                         System.out.println("List user" + p); 
68                                 }
69                         }
70                 }
71                 return oPerms;
72         }
73
74         @Override
75         public Collection<String> getRoles() {
76 //              access.log(Level.DEBUG, "AAFAuthorizationInfo.getRoles");
77                 // Until we decide to make Roles available, tie into String based permissions.
78                 return getStringPermissions();
79         }
80
81         @Override
82         public Collection<String> getStringPermissions() {
83 //              access.log(Level.DEBUG, "AAFAuthorizationInfo.getStringPermissions");
84                 synchronized(bait) {
85                         if(sPerms == null) {
86                                 sPerms = new ArrayList<String>(); 
87                                 for(org.onap.aaf.cadi.Permission p : pond) {
88                                         sPerms.add(p.getKey().replace("|",":"));
89                                         System.out.println("Replacing | to :" + p.getKey().replace("|",":")); 
90                                 }
91                         }
92                 }
93                 return sPerms;
94         }
95
96 }