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====================================================
22 package org.onap.aaf.auth.service.mapper;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.Date;
30 import java.util.GregorianCalendar;
31 import java.util.List;
32 import java.util.UUID;
34 import javax.xml.datatype.XMLGregorianCalendar;
36 import org.onap.aaf.auth.dao.Bytification;
37 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
38 import org.onap.aaf.auth.dao.cass.CertDAO;
39 import org.onap.aaf.auth.dao.cass.CredDAO;
40 import org.onap.aaf.auth.dao.cass.DelegateDAO;
41 import org.onap.aaf.auth.dao.cass.FutureDAO;
42 import org.onap.aaf.auth.dao.cass.HistoryDAO;
43 import org.onap.aaf.auth.dao.cass.Namespace;
44 import org.onap.aaf.auth.dao.cass.NsSplit;
45 import org.onap.aaf.auth.dao.cass.NsType;
46 import org.onap.aaf.auth.dao.cass.PermDAO;
47 import org.onap.aaf.auth.dao.cass.RoleDAO;
48 import org.onap.aaf.auth.dao.cass.Status;
49 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
50 import org.onap.aaf.auth.dao.cass.DelegateDAO.Data;
51 import org.onap.aaf.auth.dao.hl.Question;
52 import org.onap.aaf.auth.dao.hl.Question.Access;
53 import org.onap.aaf.auth.env.AuthzTrans;
54 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
55 import org.onap.aaf.auth.layer.Result;
56 import org.onap.aaf.auth.org.Organization;
57 import org.onap.aaf.auth.org.Organization.Expiration;
58 import org.onap.aaf.auth.rserv.Pair;
59 import org.onap.aaf.auth.service.MayChange;
60 import org.onap.aaf.cadi.aaf.marshal.CertsMarshal;
61 import org.onap.aaf.cadi.util.Vars;
62 import org.onap.aaf.misc.env.Env;
63 import org.onap.aaf.misc.env.TimeTaken;
64 import org.onap.aaf.misc.env.util.Chrono;
65 import org.onap.aaf.misc.rosetta.Marshal;
68 import aaf.v2_0.Approval;
69 import aaf.v2_0.Approvals;
70 import aaf.v2_0.Certs;
71 import aaf.v2_0.Certs.Cert;
72 import aaf.v2_0.CredRequest;
74 import aaf.v2_0.DelgRequest;
75 import aaf.v2_0.Delgs;
76 import aaf.v2_0.Error;
77 import aaf.v2_0.History;
78 import aaf.v2_0.History.Item;
80 import aaf.v2_0.NsRequest;
82 import aaf.v2_0.Nss.Ns;
84 import aaf.v2_0.PermKey;
85 import aaf.v2_0.PermRequest;
86 import aaf.v2_0.Perms;
88 import aaf.v2_0.Request;
90 import aaf.v2_0.RolePermRequest;
91 import aaf.v2_0.RoleRequest;
92 import aaf.v2_0.Roles;
93 import aaf.v2_0.UserRole;
94 import aaf.v2_0.UserRoleRequest;
95 import aaf.v2_0.UserRoles;
96 import aaf.v2_0.Users;
97 import aaf.v2_0.Users.User;
99 public class Mapper_2_0 implements Mapper<Nss, Perms, Pkey, Roles, Users, UserRoles, Delgs, Certs, Keys, Request, History, Error, Approvals> {
102 public Mapper_2_0(Question q) {
107 * @see org.onap.aaf.auth.service.mapper.Mapper#ns(java.lang.Object, org.onap.aaf.auth.service.mapper.Mapper.Holder)
110 public Result<Namespace> ns(AuthzTrans trans, Request base) {
111 NsRequest from = (NsRequest)base;
112 Namespace namespace = new Namespace();
113 namespace.name = from.getName();
114 namespace.admin = from.getAdmin();
115 namespace.owner = from.getResponsible();
116 namespace.description = from.getDescription();
117 trans.checkpoint(namespace.name, Env.ALWAYS);
119 NsType nt = NsType.fromString(from.getType());
120 if(nt.equals(NsType.UNKNOWN)) {
121 String ns = namespace.name;
123 for(int i=ns.indexOf('.');
125 i=ns.indexOf('.',i+1)) {
129 case 0: nt = NsType.ROOT;break;
130 case 1: nt = NsType.COMPANY;break;
131 default: nt = NsType.APP;
134 namespace.type = nt.type;
136 return Result.ok(namespace);
140 public Result<Nss> nss(AuthzTrans trans, Namespace from, Nss to) {
141 List<Ns> nss = to.getNs();
143 ns.setName(from.name);
144 if(from.admin!=null)ns.getAdmin().addAll(from.admin);
145 if(from.owner!=null)ns.getResponsible().addAll(from.owner);
146 if(from.attrib!=null) {
147 for(Pair<String,String> attrib : from.attrib) {
148 Ns.Attrib toAttrib = new Ns.Attrib();
149 toAttrib.setKey(attrib.x);
150 toAttrib.setValue(attrib.y);
151 ns.getAttrib().add(toAttrib);
155 ns.setDescription(from.description);
157 return Result.ok(to);
161 * Note: Prevalidate if NS given is allowed to be seen before calling
164 public Result<Nss> nss(AuthzTrans trans, Collection<Namespace> from, Nss to) {
165 List<Ns> nss = to.getNs();
166 for(Namespace nd : from) {
170 ns.getAdmin().addAll(nd.admin);
173 ns.getResponsible().addAll(nd.owner);
175 ns.setDescription(nd.description);
176 if(nd.attrib!=null) {
177 for(Pair<String,String> attrib : nd.attrib) {
178 Ns.Attrib toAttrib = new Ns.Attrib();
179 toAttrib.setKey(attrib.x);
180 toAttrib.setValue(attrib.y);
181 ns.getAttrib().add(toAttrib);
187 return Result.ok(to);
191 public Result<Perms> perms(AuthzTrans trans, List<PermDAO.Data> from, Perms to, boolean filter) {
192 List<Perm> perms = to.getPerm();
193 final boolean addNS = trans.requested(REQD_TYPE.ns);
194 TimeTaken tt = trans.start("Filter Perms before return", Env.SUB);
197 for (PermDAO.Data data : from) {
198 if(!filter || q.mayUser(trans, trans.user(), data, Access.read).isOK()) {
199 Perm perm = new Perm();
200 perm.setType(data.fullType());
201 perm.setInstance(data.instance);
202 perm.setAction(data.action);
203 perm.setDescription(data.description);
207 for(String role : data.roles(false)) {
208 perm.getRoles().add(role);
218 tt = trans.start("Sort Perms", Env.SUB);
220 Collections.sort(perms, new Comparator<Perm>() {
222 public int compare(Perm perm1, Perm perm2) {
223 int typeCompare = perm1.getType().compareToIgnoreCase(perm2.getType());
224 if (typeCompare == 0) {
225 int instanceCompare = perm1.getInstance().compareToIgnoreCase(perm2.getInstance());
226 if (instanceCompare == 0) {
227 return perm1.getAction().compareToIgnoreCase(perm2.getAction());
229 return instanceCompare;
237 return Result.ok(to);
241 public Result<Perms> perms(AuthzTrans trans, List<PermDAO.Data> from, Perms to, String[] nss, boolean filter) {
242 List<Perm> perms = to.getPerm();
243 TimeTaken tt = trans.start("Filter Perms before return", Env.SUB);
247 for (PermDAO.Data data : from) {
249 for(int i=0;!inNSS && i<nss.length;++i) {
250 if(nss[i].equals(data.ns)) {
254 if(inNSS && (!filter || q.mayUser(trans, trans.user(), data, Access.read).isOK())) {
255 Perm perm = new Perm();
256 perm.setType(data.fullType());
257 perm.setInstance(data.instance);
258 perm.setAction(data.action);
259 for(String role : data.roles(false)) {
260 perm.getRoles().add(role);
262 perm.setDescription(data.description);
271 tt = trans.start("Sort Perms", Env.SUB);
273 Collections.sort(perms, new Comparator<Perm>() {
275 public int compare(Perm perm1, Perm perm2) {
276 int typeCompare = perm1.getType().compareToIgnoreCase(perm2.getType());
277 if (typeCompare == 0) {
278 int instanceCompare = perm1.getInstance().compareToIgnoreCase(perm2.getInstance());
279 if (instanceCompare == 0) {
280 return perm1.getAction().compareToIgnoreCase(perm2.getAction());
282 return instanceCompare;
290 return Result.ok(to);
294 public Result<List<PermDAO.Data>> perms(AuthzTrans trans, Perms perms) {
295 List<PermDAO.Data> lpd = new ArrayList<PermDAO.Data>();
296 for (Perm p : perms.getPerm()) {
297 Result<NsSplit> nss = q.deriveNsSplit(trans, p.getType());
298 PermDAO.Data pd = new PermDAO.Data();
301 pd.type = nss.value.name;
302 pd.instance = p.getInstance();
303 pd.action = p.getAction();
304 for (String role : p.getRoles()) {
305 pd.roles(true).add(role);
309 return Result.err(nss);
312 return Result.ok(lpd);
317 public Result<PermDAO.Data> permkey(AuthzTrans trans, Pkey from) {
318 return q.permFrom(trans, from.getType(),from.getInstance(),from.getAction());
322 public Result<PermDAO.Data> permFromRPRequest(AuthzTrans trans, Request req) {
323 RolePermRequest from = (RolePermRequest)req;
324 Pkey perm = from.getPerm();
325 if(perm==null)return Result.err(Status.ERR_NotFound, "Permission not found");
326 Result<NsSplit> nss = q.deriveNsSplit(trans, perm.getType());
327 PermDAO.Data pd = new PermDAO.Data();
330 pd.type = nss.value.name;
331 pd.instance = from.getPerm().getInstance();
332 pd.action = from.getPerm().getAction();
333 trans.checkpoint(pd.fullPerm(), Env.ALWAYS);
337 if (from.getRole() != null) {
338 roles = from.getRole().split(",");
340 for (String role : roles) {
341 pd.roles(true).add(role);
343 return Result.ok(pd);
345 return Result.err(nss);
350 public Result<RoleDAO.Data> roleFromRPRequest(AuthzTrans trans, Request req) {
351 RolePermRequest from = (RolePermRequest)req;
352 Result<NsSplit> nss = q.deriveNsSplit(trans, from.getRole());
353 RoleDAO.Data rd = new RoleDAO.Data();
355 rd.ns = nss.value.ns;
356 rd.name = nss.value.name;
357 trans.checkpoint(rd.fullName(), Env.ALWAYS);
358 return Result.ok(rd);
360 return Result.err(nss);
365 public Result<PermDAO.Data> perm(AuthzTrans trans, Request req) {
366 PermRequest from = (PermRequest)req;
367 Result<NsSplit> nss = q.deriveNsSplit(trans, from.getType());
368 PermDAO.Data pd = new PermDAO.Data();
371 pd.type = nss.value.name;
372 pd.instance = from.getInstance();
373 pd.action = from.getAction();
374 pd.description = from.getDescription();
375 trans.checkpoint(pd.fullPerm(), Env.ALWAYS);
376 return Result.ok(pd);
378 return Result.err(nss);
383 public Request ungrantRequest(AuthzTrans trans, String role, String type, String instance, String action) {
384 RolePermRequest rpr = new RolePermRequest();
385 Pkey pkey = new Pkey();
387 pkey.setInstance(instance);
388 pkey.setAction(action);
396 public Result<RoleDAO.Data> role(AuthzTrans trans, Request base) {
397 RoleRequest from = (RoleRequest)base;
398 Result<NsSplit> nss = q.deriveNsSplit(trans, from.getName());
400 RoleDAO.Data to = new RoleDAO.Data();
401 to.ns = nss.value.ns;
402 to.name = nss.value.name;
403 to.description = from.getDescription();
404 trans.checkpoint(to.fullName(), Env.ALWAYS);
406 return Result.ok(to);
408 return Result.err(nss);
413 * @see org.onap.aaf.auth.service.mapper.Mapper#roles(java.util.List)
416 public Result<Roles> roles(AuthzTrans trans, List<RoleDAO.Data> from, Roles to, boolean filter) {
417 final boolean needNS = trans.requested(REQD_TYPE.ns);
418 for(RoleDAO.Data frole : from) {
419 // Only Add Data to view if User is allowed to see this Role
420 //if(!filter || q.mayUserViewRole(trans, trans.user(), frole).isOK()) {
421 if(!filter || q.mayUser(trans, trans.user(), frole,Access.read).isOK()) {
422 Role role = new Role();
423 role.setName(frole.ns + '.' + frole.name);
424 role.setDescription(frole.description);
426 role.setNs(frole.ns);
428 for(String p : frole.perms(false)) { // can see any Perms in the Role he has permission for
429 Result<String[]> rpa = PermDAO.Data.decodeToArray(trans,q,p);
430 if(rpa.notOK()) return Result.err(rpa);
432 String[] pa = rpa.value;
433 Pkey pKey = new Pkey();
434 pKey.setType(pa[0]+'.'+pa[1]);
435 pKey.setInstance(pa[2]);
436 pKey.setAction(pa[3]);
437 role.getPerms().add(pKey);
439 to.getRole().add(role);
442 return Result.ok(to);
447 * @see org.onap.aaf.auth.service.mapper.Mapper#users(java.util.Collection, java.lang.Object)
449 * Note: Prevalidate all data for permission to view
452 public Result<Users> users(AuthzTrans trans, Collection<UserRoleDAO.Data> from, Users to) {
453 List<User> cu = to.getUser();
454 for(UserRoleDAO.Data urd : from) {
455 User user = new User();
456 user.setId(urd.user);
457 if(urd.expires!=null) {
458 user.setExpires(Chrono.timeStamp(urd.expires));
462 return Result.ok(to);
467 * @see org.onap.aaf.auth.service.mapper.Mapper#users(java.util.Collection, java.lang.Object)
469 * Note: Prevalidate all data for permission to view
472 public Result<UserRoles> userRoles(AuthzTrans trans, Collection<UserRoleDAO.Data> from, UserRoles to) {
473 List<UserRole> cu = to.getUserRole();
474 for(UserRoleDAO.Data urd : from) {
475 UserRole ur = new UserRole();
476 ur.setUser(urd.user);
477 ur.setRole(urd.role);
478 ur.setExpires(Chrono.timeStamp(urd.expires));
481 return Result.ok(to);
491 public Result<UserRoleDAO.Data> userRole(AuthzTrans trans, Request base) {
493 UserRoleRequest from = (UserRoleRequest)base;
495 // Setup UserRoleData, either for immediate placement, or for futureIt i
496 UserRoleDAO.Data to = new UserRoleDAO.Data();
497 if (from.getUser() != null) {
498 String user = from.getUser();
501 if (from.getRole() != null) {
502 to.role(trans,q,from.getRole());
504 to.expires = getExpires(trans.org(),Expiration.UserInRole,base,from.getUser());
505 trans.checkpoint(to.toString(), Env.ALWAYS);
507 return Result.ok(to);
508 } catch (Exception t) {
509 return Result.err(Status.ERR_BadData,t.getMessage());
514 public Result<CredDAO.Data> cred(AuthzTrans trans, Request base, boolean requiresPass) {
515 CredRequest from = (CredRequest)base;
516 CredDAO.Data to = new CredDAO.Data();
518 to.ns = Question.domain2ns(to.id);
519 String passwd = from.getPassword();
521 String ok = trans.org().isValidPassword(trans, to.id,passwd);
523 return Result.err(Status.ERR_BadData,ok);
530 to.cred = ByteBuffer.wrap(passwd.getBytes());
531 to.type = CredDAO.RAW;
536 // Note: Ensure requested EndDate created will match Organization Password Rules
537 // P.S. Do not apply TempPassword rule here. Do that when you know you are doing a Create/Reset (see Service)
538 to.expires = getExpires(trans.org(),Expiration.Password,base,from.getId());
539 trans.checkpoint(to.id, Env.ALWAYS);
541 return Result.ok(to);
545 public Result<Users> cred(List<CredDAO.Data> from, Users to) {
546 List<User> cu = to.getUser();
547 for(CredDAO.Data cred : from) {
548 User user = new User();
550 user.setExpires(Chrono.timeStamp(cred.expires));
551 user.setType(cred.type);
554 return Result.ok(to);
558 public Result<Certs> cert(List<CertDAO.Data> from, Certs to) {
559 List<Cert> lc = to.getCert();
560 for(CertDAO.Data fcred : from) {
561 Cert cert = new Cert();
562 cert.setId(fcred.id);
563 cert.setX500(fcred.x500);
564 /**TODO - change Interface
566 cert.setFingerprint(fcred.serial.toByteArray());
569 return Result.ok(to);
573 * Analyze whether Requests should be acted on now, or in the future, based on Start Date, and whether the requester
574 * is allowed to change this value directly
576 * Returning Result.OK means it should be done in the future.
577 * Returning Result.ACC_Now means to act on table change now.
580 public Result<FutureDAO.Data> future(AuthzTrans trans, String table, Request from,
581 Bytification content, boolean enableApproval, Memo memo, MayChange mc) {
582 Result<?> rMayChange;
583 boolean needsAppr = enableApproval?trans.requested(REQD_TYPE.future):false;
584 if(!needsAppr && (needsAppr = (rMayChange=mc.mayChange()).notOK())) {
586 if(!trans.requested(AuthzTrans.REQD_TYPE.future)) {
587 return Result.err(rMayChange);
590 return Result.err(rMayChange);
593 GregorianCalendar now = new GregorianCalendar();
594 GregorianCalendar start = from.getStart()==null?now:from.getStart().toGregorianCalendar();
596 GregorianCalendar expires = trans.org().expiration(start, Expiration.Future);
597 XMLGregorianCalendar xgc;
598 if((xgc=from.getEnd())!=null) {
599 GregorianCalendar fgc = xgc.toGregorianCalendar();
600 expires = expires.before(fgc)?expires:fgc; // Min of desired expiration, and Org expiration
603 //TODO needs two answers from this. What's the NSS, and may Change.
605 if(start.after(now) || needsAppr ) {
606 //String user = trans.user();
607 fto = new FutureDAO.Data();
609 fto.memo = memo.get();
610 fto.start = start.getTime();
611 fto.expires = expires.getTime();
612 if(needsAppr) { // Need to add Approvers...
614 Result<Data> rslt = mc.getNsd();
615 if(rslt.notOKorIsEmpty())return Result.err(rslt);
616 appr.addAll(mc.getNsd().value.responsible);
618 //Note from 2013 Is this getting Approvers for user only? What about Delegates?
619 // 3/25/2014. Approvers are set by Corporate policy. We don't have to worry here about what that means.
620 // It is important to get Delegates, if necessary, at notification time
621 // If we add delegates now, it will get all confused as to who is actually responsible.
622 for(Organization.User ou : org.getApprovers(trans, user)) {
625 } catch (Exception e) {
626 return Result.err(Status.ERR_Policy,org.getName() + " did not respond with Approvers: " + e.getLocalizedMessage());
631 fto.construct = content.bytify();
632 } catch (Exception e) {
633 return Result.err(Status.ERR_BadData,"Data cannot be saved for Future.");
636 return Result.err(Status.ACC_Now, "Make Data changes now.");
638 return Result.ok(fto);
643 * @see org.onap.aaf.auth.service.mapper.Mapper#history(java.util.List)
646 public Result<History> history(AuthzTrans trans, List<HistoryDAO.Data> history, final int sort) {
647 History hist = new History();
648 List<Item> items = hist.getItem();
649 for(HistoryDAO.Data data : history) {
650 History.Item item = new History.Item();
651 item.setYYYYMM(Integer.toString(data.yr_mon));
652 Date date = Chrono.uuidToDate(data.id);
653 item.setTimestamp(Chrono.timeStamp(date));
654 item.setAction(data.action);
655 item.setMemo(data.memo);
656 item.setSubject(data.subject);
657 item.setTarget(data.target);
658 item.setUser(data.user);
663 TimeTaken tt = trans.start("Sort ", Env.SUB);
665 java.util.Collections.sort(items, new Comparator<Item>() {
667 public int compare(Item o1, Item o2) {
668 return sort*(o1.getTimestamp().compare(o2.getTimestamp()));
675 return Result.ok(hist);
679 public Error errorFromMessage(StringBuilder holder, String msgID, String text, String... var) {
680 Error err = new Error();
681 err.setMessageId(msgID);
682 // AT&T Restful Error Format requires numbers "%" placements
683 err.setText(Vars.convert(holder, text, var));
684 for(String s : var) {
685 err.getVariables().add(s);
691 public Class<?> getClass(API api) {
693 case NSS: return Nss.class;
694 case NS_REQ: return NsRequest.class;
695 case PERMS: return Perms.class;
696 case PERM_KEY: return PermKey.class;
697 case ROLES: return Roles.class;
698 case ROLE: return Role.class;
699 case USERS: return Users.class;
700 case DELGS: return Delgs.class;
701 case CERTS: return Certs.class;
702 case DELG_REQ: return DelgRequest.class;
703 case PERM_REQ: return PermRequest.class;
704 case ROLE_REQ: return RoleRequest.class;
705 case CRED_REQ: return CredRequest.class;
706 case USER_ROLE_REQ: return UserRoleRequest.class;
707 case USER_ROLES: return UserRoles.class;
708 case ROLE_PERM_REQ: return RolePermRequest.class;
709 case APPROVALS: return Approvals.class;
710 case KEYS: return Keys.class;
711 case HISTORY: return History.class;
712 // case MODEL: return Model.class;
713 case ERROR: return Error.class;
714 case API: return Api.class;
715 case VOID: return Void.class;
720 @SuppressWarnings("unchecked")
722 public <A> A newInstance(API api) {
724 case NS_REQ: return (A) new NsRequest();
725 case NSS: return (A) new Nss();
726 case PERMS: return (A)new Perms();
727 case PERM_KEY: return (A)new PermKey();
728 case ROLES: return (A)new Roles();
729 case ROLE: return (A)new Role();
730 case USERS: return (A)new Users();
731 case DELGS: return (A)new Delgs();
732 case CERTS: return (A)new Certs();
733 case PERM_REQ: return (A)new PermRequest();
734 case CRED_REQ: return (A)new CredRequest();
735 case ROLE_REQ: return (A)new RoleRequest();
736 case USER_ROLE_REQ: return (A)new UserRoleRequest();
737 case USER_ROLES: return (A)new UserRoles();
738 case ROLE_PERM_REQ: return (A)new RolePermRequest();
739 case HISTORY: return (A)new History();
740 case KEYS: return (A)new Keys();
741 //case MODEL: return (A)new Model();
742 case ERROR: return (A)new Error();
743 case API: return (A)new Api();
744 case VOID: return null;
746 case APPROVALS: return (A) new Approvals();
747 case DELG_REQ: return (A) new DelgRequest();
752 @SuppressWarnings("unchecked")
754 * Get Typed Marshaler as they are defined
759 public <A> Marshal<A> getMarshal(API api) {
761 case CERTS: return (Marshal<A>) new CertsMarshal();
768 public Result<Approvals> approvals(List<ApprovalDAO.Data> lAppr) {
769 Approvals apprs = new Approvals();
770 List<Approval> lappr = apprs.getApprovals();
772 for(ApprovalDAO.Data appr : lAppr) {
774 a.setId(appr.id.toString());
775 if(appr.ticket==null) {
778 a.setTicket(appr.ticket.toString());
780 a.setUser(appr.user);
781 a.setApprover(appr.approver);
782 a.setType(appr.type);
783 a.setStatus(appr.status);
784 a.setMemo(appr.memo);
785 a.setOperation(appr.operation);
786 a.setUpdated(Chrono.timeStamp(appr.updated));
789 return Result.ok(apprs);
793 public Result<List<ApprovalDAO.Data>> approvals(Approvals apprs) {
794 List<ApprovalDAO.Data> lappr = new ArrayList<ApprovalDAO.Data>();
795 for(Approval a : apprs.getApprovals()) {
796 ApprovalDAO.Data ad = new ApprovalDAO.Data();
797 String str = a.getId();
798 if(str!=null)ad.id=UUID.fromString(str);
800 if(str!=null)ad.ticket=UUID.fromString(str);
802 ad.approver=a.getApprover();
804 ad.status=a.getStatus();
805 ad.operation=a.getOperation();
808 XMLGregorianCalendar xgc = a.getUpdated();
809 if(xgc!=null)ad.updated=xgc.toGregorianCalendar().getTime();
812 return Result.ok(lappr);
816 public Result<Delgs> delegate(List<DelegateDAO.Data> lDelg) {
817 Delgs delgs = new Delgs();
818 List<Delg> ldelg = delgs.getDelgs();
820 for(DelegateDAO.Data del: lDelg) {
823 d.setDelegate(del.delegate);
824 if(del.expires!=null)d.setExpires(Chrono.timeStamp(del.expires));
827 return Result.ok(delgs);
831 public Result<Data> delegate(AuthzTrans trans, Request base) {
833 DelgRequest from = (DelgRequest)base;
834 DelegateDAO.Data to = new DelegateDAO.Data();
835 String user = from.getUser();
837 String delegate = from.getDelegate();
838 to.delegate = delegate;
839 to.expires = getExpires(trans.org(),Expiration.UserDelegate,base,from.getUser());
840 trans.checkpoint(to.user+"=>"+to.delegate, Env.ALWAYS);
842 return Result.ok(to);
843 } catch (Exception t) {
844 return Result.err(Status.ERR_BadData,t.getMessage());
849 * We want "Expired" dates to start at a specified time set by the Organization, and consistent wherever
850 * the date is created from.
852 private Date getExpires(Organization org, Expiration exp, Request base, String id) {
853 XMLGregorianCalendar end = base.getEnd();
854 GregorianCalendar gc = end==null?new GregorianCalendar():end.toGregorianCalendar();
855 GregorianCalendar orggc;
856 orggc = org.expiration(gc,exp,id);
858 // We'll choose the lesser of dates to ensure Policy Compliance...
860 GregorianCalendar endgc = end==null||gc.after(orggc)?orggc:gc;
861 // Allow the Organization to determine when official "day Start" begins, Specifically when to consider something Expired.
862 endgc = Chrono.firstMomentOfDay(endgc);
863 endgc.set(GregorianCalendar.HOUR_OF_DAY, org.startOfDay());
864 return endgc.getTime();
869 public Result<Keys> keys(Collection<String> from) {
870 Keys keys = new Keys();
871 keys.getKey().addAll(from);
872 return Result.ok(keys).emptyList(from.isEmpty());