Update Batch from Testing
[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         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);
53                 }
54                 setExpires(expires);
55                 setTargetKey(urdd.user+'|'+urdd.role);
56                 setTargetDate(urdd.expires);
57                 
58                 Result<RoleDAO.Data> r = dv.roleByName(trans, urdd.role);
59                 if(r.notOKorIsEmpty()) {
60                         throw new CadiException(r.errorString());
61                 }
62                 Result<NsDAO.Data> n = dv.ns(trans, urdd.ns);
63                 if(n.notOKorIsEmpty()) {
64                         throw new CadiException(n.errorString());
65                 }
66                 UserRoleDAO.Data found = null;
67                 Result<List<Data>> lur = dv.ursByRole(trans, urdd.role);
68                 if(lur.isOK()) {
69                         for(UserRoleDAO.Data ur : lur.value) {
70                                 if(urdd.user.equals(ur.user)) {
71                                         found = ur;
72                                         break;
73                                 }
74                         }
75                 }
76                 if(found==null) {
77                         throw new CadiException(String.format("User '%s' in Role '%s' does not exist", urdd.user,urdd.role));
78                 }
79                 
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");
83                 if(owners.isOK()) {
84                         for(UserRoleDAO.Data owner : owners.value) {
85                                 if(urdd.user.equals(owner.user)) {
86                                         isOwner = true;
87                                 } else {
88                                         ApprovalDAO.Data add = newApproval(urdd);
89                                         add.approver = owner.user;
90                                         add.type="owner";
91                                         ladd.add(add);
92                                 }
93                         }
94                 }
95
96                 if(isOwner) {
97                         try {
98                                 List<Identity> apprs = org.getApprovers(trans, urdd.user);
99                                 if(apprs!=null) {
100                                         for(Identity i : apprs) {
101                                                 ApprovalDAO.Data add = newApproval(urdd);
102                                                 add.approver = i.fullID();
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         private void setTargetDate(Date expires) {
114                 fdd.target_date = expires;
115         }
116
117         private void setTargetKey(String key) {
118                 fdd.target_key = key;
119         }
120
121         private ApprovalDAO.Data newApproval(UserRoleDAO.Data urdd) throws CadiException {
122                 ApprovalDAO.Data add = new ApprovalDAO.Data();
123                 add.id = Chrono.dateToUUID(System.currentTimeMillis());
124                 add.ticket = fdd.id;
125                 add.user = urdd.user;
126                 add.operation = FUTURE_OP.A.name();
127                 add.status = ApprovalDAO.PENDING;
128                 add.memo = getMemo(urdd);
129                 return add;
130         }
131
132         private String getMemo(Data urdd) {
133                 switch(urdd.rname) {
134                 case "owner":
135                         return String.format("Revalidate as Owner of AAF Namespace [%s] - Expires %s",
136                                            urdd.ns,
137                                            Chrono.dateOnlyStamp(urdd.expires));
138                 case "admin":
139                         return String.format("Revalidate as Admin of AAF Namespace [%s] - Expires %s",
140                                            urdd.ns,
141                                            Chrono.dateOnlyStamp(urdd.expires));
142                 default:
143                         return String.format("Extend access of User [%s] to Role [%s] - Expires %s",
144                                            urdd.user,
145                                            urdd.role,
146                                            Chrono.dateOnlyStamp(urdd.expires));
147                 }
148         }
149
150 }