Batch work and client
[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         
43         private boolean ownerSuperApprove;
44
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));
52                 
53                 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
54                 if(r.notOKorIsEmpty()) {
55                         throw new CadiException(r.errorString());
56                 }
57                 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
58                 if(n.notOKorIsEmpty()) {
59                         throw new CadiException(n.errorString());
60                 }
61                 UserRoleDAO.Data found = null;
62                 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
63                 if(lur.isOK()) {
64                         for(UserRoleDAO.Data ur : lur.value) {
65                                 if(urdd.user.equals(ur.user)) {
66                                         found = ur;
67                                         break;
68                                 }
69                         }
70                 }
71                 if(found==null) {
72                         throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
73                 }
74                 
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");
78                 if(owners.isOK()) {
79                         for(UserRoleDAO.Data owner : owners.value) {
80                                 if(urdd.user.equals(owner.user)) {
81                                         isOwner = true;
82                                 } else {
83                                         ApprovalDAO.Data add = newApproval(urdd);
84                                         add.approver = owner.user;
85                                         add.type="owner";
86                                         ladd.add(add);
87                                 }
88                         }
89                 }
90
91                 if(isOwner && ownerSuperApprove) {
92                         try {
93                                 List<Identity> apprs = org.getApprovers(trans, urdd.user);
94                                 if(apprs!=null) {
95                                         for(Identity i : apprs) {
96                                                 ApprovalDAO.Data add = newApproval(urdd);
97                                                 Identity reportsTo = i.responsibleTo();
98                                                 if(reportsTo!=null) {
99                                                         add.approver = reportsTo.fullID();
100                                                 } else {
101                                                         throw new CadiException("No Supervisor for '" + urdd.user + '\'');
102                                                 }
103                                                 add.type = org.getApproverType();
104                                                 ladd.add(add);
105                                         }
106                                 }
107                         } catch (OrganizationException e) {
108                                 throw new CadiException(e);
109                         }
110                 }
111         }
112         
113         public void ownerSuperApprove() {
114                 ownerSuperApprove = true;
115         }
116
117         private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
118                 ApprovalDAO.Data add = new ApprovalDAO.Data();
119                 add.id = Chrono.dateToUUID(System.currentTimeMillis());
120                 add.ticket = fdd.id;
121                 add.user = urdd.user;
122                 add.operation = FUTURE_OP.A.name();
123                 add.status = ApprovalDAO.PENDING;
124                 add.memo = getMemo(urdd);
125                 return add;
126         }
127
128         private String getMemo(Data urdd) {
129                 switch(urdd.rname) {
130                 case "owner":
131                         return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
132                                            urdd.ns,
133                                            Chrono.dateOnlyStamp(urdd.expires));
134                 case "admin":
135                         return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
136                                            urdd.ns,
137                                            Chrono.dateOnlyStamp(urdd.expires));
138                 default:
139                         return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
140                                            urdd.user,
141                                            urdd.role,
142                                            Chrono.dateOnlyStamp(urdd.expires));
143                 }
144         }
145
146 }