1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao.aaf.cached;
\r
25 import java.util.ArrayList;
\r
26 import java.util.List;
\r
28 import org.onap.aaf.authz.env.AuthzTrans;
\r
29 import org.onap.aaf.authz.layer.Result;
\r
30 import org.onap.aaf.dao.CIDAO;
\r
31 import org.onap.aaf.dao.CachedDAO;
\r
32 import org.onap.aaf.dao.aaf.cass.Status;
\r
33 import org.onap.aaf.dao.aaf.cass.UserRoleDAO;
\r
34 import org.onap.aaf.dao.aaf.cass.UserRoleDAO.Data;
\r
36 import org.onap.aaf.inno.env.Slot;
\r
38 public class CachedUserRoleDAO extends CachedDAO<AuthzTrans,UserRoleDAO, UserRoleDAO.Data> {
\r
39 private Slot transURSlot;
\r
41 public CachedUserRoleDAO(UserRoleDAO dao, CIDAO<AuthzTrans> info) {
\r
42 super(dao, info, UserRoleDAO.CACHE_SEG);
\r
43 transURSlot = dao.transURSlot;
\r
48 * User Roles by User are very likely to be called many times in a Transaction, to validate "May User do..."
\r
49 * Pull result, and make accessible by the Trans, which is always keyed by User.
\r
54 public Result<List<Data>> readByUser(AuthzTrans trans, final String user) {
\r
55 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
56 public Result<List<Data>> call() {
\r
57 // If the call is for THIS user, and it exists, get from TRANS, add to TRANS if not.
\r
58 if(user!=null && user.equals(trans.user())) {
\r
59 Result<List<Data>> transLD = trans.get(transURSlot,null);
\r
60 if(transLD==null ) {
\r
61 transLD = dao.readByUser(trans, user);
\r
65 return dao.readByUser(trans, user);
\r
69 Result<List<Data>> lurd = get(trans, user, getter);
\r
70 if(lurd.isOK() && lurd.isEmpty()) {
\r
71 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",user);
\r
77 public Result<List<Data>> readByRole(AuthzTrans trans, final String role) {
\r
78 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
79 public Result<List<Data>> call() {
\r
80 return dao.readByRole(trans, role);
\r
83 Result<List<Data>> lurd = get(trans, role, getter);
\r
84 if(lurd.isOK() && lurd.isEmpty()) {
\r
85 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",role);
\r
90 public Result<List<UserRoleDAO.Data>> readUserInRole(final AuthzTrans trans, final String user, final String role) {
\r
91 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
92 public Result<List<Data>> call() {
\r
93 if(user.equals(trans.user())) {
\r
94 Result<List<Data>> rrbu = readByUser(trans, user);
\r
96 List<Data> ld = new ArrayList<Data>(1);
\r
97 for(Data d : rrbu.value) {
\r
98 if(d.role.equals(role)) {
\r
103 return Result.ok(ld).emptyList(ld.isEmpty());
\r
108 return dao.readByUserRole(trans, user, role);
\r
111 Result<List<Data>> lurd = get(trans, keyFromObjs(user,role), getter);
\r
112 if(lurd.isOK() && lurd.isEmpty()) {
\r
113 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for role [%s] and user [%s]",role,user);
\r