summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/com/att/dao/aaf/hl/PermLookup.java
blob: 522db0adc57418c4b47cf21f273cc411dbb2c460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.dao.aaf.hl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.att.authz.env.AuthzTrans;
import com.att.authz.layer.Result;
import com.att.dao.aaf.cass.PermDAO;
import com.att.dao.aaf.cass.RoleDAO;
import com.att.dao.aaf.cass.Status;
import com.att.dao.aaf.cass.UserRoleDAO;

/**
 * PermLookup is a Storage class for the various pieces of looking up Permission 
 * during Transactions to avoid duplicate processing
 * 
 *
 */
// Package on purpose
class PermLookup {
	private AuthzTrans trans;
	private String user;
	private Question q;
	private Result<List<UserRoleDAO.Data>> userRoles = null;
	private Result<List<RoleDAO.Data>> roles = null;
	private Result<Set<String>> permNames = null;
	private Result<List<PermDAO.Data>> perms = null;
	
	private PermLookup() {}
	
	static PermLookup get(AuthzTrans trans, Question q, String user) {
		PermLookup lp=null;
		Map<String, PermLookup> permMap = trans.get(Question.PERMS, null);
		if (permMap == null) {
			trans.put(Question.PERMS, permMap = new HashMap<String, PermLookup>());
		} else {
			lp = permMap.get(user);
		}

		if (lp == null) {
			lp = new PermLookup();
			lp.trans = trans;
			lp.user = user;
			lp.q = q;
			permMap.put(user, lp);
		}
		return lp;
	}
	
	public Result<List<UserRoleDAO.Data>> getUserRoles() {
		if(userRoles==null) {
			userRoles = q.userRoleDAO.readByUser(trans,user);
			if(userRoles.isOKhasData()) {
				List<UserRoleDAO.Data> lurdd = new ArrayList<UserRoleDAO.Data>();
				Date now = new Date();
				for(UserRoleDAO.Data urdd : userRoles.value) {
					if(urdd.expires.after(now)) { // Remove Expired
						lurdd.add(urdd);
					}
				}
				if(lurdd.size()==0) {
					return userRoles = Result.err(Status.ERR_UserNotFound,
								"%s not found or not associated with any Roles: ",
								user);
				} else {
					return userRoles = Result.ok(lurdd);
				}
			} else {
				return userRoles;
			}
		} else {
			return userRoles;
		}
	}

	public Result<List<RoleDAO.Data>> getRoles() {
		if(roles==null) {
			Result<List<UserRoleDAO.Data>> rur = getUserRoles();
			if(rur.isOK()) {
				List<RoleDAO.Data> lrdd = new ArrayList<RoleDAO.Data>();
				for (UserRoleDAO.Data urdata : rur.value) {
					// Gather all permissions from all Roles
					    if(urdata.ns==null || urdata.rname==null) {
					    	trans.error().printf("DB Content Error: nulls in User Role %s %s", urdata.user,urdata.role);
					    } else {
							Result<List<RoleDAO.Data>> rlrd = q.roleDAO.read(
									trans, urdata.ns, urdata.rname);
							if(rlrd.isOK()) {
								lrdd.addAll(rlrd.value);
							}
					    }
					}
				return roles = Result.ok(lrdd);
			} else {
				return roles = Result.err(rur);
			}
		} else {
			return roles;
		}
	}

	public Result<Set<String>> getPermNames() {
		if(permNames==null) {
			Result<List<RoleDAO.Data>> rlrd = getRoles();
			if (rlrd.isOK()) {
				Set<String> pns = new TreeSet<String>();
				for (RoleDAO.Data rdata : rlrd.value) {
					pns.addAll(rdata.perms(false));
				}
				return permNames = Result.ok(pns);
			} else {
				return permNames = Result.err(rlrd);
			}
		} else {
			return permNames;
		}
	}
	
	public Result<List<PermDAO.Data>> getPerms(boolean lookup) {
		if(perms==null) {
			// Note: It should be ok for a Valid user to have no permissions -
			// 8/12/2013
			Result<Set<String>> rss = getPermNames();
			if(rss.isOK()) {
				List<PermDAO.Data> lpdd = new ArrayList<PermDAO.Data>();
				for (String perm : rss.value) {
					if(lookup) {
						Result<String[]> ap = PermDAO.Data.decodeToArray(trans, q, perm);
						if(ap.isOK()) {
							Result<List<PermDAO.Data>> rlpd = q.permDAO.read(perm,trans,ap);
							if (rlpd.isOKhasData()) {
								for (PermDAO.Data pData : rlpd.value) {
									lpdd.add(pData);
								}
							}
						} else {
							trans.error().log("In getPermsByUser, for", user, perm);
						}
					} else {
						Result<PermDAO.Data> pr = PermDAO.Data.decode(trans, q, perm);
						if (pr.notOK()) {
							trans.error().log("In getPermsByUser, for", user, pr.errorString());
						} else {
							lpdd.add(pr.value);
						}
					}

				}
				return perms = Result.ok(lpdd);
			} else {
				return perms = Result.err(rss);
			}
		} else {
			return perms;
		}
	}
}