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.GregorianCalendar;
25 import java.util.List;
27 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
28 import org.onap.aaf.auth.dao.cass.NsDAO;
29 import org.onap.aaf.auth.dao.cass.RoleDAO;
30 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
31 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
32 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.auth.org.Organization;
36 import org.onap.aaf.auth.org.Organization.Identity;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.misc.env.util.Chrono;
41 public class URApprovalSet extends ApprovalSet {
43 private boolean ownerSuperApprove;
45 public URApprovalSet(final AuthzTrans trans, final GregorianCalendar start, final DataView dv, final Loader<UserRoleDAO.Data> lurdd) throws IOException, CadiException {
46 super(start, "user_role", dv);
47 Organization org = trans.org();
48 UserRoleDAO.Data urdd = lurdd.load();
49 setConstruct(urdd.bytify());
50 setMemo(getMemo(urdd));
51 setExpires(org.expiration(null, Organization.Expiration.UserInRole));
53 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
54 if(r.notOKorIsEmpty()) {
55 throw new CadiException(r.errorString());
57 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
58 if(n.notOKorIsEmpty()) {
59 throw new CadiException(n.errorString());
61 UserRoleDAO.Data found = null;
62 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
64 for(UserRoleDAO.Data ur : lur.value) {
65 if(urdd.user.equals(ur.user)) {
72 throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
75 // Primarily, Owners are responsible, unless it's owned by self
76 boolean isOwner = false;
77 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
79 for(UserRoleDAO.Data owner : owners.value) {
80 if(urdd.user.equals(owner.user)) {
83 ApprovalDAO.Data add = newApproval(urdd);
84 add.approver = owner.user;
91 if(isOwner && ownerSuperApprove) {
93 List<Identity> apprs = org.getApprovers(trans, urdd.user);
95 for(Identity i : apprs) {
96 ApprovalDAO.Data add = newApproval(urdd);
97 Identity reportsTo = i.responsibleTo();
99 add.approver = reportsTo.fullID();
101 throw new CadiException("No Supervisor for '" + urdd.user + '\'');
103 add.type = org.getApproverType();
107 } catch (OrganizationException e) {
108 throw new CadiException(e);
113 public void ownerSuperApprove() {
114 ownerSuperApprove = true;
117 private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
118 ApprovalDAO.Data add = new ApprovalDAO.Data();
119 add.id = Chrono.dateToUUID(System.currentTimeMillis());
121 add.user = urdd.user;
122 add.operation = FUTURE_OP.A.name();
123 add.status = ApprovalDAO.PENDING;
124 add.memo = getMemo(urdd);
128 private String getMemo(Data urdd) {
131 return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
133 Chrono.dateOnlyStamp(urdd.expires));
135 return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
137 Chrono.dateOnlyStamp(urdd.expires));
139 return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
142 Chrono.dateOnlyStamp(urdd.expires));