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 public URApprovalSet(final AuthzTrans trans, final GregorianCalendar start, final DataView dv, final Loader<UserRoleDAO.Data> lurdd) throws IOException, CadiException {
45 super(start, "user_role", dv);
46 Organization org = trans.org();
47 UserRoleDAO.Data urdd = lurdd.load();
48 setConstruct(urdd.bytify());
49 setMemo(getMemo(urdd));
50 GregorianCalendar expires = org.expiration(null, Organization.Expiration.UserInRole);
51 if(urdd.expires.before(expires.getTime())) {
52 expires.setTime(urdd.expires);
55 setTargetKey(urdd.user+'|'+urdd.role);
56 setTargetDate(urdd.expires);
58 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
59 if(r.notOKorIsEmpty()) {
60 throw new CadiException(r.errorString());
62 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
63 if(n.notOKorIsEmpty()) {
64 throw new CadiException(n.errorString());
66 UserRoleDAO.Data found = null;
67 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
69 for(UserRoleDAO.Data ur : lur.value) {
70 if(urdd.user.equals(ur.user)) {
77 throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
80 // Primarily, Owners are responsible, unless it's owned by self
81 boolean isOwner = false;
82 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
84 for(UserRoleDAO.Data owner : owners.value) {
85 if(urdd.user.equals(owner.user)) {
88 ApprovalDAO.Data add = newApproval(urdd);
89 add.approver = owner.user;
98 List<Identity> apprs = org.getApprovers(trans, urdd.user);
100 for(Identity i : apprs) {
101 ApprovalDAO.Data add = newApproval(urdd);
102 add.approver = i.fullID();
103 add.type = org.getApproverType();
107 } catch (OrganizationException e) {
108 throw new CadiException(e);
113 private void setTargetDate(Date expires) {
114 fdd.target_date = expires;
117 private void setTargetKey(String key) {
118 fdd.target_key = key;
121 private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
122 ApprovalDAO.Data add = new ApprovalDAO.Data();
123 add.id = Chrono.dateToUUID(System.currentTimeMillis());
125 add.user = urdd.user;
126 add.operation = FUTURE_OP.A.name();
127 add.status = ApprovalDAO.PENDING;
128 add.memo = getMemo(urdd);
132 private String getMemo(Data urdd) {
135 return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
137 Chrono.dateOnlyStamp(urdd.expires));
139 return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
141 Chrono.dateOnlyStamp(urdd.expires));
143 return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
146 Chrono.dateOnlyStamp(urdd.expires));