Enable Organizations to have a subset of users the user roles of which do not expire
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / hl / PermLookup.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
22 package org.onap.aaf.auth.dao.hl;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
32
33 import org.onap.aaf.auth.dao.cass.PermDAO;
34 import org.onap.aaf.auth.dao.cass.RoleDAO;
35 import org.onap.aaf.auth.dao.cass.Status;
36 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.layer.Result;
39
40 /**
41  * PermLookup is a Storage class for the various pieces of looking up Permission
42  * during Transactions to avoid duplicate processing
43  *
44  * @author Jonathan
45  *
46  */
47 // Package on purpose
48 public class PermLookup {
49     private AuthzTrans trans;
50     private String user;
51     private Question q;
52     private Result<List<UserRoleDAO.Data>> userRoles = null;
53     private Result<List<RoleDAO.Data>> roles = null;
54     private Result<Set<String>> permNames = null;
55     private Result<List<PermDAO.Data>> perms = null;
56
57     private PermLookup() {}
58
59     public static PermLookup get(AuthzTrans trans, Question q, String user) {
60         PermLookup lp=null;
61         Map<String, PermLookup> permMap = trans.get(Question.PERMS, null);
62         if (permMap == null) {
63             trans.put(Question.PERMS, permMap = new HashMap<>());
64         } else {
65             lp = permMap.get(user);
66         }
67
68         if (lp == null) {
69             lp = new PermLookup();
70             lp.trans = trans;
71             lp.user = user;
72             lp.q = q;
73             permMap.put(user, lp);
74         }
75         return lp;
76     }
77
78     public Result<List<UserRoleDAO.Data>> getUserRoles() {
79         if (userRoles==null) {
80             userRoles = q.userRoleDAO().readByUser(trans,user);
81             if (userRoles.isOKhasData()) {
82                 List<UserRoleDAO.Data> lurdd = new ArrayList<>();
83                 Date now = new Date();
84                 for (UserRoleDAO.Data urdd : userRoles.value) {
85                     if (urdd.expires.after(now) || trans.org().isUserExpireExempt(user, urdd.expires)) { // Remove Expired
86                         lurdd.add(urdd);
87                     }
88                 }
89                 if (lurdd.size()==0) {
90                     return userRoles = Result.err(Status.ERR_UserNotFound,
91                                 "%s not found or not associated with any Roles: ",
92                                 user);
93                 } else {
94                     return userRoles = Result.ok(lurdd);
95                 }
96             } else {
97                 return userRoles;
98             }
99         } else {
100             return userRoles;
101         }
102     }
103
104     public Result<List<RoleDAO.Data>> getRoles() {
105         if (roles==null) {
106             Result<List<UserRoleDAO.Data>> rur = getUserRoles();
107             if (rur.isOK()) {
108                 List<RoleDAO.Data> lrdd = new ArrayList<>();
109                 for (UserRoleDAO.Data urdata : rur.value) {
110                     // Gather all permissions from all Roles
111                         if (urdata.ns==null || urdata.rname==null) {
112                             return Result.err(Status.ERR_BadData,"DB Content Error: nulls in User Role %s %s", urdata.user,urdata.role);
113                         } else {
114                             Result<List<RoleDAO.Data>> rlrd = q.roleDAO().read(
115                                     trans, urdata.ns, urdata.rname);
116                             if (rlrd.isOK()) {
117                                 lrdd.addAll(rlrd.value);
118                             }
119                         }
120                     }
121                 return roles = Result.ok(lrdd);
122             } else {
123                 return roles = Result.err(rur);
124             }
125         } else {
126             return roles;
127         }
128     }
129
130     public Result<Set<String>> getPermNames() {
131         if (permNames==null) {
132             Result<List<RoleDAO.Data>> rlrd = getRoles();
133             if (rlrd.isOK()) {
134                 Set<String> pns = new TreeSet<>();
135                 for (RoleDAO.Data rdata : rlrd.value) {
136                     pns.addAll(rdata.perms(false));
137                 }
138                 return permNames = Result.ok(pns);
139             } else {
140                 return permNames = Result.err(rlrd);
141             }
142         } else {
143             return permNames;
144         }
145     }
146
147     public Result<List<PermDAO.Data>> getPerms(boolean lookup) {
148         if (perms==null) {
149             // Note: It should be ok for a Valid user to have no permissions -
150             // Jonathan 8/12/2013
151             Result<Set<String>> rss = getPermNames();
152             if (rss.isOK()) {
153                 List<PermDAO.Data> lpdd = new ArrayList<>();
154                 for (String perm : rss.value) {
155                     if (lookup) {
156                         Map<String,PermDAO.Data> mspdd = new TreeMap<>();
157                         Result<String[]> ap = PermDAO.Data.decodeToArray(trans, q, perm);
158                         if (ap.isOK()) {
159
160                             Result<List<PermDAO.Data>> rlpd = q.permDAO().read(perm,trans,ap.value);
161                             if (rlpd.isOKhasData()) {
162                                 for (PermDAO.Data pData : rlpd.value) {
163                                     // ONLY add perms/roles which are related to this lookup
164                                     for(String pdr : pData.roles(false)) {
165                                         for(RoleDAO.Data r : roles.value) {
166                                             if(pdr.equals(r.encode())) {
167                                                 PermDAO.Data pdd = mspdd.get(pData.fullPerm());
168                                                 if(pdd==null) {
169                                                     pdd = new PermDAO.Data();
170                                                     pdd.ns = pData.ns;
171                                                     pdd.type = pData.type;
172                                                     pdd.instance = pData.instance;
173                                                     pdd.action = pData.action;
174                                                     pdd.description = pData.description;
175                                                     lpdd.add(pdd);
176                                                 }
177                                                 pdd.roles(true).add(pdr);
178                                                 break;
179                                             }
180                                         }
181                                     }
182                                 }
183                             }
184                         } else {
185                             trans.error().log("In getPermsByUser, for", user, perm);
186                         }
187                     } else {
188                         Result<PermDAO.Data> pr = PermDAO.Data.decode(trans, q, perm);
189                         if (pr.notOK()) {
190                             trans.error().log("In getPermsByUser, for", user, pr.errorString());
191                         } else {
192                             lpdd.add(pr.value);
193                         }
194                     }
195
196                 }
197                 return perms = Result.ok(lpdd);
198             } else {
199                 return perms = Result.err(rss);
200             }
201         } else {
202             return perms;
203         }
204     }
205 }