aboutsummaryrefslogtreecommitdiffstats
path: root/core/src/main/java/com/att/cadi/lur/EpiLur.java
blob: de6c057b44a137a608f7b638d408977aec204e44 (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
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aai
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * Copyright © 2017 Amdocs
 * * ===========================================================================
 * * 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.cadi.lur;

import java.security.Principal;
import java.util.List;

import com.att.cadi.CachingLur;
import com.att.cadi.CadiException;
import com.att.cadi.CredVal;
import com.att.cadi.Lur;
import com.att.cadi.Permission;

/**
 * EpiLUR
 * 
 * Short for "Epic LUR". Be able to run through a series of LURs to obtain the validation needed.
 * 
 * The pun is better for the other pattern... "TAF" (aka EpiTaf), but it's still the larger picture of 
 * LURs that will be accomplished.
 * 
 * FYI, the reason we separate LURs, rather than combine, is that Various User Repository Resources have
 * different Caching requirements.  For instance, the Local User Repo (with stand alone names), never expire, but might be
 * refreshed with a change in Configuration File, while the Remote Service based LURs will need to expire at prescribed intervals 
 * 
 *
 */
public final class EpiLur implements Lur {
	private final Lur[] lurs;
	
	/**
	 * EpiLur constructor
	 * 
	 * Construct the EpiLur from variable TAF parameters
	 * @param lurs
	 * @throws CadiException
	 */
	public EpiLur(Lur ... lurs) throws CadiException{
		this.lurs = lurs;
		if(lurs.length==0) throw new CadiException("Need at least one Lur implementation in constructor");
	}

	public boolean fish(Principal bait, Permission pond) {
		if(pond==null) {
			return false;
		}
		boolean rv = false;
		Lur lur;
		for(int i=0;!rv && i<lurs.length;++i) {
			rv = (lur = lurs[i]).fish(bait, pond);
			if(!rv && lur.handlesExclusively(pond)) break;
		}
		return rv;
	}

	public void fishAll(Principal bait, List<Permission> permissions) {
		for(Lur lur : lurs) {
			lur.fishAll(bait, permissions);
		}
	}

	public void destroy() {
		for(Lur lur : lurs) {
			lur.destroy();
		}
	}

	/**
	 * Return the first Lur (if any) which also implements UserPass 
	 * @return
	 */
	public CredVal getUserPassImpl() {
		for(Lur lur : lurs) {
			if(lur instanceof CredVal) {
				return (CredVal)lur;
			}
		}
		return null;
	}

	// Never needed... Only EpiLur uses...
	public boolean handlesExclusively(Permission pond) {
		return false;
	}
	
	/**
	 * Get Lur for index.  Returns null if out of range
	 * @param idx
	 * @return
	 */
	public Lur get(int idx) {
		if(idx>=0 && idx<lurs.length) {
			return lurs[idx];
		}
		return null;
	}

	public boolean supports(String userName) {
		for(Lur l : lurs) {
			if(l.supports(userName))return true;
		}
		return false;
	}

	public void remove(String id) {
		for(Lur l : lurs) {
			if(l instanceof CachingLur) {
				((CachingLur<?>)l).remove(id);
			}
		}
	}
	
	public Lur subLur(Class<? extends Lur> cls ) {
		for(Lur l : lurs) {
			if(l.getClass().isAssignableFrom(cls)) {
				return l;
			}
		}
		return null;
	}

	@Override
	public Permission createPerm(String p) {
		return new LocalPermission(p);
	}

	/* (non-Javadoc)
	 * @see com.att.cadi.Lur#clear(java.security.Principal, java.lang.StringBuilder)
	 */
	@Override
	public void clear(Principal p, StringBuilder report) {
		for(Lur lur : lurs) {
			lur.clear(p, report);
		}
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for(Lur lur : lurs) {
			sb.append(lur.getClass().getSimpleName());
			sb.append(": Report\n");
			sb.append(lur.toString());
			sb.append('\n');
		}
		return sb.toString();
	}
}