Approval Batch, prep better JUnit
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / approvalsets / URApprovalSet.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21 package org.onap.aaf.auth.batch.approvalsets;
22
23 import java.io.IOException;
24 import java.util.GregorianCalendar;
25 import java.util.List;
26
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;
40
41 public class URApprovalSet extends ApprovalSet {
42         public static final String EXTEND_STRING = "Extend access of User [%s] to Role [%s] - Expires %s";
43         
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));
51                 
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));
55                 }
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));
59                 }
60                 UserRoleDAO.Data found = null;
61                 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
62                 if(lur.isOK()) {
63                         for(UserRoleDAO.Data ur : lur.value) {
64                                 if(urdd.user.equals(ur.user)) {
65                                         found = ur;
66                                         break;
67                                 }
68                         }
69                 }
70                 if(found==null) {
71                         throw new CadiException(String.format("User '%s' in Role '%s' does not exist: %s", urdd.user,urdd.role));
72                 }
73                 
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");
77                 if(owners.isOK()) {
78                         for(UserRoleDAO.Data owner : owners.value) {
79                                 if(urdd.user.equals(owner.user)) {
80                                         isOwner = true;
81                                 } else {
82                                         ApprovalDAO.Data add = newApproval(urdd);
83                                         add.approver = owner.user;
84                                         add.type="owner";
85                                         ladd.add(add);
86                                 }
87                         }
88                 }
89
90                 if(isOwner) {
91                         try {
92                                 List<Identity> apprs = org.getApprovers(trans, urdd.user);
93                                 if(apprs!=null) {
94                                         for(Identity i : apprs) {
95                                                 ApprovalDAO.Data add = newApproval(urdd);
96                                                 Identity reportsTo = i.responsibleTo();
97                                                 if(reportsTo!=null) {
98                                                         add.approver = reportsTo.fullID();
99                                                 } else {
100                                                         throw new CadiException("No Supervisor for '" + urdd.user + '\'');
101                                                 }
102                                                 add.type = org.getApproverType();
103                                                 ladd.add(add);
104                                         }
105                                 }
106                         } catch (OrganizationException e) {
107                                 throw new CadiException(e);
108                         }
109                 }
110         }
111
112         private ApprovalDAO.Data newApproval(Data urdd) throws CadiException {
113                 ApprovalDAO.Data add = new ApprovalDAO.Data();
114                 add.id = Chrono.dateToUUID(System.currentTimeMillis());
115                 add.ticket = fdd.id;
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', ",
120                                    urdd.ns,
121                                    Chrono.dateOnlyStamp(urdd.expires));
122                 return add;
123         }
124
125 }