Improve Batches
[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.Date;
25 import java.util.GregorianCalendar;
26 import java.util.List;
27
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;
41
42 public class URApprovalSet extends ApprovalSet {
43         
44         private boolean ownerSuperApprove = true;
45
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                 setExpires(org.expiration(null, Organization.Expiration.UserInRole));
53                 setTargetKey(urdd.role);
54                 setTargetDate(urdd.expires);
55                 
56                 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
57                 if(r.notOKorIsEmpty()) {
58                         throw new CadiException(r.errorString());
59                 }
60                 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
61                 if(n.notOKorIsEmpty()) {
62                         throw new CadiException(n.errorString());
63                 }
64                 UserRoleDAO.Data found = null;
65                 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
66                 if(lur.isOK()) {
67                         for(UserRoleDAO.Data ur : lur.value) {
68                                 if(urdd.user.equals(ur.user)) {
69                                         found = ur;
70                                         break;
71                                 }
72                         }
73                 }
74                 if(found==null) {
75                         throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
76                 }
77                 
78                 // Primarily, Owners are responsible, unless it's owned by self
79                 boolean isOwner = false;
80                 Result<List<UserRoleDAO.Data>> owners = dv.ursByRole(trans, urdd.ns+".owner");
81                 if(owners.isOK()) {
82                         for(UserRoleDAO.Data owner : owners.value) {
83                                 if(urdd.user.equals(owner.user)) {
84                                         isOwner = true;
85                                 } else {
86                                         ApprovalDAO.Data add = newApproval(urdd);
87                                         add.approver = owner.user;
88                                         add.type="owner";
89                                         ladd.add(add);
90                                 }
91                         }
92                 }
93
94                 if(isOwner) {
95                         try {
96                                 List<Identity> apprs = org.getApprovers(trans, urdd.user);
97                                 if(apprs!=null) {
98                                         for(Identity i : apprs) {
99                                                 ApprovalDAO.Data add = newApproval(urdd);
100                                                 add.approver = i.fullID();
101                                                 add.type = org.getApproverType();
102                                                 ladd.add(add);
103                                         }
104                                 }
105                         } catch (OrganizationException e) {
106                                 throw new CadiException(e);
107                         }
108                 }
109         }
110         
111         private void setTargetDate(Date expires) {
112                 fdd.target_date = expires;
113         }
114
115         private void setTargetKey(String key) {
116                 fdd.target_key = key;
117         }
118
119         public void ownerSuperApprove(boolean set) {
120                 ownerSuperApprove = set;
121         }
122
123         private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
124                 ApprovalDAO.Data add = new ApprovalDAO.Data();
125                 add.id = Chrono.dateToUUID(System.currentTimeMillis());
126                 add.ticket = fdd.id;
127                 add.user = urdd.user;
128                 add.operation = FUTURE_OP.A.name();
129                 add.status = ApprovalDAO.PENDING;
130                 add.memo = getMemo(urdd);
131                 return add;
132         }
133
134         private String getMemo(Data urdd) {
135                 switch(urdd.rname) {
136                 case "owner":
137                         return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
138                                            urdd.ns,
139                                            Chrono.dateOnlyStamp(urdd.expires));
140                 case "admin":
141                         return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
142                                            urdd.ns,
143                                            Chrono.dateOnlyStamp(urdd.expires));
144                 default:
145                         return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
146                                            urdd.user,
147                                            urdd.role,
148                                            Chrono.dateOnlyStamp(urdd.expires));
149                 }
150         }
151
152 }