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 com.att.dao.aaf.cached;
\r
25 import java.util.ArrayList;
\r
26 import java.util.List;
\r
28 import com.att.authz.env.AuthzTrans;
\r
29 import com.att.authz.layer.Result;
\r
30 import com.att.dao.CIDAO;
\r
31 import com.att.dao.CachedDAO;
\r
32 import com.att.dao.aaf.cass.Status;
\r
33 import com.att.dao.aaf.cass.UserRoleDAO;
\r
34 import com.att.dao.aaf.cass.UserRoleDAO.Data;
\r
35 import com.att.inno.env.Slot;
\r
37 public class CachedUserRoleDAO extends CachedDAO<AuthzTrans,UserRoleDAO, UserRoleDAO.Data> {
\r
38 private Slot transURSlot;
\r
40 public CachedUserRoleDAO(UserRoleDAO dao, CIDAO<AuthzTrans> info) {
\r
41 super(dao, info, UserRoleDAO.CACHE_SEG);
\r
42 transURSlot = dao.transURSlot;
\r
47 * User Roles by User are very likely to be called many times in a Transaction, to validate "May User do..."
\r
48 * Pull result, and make accessible by the Trans, which is always keyed by User.
\r
53 public Result<List<Data>> readByUser(AuthzTrans trans, final String user) {
\r
54 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
55 public Result<List<Data>> call() {
\r
56 // If the call is for THIS user, and it exists, get from TRANS, add to TRANS if not.
\r
57 if(user!=null && user.equals(trans.user())) {
\r
58 Result<List<Data>> transLD = trans.get(transURSlot,null);
\r
59 if(transLD==null ) {
\r
60 transLD = dao.readByUser(trans, user);
\r
64 return dao.readByUser(trans, user);
\r
68 Result<List<Data>> lurd = get(trans, user, getter);
\r
69 if(lurd.isOK() && lurd.isEmpty()) {
\r
70 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",user);
\r
76 public Result<List<Data>> readByRole(AuthzTrans trans, final String role) {
\r
77 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
78 public Result<List<Data>> call() {
\r
79 return dao.readByRole(trans, role);
\r
82 Result<List<Data>> lurd = get(trans, role, getter);
\r
83 if(lurd.isOK() && lurd.isEmpty()) {
\r
84 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",role);
\r
89 public Result<List<UserRoleDAO.Data>> readUserInRole(final AuthzTrans trans, final String user, final String role) {
\r
90 DAOGetter getter = new DAOGetter(trans,dao()) {
\r
91 public Result<List<Data>> call() {
\r
92 if(user.equals(trans.user())) {
\r
93 Result<List<Data>> rrbu = readByUser(trans, user);
\r
95 List<Data> ld = new ArrayList<Data>(1);
\r
96 for(Data d : rrbu.value) {
\r
97 if(d.role.equals(role)) {
\r
102 return Result.ok(ld).emptyList(ld.isEmpty());
\r
107 return dao.readByUserRole(trans, user, role);
\r
110 Result<List<Data>> lurd = get(trans, keyFromObjs(user,role), getter);
\r
111 if(lurd.isOK() && lurd.isEmpty()) {
\r
112 return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for role [%s] and user [%s]",role,user);
\r