2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.auth.batch.approvalsets;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26 import java.util.List;
28 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
29 import org.onap.aaf.auth.dao.cass.NsDAO;
30 import org.onap.aaf.auth.dao.cass.RoleDAO;
31 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
32 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
33 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.layer.Result;
36 import org.onap.aaf.auth.org.Organization;
37 import org.onap.aaf.auth.org.Organization.Identity;
38 import org.onap.aaf.auth.org.OrganizationException;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.misc.env.util.Chrono;
42 public class URApprovalSet extends ApprovalSet {
44 private boolean ownerSuperApprove = true;
46 public URApprovalSet(final AuthzTrans trans, final GregorianCalendar start, final DataView dv, final Loader<UserRoleDAO.Data> lurdd) throws IOException, CadiException {
47 super(start, "user_role", dv);
48 Organization org = trans.org();
49 UserRoleDAO.Data urdd = lurdd.load();
50 setConstruct(urdd.bytify());
51 setMemo(getMemo(urdd));
52 setExpires(org.expiration(null, Organization.Expiration.UserInRole));
53 setTargetKey(urdd.role);
54 setTargetDate(urdd.expires);
56 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
57 if(r.notOKorIsEmpty()) {
58 throw new CadiException(r.errorString());
60 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
61 if(n.notOKorIsEmpty()) {
62 throw new CadiException(n.errorString());
64 UserRoleDAO.Data found = null;
65 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
67 for(UserRoleDAO.Data ur : lur.value) {
68 if(urdd.user.equals(ur.user)) {
75 throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
78 // Primarily, Owners are responsible, unless it's owned by self
79 boolean isOwner = false;
80 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
82 for(UserRoleDAO.Data owner : owners.value) {
83 if(urdd.user.equals(owner.user)) {
86 ApprovalDAO.Data add = newApproval(urdd);
87 add.approver = owner.user;
96 List<Identity> apprs = org.getApprovers(trans, urdd.user);
98 for(Identity i : apprs) {
99 ApprovalDAO.Data add = newApproval(urdd);
100 add.approver = i.fullID();
101 add.type = org.getApproverType();
105 } catch (OrganizationException e) {
106 throw new CadiException(e);
111 private void setTargetDate(Date expires) {
112 fdd.target_date = expires;
115 private void setTargetKey(String key) {
116 fdd.target_key = key;
119 public void ownerSuperApprove(boolean set) {
120 ownerSuperApprove = set;
123 private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
124 ApprovalDAO.Data add = new ApprovalDAO.Data();
125 add.id = Chrono.dateToUUID(System.currentTimeMillis());
127 add.user = urdd.user;
128 add.operation = FUTURE_OP.A.name();
129 add.status = ApprovalDAO.PENDING;
130 add.memo = getMemo(urdd);
134 private String getMemo(Data urdd) {
137 return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
139 Chrono.dateOnlyStamp(urdd.expires));
141 return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
143 Chrono.dateOnlyStamp(urdd.expires));
145 return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
148 Chrono.dateOnlyStamp(urdd.expires));