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 GregorianCalendar expires = org.expiration(null, Organization.Expiration.UserInRole);
53 if(urdd.expires.before(expires.getTime())) {
54 expires.setTime(urdd.expires);
57 setTargetKey(urdd.user+'|'+urdd.role);
58 setTargetDate(urdd.expires);
60 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
61 if(r.notOKorIsEmpty()) {
62 throw new CadiException(r.errorString());
64 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
65 if(n.notOKorIsEmpty()) {
66 throw new CadiException(n.errorString());
68 UserRoleDAO.Data found = null;
69 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
71 for(UserRoleDAO.Data ur : lur.value) {
72 if(urdd.user.equals(ur.user)) {
79 throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
82 // Primarily, Owners are responsible, unless it's owned by self
83 boolean isOwner = false;
84 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
86 for(UserRoleDAO.Data owner : owners.value) {
87 if(urdd.user.equals(owner.user)) {
90 ApprovalDAO.Data add = newApproval(urdd);
91 add.approver = owner.user;
100 List<Identity> apprs = org.getApprovers(trans, urdd.user);
102 for(Identity i : apprs) {
103 ApprovalDAO.Data add = newApproval(urdd);
104 add.approver = i.fullID();
105 add.type = org.getApproverType();
109 } catch (OrganizationException e) {
110 throw new CadiException(e);
115 private void setTargetDate(Date expires) {
116 fdd.target_date = expires;
119 private void setTargetKey(String key) {
120 fdd.target_key = key;
123 public void ownerSuperApprove(boolean set) {
124 ownerSuperApprove = set;
127 private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
128 ApprovalDAO.Data add = new ApprovalDAO.Data();
129 add.id = Chrono.dateToUUID(System.currentTimeMillis());
131 add.user = urdd.user;
132 add.operation = FUTURE_OP.A.name();
133 add.status = ApprovalDAO.PENDING;
134 add.memo = getMemo(urdd);
138 private String getMemo(Data urdd) {
141 return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
143 Chrono.dateOnlyStamp(urdd.expires));
145 return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
147 Chrono.dateOnlyStamp(urdd.expires));
149 return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
152 Chrono.dateOnlyStamp(urdd.expires));