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 {
42 public static final String EXTEND_STRING = "Extend access of User [%s] to Role [%s] - Expires %s";
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(String.format(EXTEND_STRING,urdd.user,urdd.role,Chrono.dateOnlyStamp(urdd.expires)));
50 setExpires(org.expiration(null, Organization.Expiration.UserInRole));
52 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
53 if(r.notOKorIsEmpty()) {
54 throw new CadiException(String.format("Role '%s' does not exist: %s", urdd.role, r.details));
56 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
57 if(n.notOKorIsEmpty()) {
58 throw new CadiException(String.format("Namespace '%s' does not exist: %s", urdd.ns));
60 UserRoleDAO.Data found = null;
61 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
63 for(UserRoleDAO.Data ur : lur.value) {
64 if(urdd.user.equals(ur.user)) {
71 throw new CadiException(String.format("User '%s' in Role '%s' does not exist: %s", urdd.user,urdd.role));
74 // Primarily, Owners are responsible, unless it's owned by self
75 boolean isOwner = false;
76 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
78 for(UserRoleDAO.Data owner : owners.value) {
79 if(urdd.user.equals(owner.user)) {
82 ApprovalDAO.Data add = newApproval(urdd);
83 add.approver = owner.user;
92 List<Identity> apprs = org.getApprovers(trans, urdd.user);
94 for(Identity i : apprs) {
95 ApprovalDAO.Data add = newApproval(urdd);
96 Identity reportsTo = i.responsibleTo();
98 add.approver = reportsTo.fullID();
100 throw new CadiException("No Supervisor for '" + urdd.user + '\'');
102 add.type = org.getApproverType();
106 } catch (OrganizationException e) {
107 throw new CadiException(e);
112 private ApprovalDAO.Data newApproval(Data urdd) throws CadiException {
113 ApprovalDAO.Data add = new ApprovalDAO.Data();
114 add.id = Chrono.dateToUUID(System.currentTimeMillis());
116 add.user = urdd.user;
117 add.operation = FUTURE_OP.A.name();
118 add.status = ApprovalDAO.PENDING;
119 add.memo = String.format("Re-Validate as Owner for AAF Namespace '%s' - expiring %s', ",
121 Chrono.dateOnlyStamp(urdd.expires));