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;
24 import static org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE.force;
25 import static org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE.future;
26 import static org.onap.aaf.auth.layer.Result.OK;
27 import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
28 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
29 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
30 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.List;
44 import java.util.TreeMap;
45 import java.util.UUID;
47 import javax.servlet.http.HttpServletRequest;
49 import org.onap.aaf.auth.common.Define;
50 import org.onap.aaf.auth.dao.DAOException;
51 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
52 import org.onap.aaf.auth.dao.cass.CertDAO;
53 import org.onap.aaf.auth.dao.cass.CredDAO;
54 import org.onap.aaf.auth.dao.cass.DelegateDAO;
55 import org.onap.aaf.auth.dao.cass.FutureDAO;
56 import org.onap.aaf.auth.dao.cass.HistoryDAO;
57 import org.onap.aaf.auth.dao.cass.Namespace;
58 import org.onap.aaf.auth.dao.cass.NsDAO;
59 import org.onap.aaf.auth.dao.cass.NsDAO.Data;
60 import org.onap.aaf.auth.dao.cass.NsSplit;
61 import org.onap.aaf.auth.dao.cass.NsType;
62 import org.onap.aaf.auth.dao.cass.PermDAO;
63 import org.onap.aaf.auth.dao.cass.RoleDAO;
64 import org.onap.aaf.auth.dao.cass.Status;
65 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
66 import org.onap.aaf.auth.dao.hl.CassExecutor;
67 import org.onap.aaf.auth.dao.hl.Function;
68 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
69 import org.onap.aaf.auth.dao.hl.Function.Lookup;
70 import org.onap.aaf.auth.dao.hl.Function.OP_STATUS;
71 import org.onap.aaf.auth.dao.hl.Question;
72 import org.onap.aaf.auth.dao.hl.Question.Access;
73 import org.onap.aaf.auth.env.AuthzTrans;
74 import org.onap.aaf.auth.layer.Result;
75 import org.onap.aaf.auth.org.Executor;
76 import org.onap.aaf.auth.org.Organization;
77 import org.onap.aaf.auth.org.Organization.Expiration;
78 import org.onap.aaf.auth.org.Organization.Identity;
79 import org.onap.aaf.auth.org.Organization.Policy;
80 import org.onap.aaf.auth.org.OrganizationException;
81 import org.onap.aaf.auth.rserv.doc.ApiDoc;
82 import org.onap.aaf.auth.service.mapper.Mapper;
83 import org.onap.aaf.auth.service.mapper.Mapper.API;
84 import org.onap.aaf.auth.service.validation.ServiceValidator;
85 import org.onap.aaf.auth.validation.Validator;
86 import org.onap.aaf.cadi.principal.BasicPrincipal;
87 import org.onap.aaf.misc.env.Env;
88 import org.onap.aaf.misc.env.TimeTaken;
89 import org.onap.aaf.misc.env.util.Chrono;
90 import org.onap.aaf.misc.env.util.Split;
92 import aaf.v2_0.CredRequest;
95 * AuthzCassServiceImpl implements AuthzCassService for
110 public class AuthzCassServiceImpl <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS>
111 implements AuthzService <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> {
113 private Mapper <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper;
115 public Mapper <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper() {return mapper;}
117 private static final String ASTERIX = "*";
118 private static final String CACHE = "cache";
119 private static final String ROOT_NS = Define.ROOT_NS();
120 private static final String ROOT_COMPANY = Define.ROOT_COMPANY();
122 private final Question ques;
123 private final Function func;
125 public AuthzCassServiceImpl(AuthzTrans trans, Mapper<NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper,Question question) {
126 this.ques = question;
127 func = new Function(trans, question);
128 this.mapper = mapper;
132 /***********************************
134 ***********************************/
137 * @throws DAOException
138 * @see org.onap.aaf.auth.service.AuthzService#createNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.String)
145 errorCodes = { 403,404,406,409 },
146 text = { "Namespace consists of: ",
147 "<ul><li>name - What you want to call this Namespace</li>",
148 "<li>responsible(s) - Person(s) who receive Notifications and approves Requests ",
149 "regarding this Namespace. Companies have Policies as to who may take on ",
150 "this Responsibility. Separate multiple identities with commas</li>",
151 "<li>admin(s) - Person(s) who are allowed to make changes on the namespace, ",
152 "including creating Roles, Permissions and Credentials. Separate multiple ",
153 "identities with commas</li></ul>",
154 "Note: Namespaces are dot-delimited (i.e. com.myCompany.myApp) and must be ",
155 "created with parent credentials (i.e. To create com.myCompany.myApp, you must ",
156 "be an admin of com.myCompany or com"
160 public Result<Void> createNS(final AuthzTrans trans, REQUEST from, NsType type) {
161 final Result<Namespace> rnamespace = mapper.ns(trans, from);
162 final ServiceValidator v = new ServiceValidator();
163 if (v.ns(rnamespace).err()) {
164 return Result.err(Status.ERR_BadData,v.errs());
166 final Namespace namespace = rnamespace.value;
167 final Result<NsDAO.Data> parentNs = ques.deriveNs(trans,namespace.name);
168 if (parentNs.notOK()) {
169 return Result.err(parentNs);
172 if (namespace.name.lastIndexOf('.')<0) { // Root Namespace... Function will check if allowed
173 return func.createNS(trans, namespace, false);
176 Result<FutureDAO.Data> fd = mapper.future(trans, NsDAO.TABLE,from,namespace,true,
179 public String get() {
180 return "Create Namespace [" + namespace.name + ']';
184 private Result<NsDAO.Data> rnd;
186 public Result<?> mayChange() {
188 rnd = ques.mayUser(trans, trans.user(), parentNs.value,Access.write);
195 Result<String> rfc = func.createFuture(trans, fd.value, namespace.name, trans.user(),parentNs.value, FUTURE_OP.C);
197 return Result.err(Status.ACC_Future, "NS [%s] is saved for future processing",namespace.name);
199 return Result.err(rfc);
202 return func.createNS(trans, namespace, false);
204 return Result.err(fd);
210 path = "/authz/ns/:ns/admin/:id",
211 params = { "ns|string|true",
215 errorCodes = { 403,404,406,409 },
216 text = { "Add an Identity :id to the list of Admins for the Namespace :ns",
217 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
220 public Result<Void> addAdminNS(AuthzTrans trans, String ns, String id) {
221 return func.addUserRole(trans, id, ns,Question.ADMIN);
226 path = "/authz/ns/:ns/admin/:id",
227 params = { "ns|string|true",
231 errorCodes = { 403,404 },
232 text = { "Remove an Identity :id from the list of Admins for the Namespace :ns",
233 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
236 public Result<Void> delAdminNS(AuthzTrans trans, String ns, String id) {
237 return func.delAdmin(trans,ns,id);
242 path = "/authz/ns/:ns/responsible/:id",
243 params = { "ns|string|true",
247 errorCodes = { 403,404,406,409 },
248 text = { "Add an Identity :id to the list of Responsibles for the Namespace :ns",
249 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
252 public Result<Void> addResponsibleNS(AuthzTrans trans, String ns, String id) {
253 return func.addUserRole(trans,id,ns,Question.OWNER);
258 path = "/authz/ns/:ns/responsible/:id",
259 params = { "ns|string|true",
263 errorCodes = { 403,404 },
264 text = { "Remove an Identity :id to the list of Responsibles for the Namespace :ns",
265 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)",
266 "Note: A namespace must have at least 1 responsible party"
270 public Result<Void> delResponsibleNS(AuthzTrans trans, String ns, String id) {
271 return func.delOwner(trans,ns,id);
275 * @see org.onap.aaf.auth.service.AuthzService#applyModel(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
279 path = "/authz/ns/:ns/attrib/:key/:value",
280 params = { "ns|string|true",
282 "value|string|true"},
284 errorCodes = { 403,404,406,409 },
286 "Create an attribute in the Namespace",
287 "You must be given direct permission for key by AAF"
291 public Result<Void> createNsAttrib(AuthzTrans trans, String ns, String key, String value) {
292 TimeTaken tt = trans.start("Create NsAttrib " + ns + ':' + key + ':' + value, Env.SUB);
295 final Validator v = new ServiceValidator();
296 if (v.ns(ns).err() ||
298 v.value(value).err()) {
299 return Result.err(Status.ERR_BadData,v.errs());
302 // Check if exists already
303 Result<List<Data>> rlnsd = ques.nsDAO.read(trans, ns);
304 if (rlnsd.notOKorIsEmpty()) {
305 return Result.err(rlnsd);
307 NsDAO.Data nsd = rlnsd.value.get(0);
309 // Check for Existence
310 if (nsd.attrib.get(key)!=null) {
311 return Result.err(Status.ERR_ConflictAlreadyExists, "NS Property %s:%s exists", ns, key);
314 // Check if User may put
315 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
316 ":"+trans.org().getDomain()+".*:"+key, Access.write.name())) {
317 return Result.err(Status.ERR_Denied, "%s may not create NS Attrib [%s:%s]", trans.user(),ns, key);
321 nsd.attrib.put(key, value);
322 ques.nsDAO.dao().attribAdd(trans,ns,key,value);
331 path = "/authz/ns/attrib/:key",
332 params = { "key|string|true" },
334 errorCodes = { 403,404 },
336 "Read Attributes for Namespace"
340 public Result<KEYS> readNsByAttrib(AuthzTrans trans, String key) {
342 final Validator v = new ServiceValidator();
343 if (v.nullOrBlank("Key",key).err()) {
344 return Result.err(Status.ERR_BadData,v.errs());
348 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
349 ":"+trans.org().getDomain()+".*:"+key, Question.READ)) {
350 return Result.err(Status.ERR_Denied,"%s may not read NS by Attrib '%s'",trans.user(),key);
353 Result<Set<String>> rsd = ques.nsDAO.dao().readNsByAttrib(trans, key);
355 return Result.err(rsd);
357 return mapper().keys(rsd.value);
363 path = "/authz/ns/:ns/attrib/:key/:value",
364 params = { "ns|string|true",
367 errorCodes = { 403,404 },
369 "Update Value on an existing attribute in the Namespace",
370 "You must be given direct permission for key by AAF"
374 public Result<?> updateNsAttrib(AuthzTrans trans, String ns, String key, String value) {
375 TimeTaken tt = trans.start("Update NsAttrib " + ns + ':' + key + ':' + value, Env.SUB);
378 final Validator v = new ServiceValidator();
379 if (v.ns(ns).err() ||
381 v.value(value).err()) {
382 return Result.err(Status.ERR_BadData,v.errs());
385 // Check if exists already (NS must exist)
386 Result<List<Data>> rlnsd = ques.nsDAO.read(trans, ns);
387 if (rlnsd.notOKorIsEmpty()) {
388 return Result.err(rlnsd);
390 NsDAO.Data nsd = rlnsd.value.get(0);
392 // Check for Existence
393 if (nsd.attrib.get(key)==null) {
394 return Result.err(Status.ERR_NotFound, "NS Property %s:%s exists", ns, key);
397 // Check if User may put
398 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
399 ":"+trans.org().getDomain()+".*:"+key, Access.write.name())) {
400 return Result.err(Status.ERR_Denied, "%s may not create NS Attrib [%s:%s]", trans.user(),ns, key);
404 nsd.attrib.put(key, value);
406 return ques.nsDAO.update(trans,nsd);
415 path = "/authz/ns/:ns/attrib/:key",
416 params = { "ns|string|true",
419 errorCodes = { 403,404 },
421 "Delete an attribute in the Namespace",
422 "You must be given direct permission for key by AAF"
426 public Result<Void> deleteNsAttrib(AuthzTrans trans, String ns, String key) {
427 TimeTaken tt = trans.start("Delete NsAttrib " + ns + ':' + key, Env.SUB);
430 final Validator v = new ServiceValidator();
431 if (v.nullOrBlank("NS",ns).err() ||
432 v.nullOrBlank("Key",key).err()) {
433 return Result.err(Status.ERR_BadData,v.errs());
436 // Check if exists already
437 Result<List<Data>> rlnsd = ques.nsDAO.read(trans, ns);
438 if (rlnsd.notOKorIsEmpty()) {
439 return Result.err(rlnsd);
441 NsDAO.Data nsd = rlnsd.value.get(0);
443 // Check for Existence
444 if (nsd.attrib.get(key)==null) {
445 return Result.err(Status.ERR_NotFound, "NS Property [%s:%s] does not exist", ns, key);
448 // Check if User may del
449 if (!ques.isGranted(trans, trans.user(), ROOT_NS, "attrib", ":" + ROOT_COMPANY + ".*:"+key, Access.write.name())) {
450 return Result.err(Status.ERR_Denied, "%s may not delete NS Attrib [%s:%s]", trans.user(),ns, key);
454 nsd.attrib.remove(key);
455 ques.nsDAO.dao().attribRemove(trans,ns,key);
464 path = "/authz/nss/:id",
465 params = { "id|string|true" },
467 errorCodes = { 404,406 },
469 "Lists the Admin(s), Responsible Party(s), Role(s), Permission(s)",
470 "Credential(s) and Expiration of Credential(s) in Namespace :id",
474 public Result<NSS> getNSbyName(AuthzTrans trans, String ns) {
475 final Validator v = new ServiceValidator();
476 if (v.nullOrBlank("NS", ns).err()) {
477 return Result.err(Status.ERR_BadData,v.errs());
480 Result<List<NsDAO.Data>> rlnd = ques.nsDAO.read(trans, ns);
482 if (rlnd.isEmpty()) {
483 return Result.err(Status.ERR_NotFound, "No data found for %s",ns);
485 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rlnd.value.get(0), Access.read);
487 return Result.err(rnd);
491 Namespace namespace = new Namespace(rnd.value);
492 Result<List<String>> rd = func.getOwners(trans, namespace.name, false);
494 namespace.owner = rd.value;
496 rd = func.getAdmins(trans, namespace.name, false);
498 namespace.admin = rd.value;
501 NSS nss = mapper.newInstance(API.NSS);
502 return mapper.nss(trans, namespace, nss);
504 return Result.err(rlnd);
510 path = "/authz/nss/admin/:id",
511 params = { "id|string|true" },
513 errorCodes = { 403,404 },
514 text = { "Lists all Namespaces where Identity :id is an Admin",
515 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
519 public Result<NSS> getNSbyAdmin(AuthzTrans trans, String user, boolean full) {
520 final Validator v = new ServiceValidator();
521 if (v.nullOrBlank("User", user).err()) {
522 return Result.err(Status.ERR_BadData, v.errs());
525 Result<Collection<Namespace>> rn = loadNamepace(trans, user, ".admin", full);
527 return Result.err(rn);
530 return Result.err(Status.ERR_NotFound, "[%s] is not an admin for any namespaces",user);
532 NSS nss = mapper.newInstance(API.NSS);
533 // Note: "loadNamespace" already validates view of Namespace
534 return mapper.nss(trans, rn.value, nss);
539 path = "/authz/nss/either/:id",
540 params = { "id|string|true" },
542 errorCodes = { 403,404 },
543 text = { "Lists all Namespaces where Identity :id is either an Admin or an Owner",
544 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
548 public Result<NSS> getNSbyEither(AuthzTrans trans, String user, boolean full) {
549 final Validator v = new ServiceValidator();
550 if (v.nullOrBlank("User", user).err()) {
551 return Result.err(Status.ERR_BadData, v.errs());
554 Result<Collection<Namespace>> rn = loadNamepace(trans, user, null, full);
556 return Result.err(rn);
559 return Result.err(Status.ERR_NotFound, "[%s] is not an admin or owner for any namespaces",user);
561 NSS nss = mapper.newInstance(API.NSS);
562 // Note: "loadNamespace" already validates view of Namespace
563 return mapper.nss(trans, rn.value, nss);
566 private Result<Collection<Namespace>> loadNamepace(AuthzTrans trans, String user, String endsWith, boolean full) {
567 Result<List<UserRoleDAO.Data>> urd = ques.userRoleDAO.readByUser(trans, user);
568 if (urd.notOKorIsEmpty()) {
569 return Result.err(urd);
571 Map<String, Namespace> lm = new HashMap<>();
572 Map<String, Namespace> other = full || endsWith==null?null:new TreeMap<>();
573 for (UserRoleDAO.Data urdd : urd.value) {
575 if (endsWith==null || urdd.role.endsWith(endsWith)) {
576 RoleDAO.Data rd = RoleDAO.Data.decode(urdd);
577 Result<NsDAO.Data> nsd = ques.mayUser(trans, user, rd, Access.read);
579 Namespace namespace = lm.get(nsd.value.name);
580 if (namespace==null) {
581 namespace = new Namespace(nsd.value);
582 lm.put(namespace.name,namespace);
584 Result<List<String>> rls = func.getAdmins(trans, namespace.name, false);
586 namespace.admin=rls.value;
589 rls = func.getOwners(trans, namespace.name, false);
591 namespace.owner=rls.value;
595 } else { // Shortened version. Only Namespace Info available from Role.
596 if (Question.ADMIN.equals(urdd.rname) || Question.OWNER.equals(urdd.rname)) {
597 RoleDAO.Data rd = RoleDAO.Data.decode(urdd);
598 Result<NsDAO.Data> nsd = ques.mayUser(trans, user, rd, Access.read);
600 Namespace namespace = lm.get(nsd.value.name);
601 if (namespace==null) {
603 namespace = other.remove(nsd.value.name);
605 if (namespace==null) {
606 namespace = new Namespace(nsd.value);
607 namespace.admin=new ArrayList<>();
608 namespace.owner=new ArrayList<>();
610 if (endsWith==null || urdd.role.endsWith(endsWith)) {
611 lm.put(namespace.name,namespace);
613 other.put(namespace.name,namespace);
616 if (Question.OWNER.equals(urdd.rname)) {
617 namespace.owner.add(urdd.user);
619 namespace.admin.add(urdd.user);
625 return Result.ok(lm.values());
630 path = "/authz/nss/responsible/:id",
631 params = { "id|string|true" },
633 errorCodes = { 403,404 },
634 text = { "Lists all Namespaces where Identity :id is a Responsible Party",
635 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
639 public Result<NSS> getNSbyResponsible(AuthzTrans trans, String user, boolean full) {
640 final Validator v = new ServiceValidator();
641 if (v.nullOrBlank("User", user).err()) {
642 return Result.err(Status.ERR_BadData, v.errs());
644 Result<Collection<Namespace>> rn = loadNamepace(trans, user, ".owner",full);
646 return Result.err(rn);
649 return Result.err(Status.ERR_NotFound, "[%s] is not an owner for any namespaces",user);
651 NSS nss = mapper.newInstance(API.NSS);
652 // Note: "loadNamespace" prevalidates
653 return mapper.nss(trans, rn.value, nss);
658 path = "/authz/nss/children/:id",
659 params = { "id|string|true" },
661 errorCodes = { 403,404 },
662 text = { "Lists all Child Namespaces of Namespace :id",
663 "Note: This is not a cached read"
667 public Result<NSS> getNSsChildren(AuthzTrans trans, String parent) {
668 final Validator v = new ServiceValidator();
669 if (v.nullOrBlank("NS", parent).err()) {
670 return Result.err(Status.ERR_BadData,v.errs());
673 Result<NsDAO.Data> rnd = ques.deriveNs(trans, parent);
675 return Result.err(rnd);
677 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
679 return Result.err(rnd);
682 Set<Namespace> lm = new HashSet<>();
683 Result<List<NsDAO.Data>> rlnd = ques.nsDAO.dao().getChildren(trans, parent);
685 if (rlnd.isEmpty()) {
686 return Result.err(Status.ERR_NotFound, "No data found for %s",parent);
688 for (NsDAO.Data ndd : rlnd.value) {
689 Namespace namespace = new Namespace(ndd);
690 Result<List<String>> rls = func.getAdmins(trans, namespace.name, false);
692 namespace.admin=rls.value;
695 rls = func.getOwners(trans, namespace.name, false);
697 namespace.owner=rls.value;
702 NSS nss = mapper.newInstance(API.NSS);
703 return mapper.nss(trans,lm, nss);
705 return Result.err(rlnd);
715 errorCodes = { 403,404,406 },
716 text = { "Replace the Current Description of a Namespace with a new one"
720 public Result<Void> updateNsDescription(AuthzTrans trans, REQUEST from) {
721 final Result<Namespace> nsd = mapper.ns(trans, from);
722 final ServiceValidator v = new ServiceValidator();
723 if (v.ns(nsd).err()) {
724 return Result.err(Status.ERR_BadData,v.errs());
726 if (v.nullOrBlank("description", nsd.value.description).err()) {
727 return Result.err(Status.ERR_BadData,v.errs());
730 Namespace namespace = nsd.value;
731 Result<List<NsDAO.Data>> rlnd = ques.nsDAO.read(trans, namespace.name);
733 if (rlnd.notOKorIsEmpty()) {
734 return Result.err(Status.ERR_NotFound, "Namespace [%s] does not exist",namespace.name);
737 if (ques.mayUser(trans, trans.user(), rlnd.value.get(0), Access.write).notOK()) {
738 return Result.err(Status.ERR_Denied, "You do not have approval to change %s",namespace.name);
741 Result<Void> rdr = ques.nsDAO.dao().addDescription(trans, namespace.name, namespace.description);
745 return Result.err(rdr);
751 * @throws DAOException
752 * @see org.onap.aaf.auth.service.AuthzService#deleteNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.String)
756 path = "/authz/ns/:ns",
757 params = { "ns|string|true" },
759 errorCodes = { 403,404,424 },
760 text = { "Delete the Namespace :ns. Namespaces cannot normally be deleted when there ",
761 "are still credentials associated with them, but they can be deleted by setting ",
762 "the \"force\" property. To do this: Add 'force=true' as a query parameter",
763 "<p>WARNING: Using force will delete all credentials attached to this namespace. Use with care.</p>"
764 + "if the \"force\" property is set to 'force=move', then Permissions and Roles are not deleted,"
765 + "but are retained, and assigned to the Parent Namespace. 'force=move' is not permitted "
766 + "at or below Application Scope"
770 public Result<Void> deleteNS(AuthzTrans trans, String ns) {
771 return func.deleteNS(trans, ns);
775 /***********************************
777 ***********************************/
781 * @see org.onap.aaf.auth.service.AuthzService#createOrUpdatePerm(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object, boolean, java.lang.String, java.lang.String, java.lang.String, java.util.List, java.util.List)
785 path = "/authz/perm",
788 errorCodes = {403,404,406,409},
789 text = { "Permission consists of:",
790 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
791 + "is being protected</li>",
792 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
793 + " instance of the type</li>",
794 "<li>action - what kind of action is allowed</li></ul>",
795 "Note: instance and action can be an *"
799 public Result<Void> createPerm(final AuthzTrans trans,REQUEST rreq) {
800 final Result<PermDAO.Data> newPd = mapper.perm(trans, rreq);
801 final ServiceValidator v = new ServiceValidator();
802 if (v.perm(newPd).err()) {
803 return Result.err(Status.ERR_BadData,v.errs());
806 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, newPd.value,false,
809 public String get() {
810 return "Create Permission [" +
811 newPd.value.fullType() + '|' +
812 newPd.value.instance + '|' +
813 newPd.value.action + ']';
817 private Result<NsDAO.Data> nsd;
819 public Result<?> mayChange() {
821 nsd = ques.mayUser(trans, trans.user(), newPd.value, Access.write);
826 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, newPd.value.ns);
827 if (nsr.notOKorIsEmpty()) {
828 return Result.err(nsr);
832 Result<String> rfc = func.createFuture(trans,fd.value,
833 newPd.value.fullType() + '|' + newPd.value.instance + '|' + newPd.value.action,
838 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
841 newPd.value.instance,
844 return Result.err(rfc);
847 return func.createPerm(trans, newPd.value, true);
849 return Result.err(fd);
855 path = "/authz/perms/:type",
856 params = {"type|string|true"},
858 errorCodes = { 404,406 },
859 text = { "List All Permissions that match the :type element of the key" }
862 public Result<PERMS> getPermsByType(AuthzTrans trans, final String permType) {
863 final Validator v = new ServiceValidator();
864 if (v.nullOrBlank("PermType", permType).err()) {
865 return Result.err(Status.ERR_BadData,v.errs());
868 Result<List<PermDAO.Data>> rlpd = ques.getPermsByType(trans, permType);
870 return Result.err(rlpd);
873 // We don't have instance & action for mayUserView... do we want to loop through all returned here as well as in mapper?
874 // Result<NsDAO.Data> r;
875 // if ((r = ques.mayUserViewPerm(trans, trans.user(), permType)).notOK())return Result.err(r);
877 PERMS perms = mapper.newInstance(API.PERMS);
878 if (!rlpd.isEmpty()) {
879 // Note: Mapper will restrict what can be viewed
880 return mapper.perms(trans, rlpd.value, perms, true);
882 return Result.ok(perms);
887 path = "/authz/perms/:type/:instance/:action",
888 params = {"type|string|true",
889 "instance|string|true",
890 "action|string|true"},
892 errorCodes = { 404,406 },
893 text = { "List Permissions that match key; :type, :instance and :action" }
896 public Result<PERMS> getPermsByName(AuthzTrans trans, String type, String instance, String action) {
897 final Validator v = new ServiceValidator();
898 if (v.nullOrBlank("PermType", type).err()
899 || v.nullOrBlank("PermInstance", instance).err()
900 || v.nullOrBlank("PermAction", action).err()) {
901 return Result.err(Status.ERR_BadData,v.errs());
904 Result<List<PermDAO.Data>> rlpd = ques.getPermsByName(trans, type, instance, action);
906 return Result.err(rlpd);
909 PERMS perms = mapper.newInstance(API.PERMS);
910 if (!rlpd.isEmpty()) {
911 // Note: Mapper will restrict what can be viewed
912 return mapper.perms(trans, rlpd.value, perms, true);
914 return Result.ok(perms);
919 path = "/authz/perms/user/:user",
920 params = {"user|string|true"},
922 errorCodes = { 404,406 },
923 text = { "List All Permissions that match user :user",
924 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>"}
927 public Result<PERMS> getPermsByUser(AuthzTrans trans, String user) {
928 final Validator v = new ServiceValidator();
929 if (v.nullOrBlank("User", user).err()) {
930 return Result.err(Status.ERR_BadData,v.errs());
933 Result<List<PermDAO.Data>> rlpd = ques.getPermsByUser(trans, user,
934 trans.requested(force));
936 return Result.err(rlpd);
939 PERMS perms = mapper.newInstance(API.PERMS);
941 if (rlpd.isEmpty()) {
942 return Result.ok(perms);
944 // Note: Mapper will restrict what can be viewed
945 // if user is the same as that which is looked up, no filtering is required
946 return mapper.perms(trans, rlpd.value,
948 !user.equals(trans.user()));
953 path = "/authz/perms/user/:user/scope/:scope",
954 params = {"user|string|true","scope|string|true"},
956 errorCodes = { 404,406 },
957 text = { "List All Permissions that match user :user, filtered by NS (Scope)",
958 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>",
959 "<p>'scope' must be expressed as NSs separated by ':'</p>"
963 public Result<PERMS> getPermsByUserScope(AuthzTrans trans, String user, String[] scopes) {
964 final Validator v = new ServiceValidator();
965 if (v.nullOrBlank("User", user).err()) {
966 return Result.err(Status.ERR_BadData,v.errs());
969 Result<List<PermDAO.Data>> rlpd = ques.getPermsByUser(trans, user, trans.requested(force));
971 return Result.err(rlpd);
974 PERMS perms = mapper.newInstance(API.PERMS);
976 if (rlpd.isEmpty()) {
977 return Result.ok(perms);
979 // Note: Mapper will restrict what can be viewed
980 // if user is the same as that which is looked up, no filtering is required
981 return mapper.perms(trans, rlpd.value,
984 !user.equals(trans.user()));
989 path = "/authz/perms/user/:user",
990 params = {"user|string|true"},
992 errorCodes = { 404,406 },
993 text = { "List All Permissions that match user :user",
994 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>",
996 "Present Queries as one or more Permissions (see ContentType Links below for format).",
998 "If the Caller is Granted this specific Permission, and the Permission is valid",
999 " for the User, it will be included in response Permissions, along with",
1000 " all the normal permissions on the 'GET' version of this call. If it is not",
1001 " valid, or Caller does not have permission to see, it will be removed from the list",
1003 " *Note: This design allows you to make one call for all expected permissions",
1004 " The permission to be included MUST be:",
1005 " <user namespace>.access|:<ns|role|perm>[:key]|<create|read|write>",
1007 " com.att.myns.access|:ns|write",
1008 " com.att.myns.access|:role:myrole|create",
1009 " com.att.myns.access|:perm:mytype:myinstance:myaction|read",
1014 public Result<PERMS> getPermsByUser(AuthzTrans trans, PERMS _perms, String user) {
1015 PERMS perms = _perms;
1016 final Validator v = new ServiceValidator();
1017 if (v.nullOrBlank("User", user).err()) {
1018 return Result.err(Status.ERR_BadData,v.errs());
1022 Result<List<PermDAO.Data>> rlpd = ques.getPermsByUser(trans, user,trans.requested(force));
1024 return Result.err(rlpd);
1028 1) See if allowed to query
1029 2) See if User is allowed
1031 Result<List<PermDAO.Data>> in = mapper.perms(trans, perms);
1032 if (in.isOKhasData()) {
1033 List<PermDAO.Data> out = rlpd.value;
1035 for (PermDAO.Data pdd : in.value) {
1037 if ("access".equals(pdd.type)) {
1038 Access access = Access.valueOf(pdd.action);
1039 String[] mdkey = Split.splitTrim(':',pdd.instance);
1040 if (mdkey.length>1) {
1041 String type = mdkey[1];
1042 if ("role".equals(type)) {
1043 if (mdkey.length>2) {
1044 RoleDAO.Data rdd = new RoleDAO.Data();
1047 ok = ques.mayUser(trans, trans.user(), rdd, Access.read).isOK() && ques.mayUser(trans, user, rdd , access).isOK();
1049 } else if ("perm".equals(type)) {
1050 if (mdkey.length>4) { // also need instance/action
1051 PermDAO.Data p = new PermDAO.Data();
1054 p.instance=mdkey[3];
1056 ok = ques.mayUser(trans, trans.user(), p, Access.read).isOK() && ques.mayUser(trans, user, p , access).isOK();
1058 } else if ("ns".equals(type)) {
1059 NsDAO.Data ndd = new NsDAO.Data();
1061 ok = ques.mayUser(trans, trans.user(), ndd, Access.read).isOK() && ques.mayUser(trans, user, ndd , access).isOK();
1071 perms = mapper.newInstance(API.PERMS);
1072 if (rlpd.isEmpty()) {
1073 return Result.ok(perms);
1075 // Note: Mapper will restrict what can be viewed
1076 // if user is the same as that which is looked up, no filtering is required
1077 return mapper.perms(trans, rlpd.value,
1079 !user.equals(trans.user()));
1084 path = "/authz/perms/role/:role",
1085 params = {"role|string|true"},
1087 errorCodes = { 404,406 },
1088 text = { "List All Permissions that are granted to :role" }
1091 public Result<PERMS> getPermsByRole(AuthzTrans trans,String role) {
1092 final Validator v = new ServiceValidator();
1093 if (v.nullOrBlank("Role", role).err()) {
1094 return Result.err(Status.ERR_BadData,v.errs());
1097 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques,role);
1099 return Result.err(rrdd);
1102 Result<NsDAO.Data> r = ques.mayUser(trans, trans.user(), rrdd.value, Access.read);
1104 return Result.err(r);
1107 PERMS perms = mapper.newInstance(API.PERMS);
1109 Result<List<PermDAO.Data>> rlpd = ques.getPermsByRole(trans, role, trans.requested(force));
1110 if (rlpd.isOKhasData()) {
1111 // Note: Mapper will restrict what can be viewed
1112 return mapper.perms(trans, rlpd.value, perms, true);
1114 return Result.ok(perms);
1119 path = "/authz/perms/ns/:ns",
1120 params = {"ns|string|true"},
1122 errorCodes = { 404,406 },
1123 text = { "List All Permissions that are in Namespace :ns" }
1126 public Result<PERMS> getPermsByNS(AuthzTrans trans,String ns) {
1127 final Validator v = new ServiceValidator();
1128 if (v.nullOrBlank("NS", ns).err()) {
1129 return Result.err(Status.ERR_BadData,v.errs());
1132 Result<NsDAO.Data> rnd = ques.deriveNs(trans, ns);
1134 return Result.err(rnd);
1137 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
1139 return Result.err(rnd);
1142 Result<List<PermDAO.Data>> rlpd = ques.permDAO.readNS(trans, ns);
1144 return Result.err(rlpd);
1147 PERMS perms = mapper.newInstance(API.PERMS);
1148 if (!rlpd.isEmpty()) {
1149 // Note: Mapper will restrict what can be viewed
1150 return mapper.perms(trans, rlpd.value,perms, true);
1152 return Result.ok(perms);
1157 path = "/authz/perm/:type/:instance/:action",
1158 params = {"type|string|true",
1159 "instance|string|true",
1160 "action|string|true"},
1162 errorCodes = { 404,406, 409 },
1163 text = { "Rename the Permission referenced by :type :instance :action, and "
1164 + "rename (copy/delete) to the Permission described in PermRequest" }
1167 public Result<Void> renamePerm(final AuthzTrans trans,REQUEST rreq, String origType, String origInstance, String origAction) {
1168 final Result<PermDAO.Data> newPd = mapper.perm(trans, rreq);
1169 final ServiceValidator v = new ServiceValidator();
1170 if (v.perm(newPd).err()) {
1171 return Result.err(Status.ERR_BadData,v.errs());
1174 if (ques.mayUser(trans, trans.user(), newPd.value,Access.write).notOK()) {
1175 return Result.err(Status.ERR_Denied, "You do not have approval to change Permission [%s.%s|%s|%s]",
1176 newPd.value.ns,newPd.value.type,newPd.value.instance,newPd.value.action);
1179 Result<NsSplit> nss = ques.deriveNsSplit(trans, origType);
1180 Result<List<PermDAO.Data>> origRlpd = ques.permDAO.read(trans, nss.value.ns, nss.value.name, origInstance, origAction);
1182 if (origRlpd.notOKorIsEmpty()) {
1183 return Result.err(Status.ERR_PermissionNotFound,
1184 "Permission [%s|%s|%s] does not exist",
1185 origType,origInstance,origAction);
1188 PermDAO.Data origPd = origRlpd.value.get(0);
1190 if (!origPd.ns.equals(newPd.value.ns)) {
1191 return Result.err(Status.ERR_Denied, "Cannot change namespace with rename command. " +
1192 "<new type> must start with [" + origPd.ns + "]");
1195 if ( origPd.type.equals(newPd.value.type) &&
1196 origPd.action.equals(newPd.value.action) &&
1197 origPd.instance.equals(newPd.value.instance) ) {
1198 return Result.err(Status.ERR_ConflictAlreadyExists, "New Permission must be different than original permission");
1201 Set<String> origRoles = origPd.roles(false);
1202 if (!origRoles.isEmpty()) {
1203 Set<String> roles = newPd.value.roles(true);
1204 for (String role : origPd.roles) {
1209 newPd.value.description = origPd.description;
1211 Result<Void> rv = null;
1213 rv = func.createPerm(trans, newPd.value, false);
1215 rv = func.deletePerm(trans, origPd, true, false);
1222 path = "/authz/perm",
1225 errorCodes = { 404,406 },
1226 text = { "Add Description Data to Perm" }
1229 public Result<Void> updatePermDescription(AuthzTrans trans, REQUEST from) {
1230 final Result<PermDAO.Data> pd = mapper.perm(trans, from);
1231 final ServiceValidator v = new ServiceValidator();
1232 if (v.perm(pd).err()) {
1233 return Result.err(Status.ERR_BadData,v.errs());
1235 if (v.nullOrBlank("description", pd.value.description).err()) {
1236 return Result.err(Status.ERR_BadData,v.errs());
1238 final PermDAO.Data perm = pd.value;
1239 if (ques.permDAO.read(trans, perm.ns, perm.type, perm.instance,perm.action).notOKorIsEmpty()) {
1240 return Result.err(Status.ERR_NotFound, "Permission [%s.%s|%s|%s] does not exist",
1241 perm.ns,perm.type,perm.instance,perm.action);
1244 if (ques.mayUser(trans, trans.user(), perm, Access.write).notOK()) {
1245 return Result.err(Status.ERR_Denied, "You do not have approval to change Permission [%s.%s|%s|%s]",
1246 perm.ns,perm.type,perm.instance,perm.action);
1249 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, pd.value.ns);
1250 if (nsr.notOKorIsEmpty()) {
1251 return Result.err(nsr);
1254 Result<Void> rdr = ques.permDAO.addDescription(trans, perm.ns, perm.type, perm.instance,
1255 perm.action, perm.description);
1259 return Result.err(rdr);
1266 path = "/authz/role/perm",
1269 errorCodes = {403,404,406,409},
1270 text = { "Set a permission's roles to roles given" }
1274 public Result<Void> resetPermRoles(final AuthzTrans trans, REQUEST rreq) {
1275 final Result<PermDAO.Data> updt = mapper.permFromRPRequest(trans, rreq);
1276 if (updt.notOKorIsEmpty()) {
1277 return Result.err(updt);
1280 final ServiceValidator v = new ServiceValidator();
1281 if (v.perm(updt).err()) {
1282 return Result.err(Status.ERR_BadData,v.errs());
1285 Result<NsDAO.Data> nsd = ques.mayUser(trans, trans.user(), updt.value, Access.write);
1287 return Result.err(nsd);
1290 // Read full set to get CURRENT values
1291 Result<List<PermDAO.Data>> rcurr = ques.permDAO.read(trans,
1294 updt.value.instance,
1297 if (rcurr.notOKorIsEmpty()) {
1298 return Result.err(Status.ERR_PermissionNotFound,
1299 "Permission [%s.%s|%s|%s] does not exist",
1300 updt.value.ns,updt.value.type,updt.value.instance,updt.value.action);
1303 // Create a set of Update Roles, which are in Internal Format
1304 Set<String> updtRoles = new HashSet<>();
1305 Result<NsSplit> nss;
1306 for (String role : updt.value.roles(false)) {
1307 nss = ques.deriveNsSplit(trans, role);
1309 updtRoles.add(nss.value.ns + '|' + nss.value.name);
1311 trans.error().log(nss.errorString());
1315 Result<Void> rv = null;
1317 for (PermDAO.Data curr : rcurr.value) {
1318 Set<String> currRoles = curr.roles(false);
1319 // must add roles to this perm, and add this perm to each role
1320 // in the update, but not in the current
1321 for (String role : updtRoles) {
1322 if (!currRoles.contains(role)) {
1323 Result<RoleDAO.Data> key = RoleDAO.Data.decode(trans, ques, role);
1324 if (key.isOKhasData()) {
1325 Result<List<RoleDAO.Data>> rrd = ques.roleDAO.read(trans, key.value);
1326 if (rrd.isOKhasData()) {
1327 for (RoleDAO.Data r : rrd.value) {
1328 rv = func.addPermToRole(trans, r, curr, false);
1329 if (rv.notOK() && rv.status!=Result.ERR_ConflictAlreadyExists) {
1330 return Result.err(rv);
1334 return Result.err(rrd);
1339 // similarly, must delete roles from this perm, and delete this perm from each role
1340 // in the update, but not in the current
1341 for (String role : currRoles) {
1342 if (!updtRoles.contains(role)) {
1343 Result<RoleDAO.Data> key = RoleDAO.Data.decode(trans, ques, role);
1344 if (key.isOKhasData()) {
1345 Result<List<RoleDAO.Data>> rdd = ques.roleDAO.read(trans, key.value);
1346 if (rdd.isOKhasData()) {
1347 for (RoleDAO.Data r : rdd.value) {
1348 rv = func.delPermFromRole(trans, r, curr, true);
1349 if (rv.notOK() && rv.status!=Status.ERR_PermissionNotFound) {
1350 return Result.err(rv);
1358 return rv==null?Result.ok():rv;
1363 path = "/authz/perm",
1366 errorCodes = { 404,406 },
1367 text = { "Delete the Permission referenced by PermKey.",
1368 "You cannot normally delete a permission which is still granted to roles,",
1369 "however the \"force\" property allows you to do just that. To do this: Add",
1370 "'force=true' as a query parameter.",
1371 "<p>WARNING: Using force will ungrant this permission from all roles. Use with care.</p>" }
1374 public Result<Void> deletePerm(final AuthzTrans trans, REQUEST from) {
1375 Result<PermDAO.Data> pd = mapper.perm(trans, from);
1377 return Result.err(pd);
1379 final ServiceValidator v = new ServiceValidator();
1380 if (v.nullOrBlank(pd.value).err()) {
1381 return Result.err(Status.ERR_BadData,v.errs());
1383 final PermDAO.Data perm = pd.value;
1384 if (ques.permDAO.read(trans, perm).notOKorIsEmpty()) {
1385 return Result.err(Status.ERR_PermissionNotFound, "Permission [%s.%s|%s|%s] does not exist",
1386 perm.ns,perm.type,perm.instance,perm.action );
1389 Result<FutureDAO.Data> fd = mapper.future(trans,PermDAO.TABLE,from,perm,false,
1392 public String get() {
1393 return "Delete Permission [" + perm.fullPerm() + ']';
1397 private Result<NsDAO.Data> nsd;
1399 public Result<?> mayChange() {
1401 nsd = ques.mayUser(trans, trans.user(), perm, Access.write);
1409 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, perm.ns);
1410 if (nsr.notOKorIsEmpty()) {
1411 return Result.err(nsr);
1414 Result<String> rfc = func.createFuture(trans, fd.value,
1415 perm.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.D);
1417 return Result.err(Status.ACC_Future, "Perm Deletion [%s] is saved for future processing",perm.encode());
1419 return Result.err(rfc);
1421 case Status.ACC_Now:
1422 return func.deletePerm(trans,perm,trans.requested(force), false);
1424 return Result.err(fd);
1430 path = "/authz/perm/:name/:type/:action",
1431 params = {"type|string|true",
1432 "instance|string|true",
1433 "action|string|true"},
1435 errorCodes = { 404,406 },
1436 text = { "Delete the Permission referenced by :type :instance :action",
1437 "You cannot normally delete a permission which is still granted to roles,",
1438 "however the \"force\" property allows you to do just that. To do this: Add",
1439 "'force=true' as a query parameter",
1440 "<p>WARNING: Using force will ungrant this permission from all roles. Use with care.</p>"}
1443 public Result<Void> deletePerm(AuthzTrans trans, String type, String instance, String action) {
1444 final Validator v = new ServiceValidator();
1445 if (v.nullOrBlank("Type",type)
1446 .nullOrBlank("Instance",instance)
1447 .nullOrBlank("Action",action)
1449 return Result.err(Status.ERR_BadData,v.errs());
1452 Result<PermDAO.Data> pd = ques.permFrom(trans, type, instance, action);
1454 return func.deletePerm(trans, pd.value, trans.requested(force), false);
1456 return Result.err(pd);
1460 /***********************************
1462 ***********************************/
1465 path = "/authz/role",
1468 errorCodes = {403,404,406,409},
1471 "Roles are part of Namespaces",
1473 "<ul><li>org.onap.aaf - The team that created and maintains AAF</li>",
1474 "Roles do not include implied permissions for an App. Instead, they contain explicit Granted Permissions by any Namespace in AAF (See Permissions)",
1475 "Restrictions on Role Names:",
1476 "<ul><li>Must start with valid Namespace name, terminated by . (dot/period)</li>",
1477 "<li>Allowed Characters are a-zA-Z0-9._-</li>",
1478 "<li>role names are Case Sensitive</li></ul>",
1479 "The right questions to ask for defining and populating a Role in AAF, therefore, are:",
1480 "<ul><li>'What Job Function does this represent?'</li>",
1481 "<li>'Does this person perform this Job Function?'</li></ul>" }
1485 public Result<Void> createRole(final AuthzTrans trans, REQUEST from) {
1486 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
1487 final ServiceValidator v = new ServiceValidator();
1488 if (v.role(rd).err()) {
1489 return Result.err(Status.ERR_BadData,v.errs());
1491 final RoleDAO.Data role = rd.value;
1492 if (ques.roleDAO.read(trans, role.ns, role.name).isOKhasData()) {
1493 return Result.err(Status.ERR_ConflictAlreadyExists, "Role [" + role.fullName() + "] already exists");
1496 Result<FutureDAO.Data> fd = mapper.future(trans,RoleDAO.TABLE,from,role,false,
1499 public String get() {
1500 return "Create Role [" +
1501 rd.value.fullName() +
1506 private Result<NsDAO.Data> nsd;
1508 public Result<?> mayChange() {
1510 nsd = ques.mayUser(trans, trans.user(), role, Access.write);
1516 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rd.value.ns);
1517 if (nsr.notOKorIsEmpty()) {
1518 return Result.err(nsr);
1523 Result<String> rfc = func.createFuture(trans, fd.value,
1524 role.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.C);
1526 return Result.err(Status.ACC_Future, "Role [%s.%s] is saved for future processing",
1530 return Result.err(rfc);
1532 case Status.ACC_Now:
1533 Result<RoleDAO.Data> rdr = ques.roleDAO.create(trans, role);
1537 return Result.err(rdr);
1540 return Result.err(fd);
1545 * @see org.onap.aaf.auth.service.AuthzService#getRolesByName(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1549 path = "/authz/roles/:role",
1550 params = {"role|string|true"},
1552 errorCodes = {404,406},
1553 text = { "List Roles that match :role",
1554 "Note: You must have permission to see any given role"
1558 public Result<ROLES> getRolesByName(AuthzTrans trans, String role) {
1559 final Validator v = new ServiceValidator();
1560 if (v.nullOrBlank("Role", role).err()) {
1561 return Result.err(Status.ERR_BadData,v.errs());
1564 // Determine if User can ask this question
1565 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
1566 if (rrdd.isOKhasData()) {
1567 Result<NsDAO.Data> r;
1568 if ((r = ques.mayUser(trans, trans.user(), rrdd.value, Access.read)).notOK()) {
1569 return Result.err(r);
1572 return Result.err(rrdd);
1576 int query = role.indexOf('?');
1577 Result<List<RoleDAO.Data>> rlrd = ques.getRolesByName(trans, query<0?role:role.substring(0, query));
1579 // Note: Mapper will restrict what can be viewed
1580 ROLES roles = mapper.newInstance(API.ROLES);
1581 return mapper.roles(trans, rlrd.value, roles, true);
1583 return Result.err(rlrd);
1588 * @see org.onap.aaf.auth.service.AuthzService#getRolesByUser(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1592 path = "/authz/roles/user/:name",
1593 params = {"name|string|true"},
1595 errorCodes = {404,406},
1596 text = { "List all Roles that match user :name",
1597 "'user' must be expressed as full identity (ex: id@full.domain.com)",
1598 "Note: You must have permission to see any given role"
1603 public Result<ROLES> getRolesByUser(AuthzTrans trans, String user) {
1604 final Validator v = new ServiceValidator();
1605 if (v.nullOrBlank("User", user).err()) {
1606 return Result.err(Status.ERR_BadData,v.errs());
1609 ROLES roles = mapper.newInstance(API.ROLES);
1610 // Get list of roles per user, then add to Roles as we go
1611 Result<List<RoleDAO.Data>> rlrd;
1612 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByUser(trans, user);
1613 if (rlurd.isOKhasData()) {
1614 for (UserRoleDAO.Data urd : rlurd.value ) {
1615 rlrd = ques.roleDAO.read(trans, urd.ns,urd.rname);
1616 // Note: Mapper will restrict what can be viewed
1617 // if user is the same as that which is looked up, no filtering is required
1618 if (rlrd.isOKhasData()) {
1619 mapper.roles(trans, rlrd.value,roles, !user.equals(trans.user()));
1623 return Result.ok(roles);
1628 * @see org.onap.aaf.auth.service.AuthzService#getRolesByNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1632 path = "/authz/roles/ns/:ns",
1633 params = {"ns|string|true"},
1635 errorCodes = {404,406},
1636 text = { "List all Roles for the Namespace :ns",
1637 "Note: You must have permission to see any given role"
1642 public Result<ROLES> getRolesByNS(AuthzTrans trans, String ns) {
1643 final Validator v = new ServiceValidator();
1644 if (v.nullOrBlank("NS", ns).err()) {
1645 return Result.err(Status.ERR_BadData,v.errs());
1648 // check if user is allowed to view NS
1649 Result<NsDAO.Data> rnsd = ques.deriveNs(trans, ns);
1651 return Result.err(rnsd);
1653 rnsd = ques.mayUser(trans, trans.user(), rnsd.value, Access.read);
1655 return Result.err(rnsd);
1658 TimeTaken tt = trans.start("MAP Roles by NS to Roles", Env.SUB);
1660 ROLES roles = mapper.newInstance(API.ROLES);
1661 // Get list of roles per user, then add to Roles as we go
1662 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO.readNS(trans, ns);
1664 if (!rlrd.isEmpty()) {
1665 // Note: Mapper doesn't need to restrict what can be viewed, because we did it already.
1666 mapper.roles(trans,rlrd.value,roles,false);
1668 return Result.ok(roles);
1670 return Result.err(rlrd);
1679 * @see org.onap.aaf.auth.service.AuthzService#getRolesByNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1683 path = "/authz/roles/name/:name",
1684 params = {"name|string|true"},
1686 errorCodes = {404,406},
1687 text = { "List all Roles for only the Name of Role (without Namespace)",
1688 "Note: You must have permission to see any given role"
1692 public Result<ROLES> getRolesByNameOnly(AuthzTrans trans, String name) {
1693 final Validator v = new ServiceValidator();
1694 if (v.nullOrBlank("Name", name).err()) {
1695 return Result.err(Status.ERR_BadData,v.errs());
1698 // User Mapper to make sure user is allowed to view NS
1700 TimeTaken tt = trans.start("MAP Roles by Name to Roles", Env.SUB);
1702 ROLES roles = mapper.newInstance(API.ROLES);
1703 // Get list of roles per user, then add to Roles as we go
1704 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO.readName(trans, name);
1706 if (!rlrd.isEmpty()) {
1707 // Note: Mapper will restrict what can be viewed
1708 mapper.roles(trans,rlrd.value,roles,true);
1710 return Result.ok(roles);
1712 return Result.err(rlrd);
1721 path = "/authz/roles/perm/:type/:instance/:action",
1722 params = {"type|string|true",
1723 "instance|string|true",
1724 "action|string|true"},
1726 errorCodes = {404,406},
1727 text = { "Find all Roles containing the given Permission." +
1728 "Permission consists of:",
1729 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
1730 + "is being protected</li>",
1731 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
1732 + " instance of the type</li>",
1733 "<li>action - what kind of action is allowed</li></ul>",
1734 "Notes: instance and action can be an *",
1735 " You must have permission to see any given role"
1740 public Result<ROLES> getRolesByPerm(AuthzTrans trans, String type, String instance, String action) {
1741 final Validator v = new ServiceValidator();
1742 if (v.permType(type)
1743 .permInstance(instance)
1746 return Result.err(Status.ERR_BadData,v.errs());
1749 TimeTaken tt = trans.start("Map Perm Roles Roles", Env.SUB);
1751 ROLES roles = mapper.newInstance(API.ROLES);
1752 // Get list of roles per user, then add to Roles as we go
1753 Result<NsSplit> nsSplit = ques.deriveNsSplit(trans, type);
1754 if (nsSplit.isOK()) {
1755 PermDAO.Data pdd = new PermDAO.Data(nsSplit.value, instance, action);
1757 if ((res=ques.mayUser(trans, trans.user(), pdd, Question.Access.read)).notOK()) {
1758 return Result.err(res);
1761 Result<List<PermDAO.Data>> pdlr = ques.permDAO.read(trans, pdd);
1762 if (pdlr.isOK())for (PermDAO.Data pd : pdlr.value) {
1763 Result<List<RoleDAO.Data>> rlrd;
1764 for (String r : pd.roles) {
1765 Result<String[]> rs = RoleDAO.Data.decodeToArray(trans, ques, r);
1767 rlrd = ques.roleDAO.read(trans, rs.value[0],rs.value[1]);
1768 // Note: Mapper will restrict what can be viewed
1769 if (rlrd.isOKhasData()) {
1770 mapper.roles(trans,rlrd.value,roles,true);
1776 return Result.ok(roles);
1784 path = "/authz/role",
1787 errorCodes = {404,406},
1788 text = { "Add Description Data to a Role" }
1792 public Result<Void> updateRoleDescription(AuthzTrans trans, REQUEST from) {
1793 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
1794 final ServiceValidator v = new ServiceValidator();
1795 if (v.role(rd).err()) {
1796 return Result.err(Status.ERR_BadData,v.errs());
1798 if (v.nullOrBlank("description", rd.value.description).err()) {
1799 return Result.err(Status.ERR_BadData,v.errs());
1802 final RoleDAO.Data role = rd.value;
1803 if (ques.roleDAO.read(trans, role.ns, role.name).notOKorIsEmpty()) {
1804 return Result.err(Status.ERR_NotFound, "Role [" + role.fullName() + "] does not exist");
1807 if (ques.mayUser(trans, trans.user(), role, Access.write).notOK()) {
1808 return Result.err(Status.ERR_Denied, "You do not have approval to change " + role.fullName());
1811 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rd.value.ns);
1812 if (nsr.notOKorIsEmpty()) {
1813 return Result.err(nsr);
1816 Result<Void> rdr = ques.roleDAO.addDescription(trans, role.ns, role.name, role.description);
1820 return Result.err(rdr);
1827 path = "/authz/role/perm",
1830 errorCodes = {403,404,406,409},
1831 text = { "Grant a Permission to a Role",
1832 "Permission consists of:",
1833 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
1834 + "is being protected</li>",
1835 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
1836 + " instance of the type</li>",
1837 "<li>action - what kind of action is allowed</li></ul>",
1838 "Note: instance and action can be an *",
1839 "Note: Using the \"force\" property will create the Permission, if it doesn't exist AND the requesting " +
1840 " ID is allowed to create. It will then grant",
1841 " the permission to the role in one step. To do this: add 'force=true' as a query parameter."
1846 public Result<Void> addPermToRole(final AuthzTrans trans, REQUEST rreq) {
1847 // Translate Request into Perm and Role Objects
1848 final Result<PermDAO.Data> rpd = mapper.permFromRPRequest(trans, rreq);
1849 if (rpd.notOKorIsEmpty()) {
1850 return Result.err(rpd);
1852 final Result<RoleDAO.Data> rrd = mapper.roleFromRPRequest(trans, rreq);
1853 if (rrd.notOKorIsEmpty()) {
1854 return Result.err(rrd);
1857 // Validate Role and Perm values
1858 final ServiceValidator v = new ServiceValidator();
1859 if (v.perm(rpd.value)
1862 return Result.err(Status.ERR_BadData,v.errs());
1865 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO.read(trans, rrd.value.ns, rrd.value.name);
1866 if (rlrd.notOKorIsEmpty()) {
1867 return Result.err(Status.ERR_RoleNotFound, "Role [%s] does not exist", rrd.value.fullName());
1870 // Check Status of Data in DB (does it exist)
1871 Result<List<PermDAO.Data>> rlpd = ques.permDAO.read(trans, rpd.value.ns,
1872 rpd.value.type, rpd.value.instance, rpd.value.action);
1873 PermDAO.Data createPerm = null; // if not null, create first
1874 if (rlpd.notOKorIsEmpty()) { // Permission doesn't exist
1875 if (trans.requested(force)) {
1876 // Remove roles from perm data object so we just create the perm here
1877 createPerm = rpd.value;
1878 createPerm.roles.clear();
1880 return Result.err(Status.ERR_PermissionNotFound,"Permission [%s.%s|%s|%s] does not exist",
1881 rpd.value.ns,rpd.value.type,rpd.value.instance,rpd.value.action);
1884 if (rlpd.value.get(0).roles(false).contains(rrd.value.encode())) {
1885 return Result.err(Status.ERR_ConflictAlreadyExists,
1886 "Permission [%s.%s|%s|%s] already granted to Role [%s.%s]",
1887 rpd.value.ns,rpd.value.type,rpd.value.instance,rpd.value.action,
1888 rrd.value.ns,rrd.value.name
1894 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, rpd.value,true, // Allow grants to create Approvals
1897 public String get() {
1898 return "Grant Permission [" + rpd.value.fullPerm() + ']' +
1899 " to Role [" + rrd.value.fullName() + "]";
1903 private Result<NsDAO.Data> nsd;
1905 public Result<?> mayChange() {
1907 nsd = ques.mayUser(trans, trans.user(), rpd.value, Access.write);
1912 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rpd.value.ns);
1913 if (nsr.notOKorIsEmpty()) {
1914 return Result.err(nsr);
1918 Result<String> rfc = func.createFuture(trans,fd.value,
1919 rpd.value.fullPerm(),
1924 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
1930 return Result.err(rfc);
1932 case Status.ACC_Now:
1933 Result<Void> rv = null;
1934 if (createPerm!=null) {// has been validated for creating
1935 rv = func.createPerm(trans, createPerm, false);
1937 if (rv==null || rv.isOK()) {
1938 rv = func.addPermToRole(trans, rrd.value, rpd.value, false);
1942 return Result.err(fd);
1948 * Delete Perms from Roles (UnGrant)
1950 * @param roleFullName
1955 path = "/authz/role/:role/perm",
1956 params = {"role|string|true"},
1958 errorCodes = {404,406},
1959 text = { "Ungrant a permission from Role :role" }
1963 public Result<Void> delPermFromRole(final AuthzTrans trans, REQUEST rreq) {
1964 final Result<PermDAO.Data> updt = mapper.permFromRPRequest(trans, rreq);
1965 if (updt.notOKorIsEmpty()) {
1966 return Result.err(updt);
1968 final Result<RoleDAO.Data> rrd = mapper.roleFromRPRequest(trans, rreq);
1969 if (rrd.notOKorIsEmpty()) {
1970 return Result.err(rrd);
1973 final ServiceValidator v = new ServiceValidator();
1974 if (v.nullOrBlank(updt.value)
1975 .nullOrBlank(rrd.value)
1977 return Result.err(Status.ERR_BadData,v.errs());
1980 return delPermFromRole(trans, updt.value,rrd.value, rreq);
1983 private Result<Void> delPermFromRole(final AuthzTrans trans, PermDAO.Data pdd, RoleDAO.Data rdd, REQUEST rreq) {
1984 Result<List<PermDAO.Data>> rlpd = ques.permDAO.read(trans, pdd.ns, pdd.type,
1985 pdd.instance, pdd.action);
1987 if (rlpd.notOKorIsEmpty()) {
1988 return Result.err(Status.ERR_PermissionNotFound,
1989 "Permission [%s.%s|%s|%s] does not exist",
1990 pdd.ns,pdd.type,pdd.instance,pdd.action);
1993 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, pdd,true, // allow ungrants requests
1996 public String get() {
1997 return "Ungrant Permission [" + pdd.fullPerm() + ']' +
1998 " from Role [" + rdd.fullName() + "]";
2002 private Result<NsDAO.Data> nsd;
2004 public Result<?> mayChange() {
2006 nsd = ques.mayUser(trans, trans.user(), pdd, Access.write);
2011 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, pdd.ns);
2012 if (nsr.notOKorIsEmpty()) {
2013 return Result.err(nsr);
2017 Result<String> rfc = func.createFuture(trans,fd.value,
2024 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
2030 return Result.err(rfc);
2032 case Status.ACC_Now:
2033 return func.delPermFromRole(trans, rdd, pdd, false);
2035 return Result.err(fd);
2042 path = "/authz/role/:role/perm/:type/:instance/:action",
2043 params = {"role|string|true",
2044 "perm type|string|true",
2045 "perm instance|string|true",
2046 "perm action|string|true"
2049 errorCodes = {404,406},
2050 text = { "Ungrant a single permission from Role :role with direct key" }
2054 public Result<Void> delPermFromRole(AuthzTrans trans, String role, String type, String instance, String action) {
2055 Result<Data> rpns = ques.deriveNs(trans, type);
2056 if (rpns.notOKorIsEmpty()) {
2057 return Result.err(rpns);
2060 final Validator v = new ServiceValidator();
2062 .permType(rpns.value.name,rpns.value.parent)
2063 .permInstance(instance)
2066 return Result.err(Status.ERR_BadData,v.errs());
2069 Result<Data> rrns = ques.deriveNs(trans, role);
2070 if (rrns.notOKorIsEmpty()) {
2071 return Result.err(rrns);
2074 final Result<List<RoleDAO.Data>> rrd = ques.roleDAO.read(trans, rrns.value.parent, rrns.value.name);
2075 if (rrd.notOKorIsEmpty()) {
2076 return Result.err(rrd);
2079 final Result<List<PermDAO.Data>> rpd = ques.permDAO.read(trans, rpns.value.parent, rpns.value.name, instance, action);
2080 if (rpd.notOKorIsEmpty()) {
2081 return Result.err(rpd);
2085 return delPermFromRole(trans,rpd.value.get(0), rrd.value.get(0), mapper.ungrantRequest(trans, role, type, instance, action));
2090 path = "/authz/role/:role",
2091 params = {"role|string|true"},
2093 errorCodes = {404,406},
2094 text = { "Delete the Role named :role"}
2098 public Result<Void> deleteRole(AuthzTrans trans, String role) {
2099 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
2100 if (rrdd.isOKhasData()) {
2101 final ServiceValidator v = new ServiceValidator();
2102 if (v.nullOrBlank(rrdd.value).err()) {
2103 return Result.err(Status.ERR_BadData,v.errs());
2105 return func.deleteRole(trans, rrdd.value, false, false);
2107 return Result.err(rrdd);
2113 path = "/authz/role",
2116 errorCodes = { 404,406 },
2117 text = { "Delete the Role referenced by RoleKey",
2118 "You cannot normally delete a role which still has permissions granted or users assigned to it,",
2119 "however the \"force\" property allows you to do just that. To do this: Add 'force=true'",
2120 "as a query parameter.",
2121 "<p>WARNING: Using force will remove all users and permission from this role. Use with care.</p>"}
2125 public Result<Void> deleteRole(final AuthzTrans trans, REQUEST from) {
2126 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
2127 final ServiceValidator v = new ServiceValidator();
2129 return Result.err(Status.ERR_BadData,"Request does not contain Role");
2131 if (v.nullOrBlank(rd.value).err()) {
2132 return Result.err(Status.ERR_BadData,v.errs());
2134 final RoleDAO.Data role = rd.value;
2135 if (ques.roleDAO.read(trans, role).notOKorIsEmpty() && !trans.requested(force)) {
2136 return Result.err(Status.ERR_RoleNotFound, "Role [" + role.fullName() + "] does not exist");
2139 Result<FutureDAO.Data> fd = mapper.future(trans,RoleDAO.TABLE,from,role,false,
2140 () -> "Delete Role [" + role.fullName() + ']'
2141 + " and all attached user roles",
2143 private Result<NsDAO.Data> nsd;
2145 public Result<?> mayChange() {
2147 nsd = ques.mayUser(trans, trans.user(), role, Access.write);
2155 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rd.value.ns);
2156 if (nsr.notOKorIsEmpty()) {
2157 return Result.err(nsr);
2160 Result<String> rfc = func.createFuture(trans, fd.value,
2161 role.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.D);
2163 return Result.err(Status.ACC_Future, "Role Deletion [%s.%s] is saved for future processing",
2167 return Result.err(rfc);
2169 case Status.ACC_Now:
2170 return func.deleteRole(trans,role,trans.requested(force), true /*preapproved*/);
2172 return Result.err(fd);
2177 /***********************************
2179 ***********************************/
2180 private class MayCreateCred implements MayChange {
2181 private Result<NsDAO.Data> nsd;
2182 private AuthzTrans trans;
2183 private CredDAO.Data cred;
2184 private Executor exec;
2186 public MayCreateCred(AuthzTrans trans, CredDAO.Data cred, Executor exec) {
2193 public Result<?> mayChange() {
2195 nsd = ques.validNSOfDomain(trans, cred.id);
2197 // is Ns of CredID valid?
2201 if (trans.org().validate(trans,Policy.CREATE_MECHID, exec, cred.id)==null) {
2204 Result<?> rmc = ques.mayUser(trans, trans.user(), nsd.value, Access.write);
2205 if (rmc.isOKhasData()) {
2209 } catch (Exception e) {
2210 trans.warn().log(e);
2213 trans.warn().log(nsd.errorString());
2215 return Result.err(Status.ERR_Denied,"%s is not allowed to create %s in %s",trans.user(),cred.id,cred.ns);
2219 private class MayChangeCred implements MayChange {
2221 private Result<NsDAO.Data> nsd;
2222 private AuthzTrans trans;
2223 private CredDAO.Data cred;
2224 public MayChangeCred(AuthzTrans trans, CredDAO.Data cred) {
2230 public Result<?> mayChange() {
2231 // User can change himself (but not create)
2232 if (trans.user().equals(cred.id)) {
2236 nsd = ques.validNSOfDomain(trans, cred.id);
2238 // Get the Namespace
2240 if (ques.mayUser(trans, trans.user(), nsd.value,Access.write).isOK()) {
2243 String user[] = Split.split('.',trans.user());
2244 if (user.length>2) {
2245 String company = user[user.length-1] + '.' + user[user.length-2];
2246 if (ques.isGranted(trans, trans.user(), ROOT_NS,"password",company,"reset")) {
2251 return Result.err(Status.ERR_Denied,"%s is not allowed to change %s in %s",trans.user(),cred.id,cred.ns);
2256 private final long DAY_IN_MILLIS = 24*3600*1000L;
2260 path = "/authn/cred",
2263 errorCodes = {403,404,406,409},
2264 text = { "A credential consists of:",
2265 "<ul><li>id - the ID to create within AAF. The domain is in reverse",
2266 "order of Namespace (i.e. Users of Namespace com.att.myapp would be",
2267 "AB1234@myapp.att.com</li>",
2268 "<li>password - Company Policy Compliant Password</li></ul>",
2269 "Note: AAF does support multiple credentials with the same ID.",
2270 "Check with your organization if you have this implemented."
2274 public Result<Void> createUserCred(final AuthzTrans trans, REQUEST from) {
2275 final String cmdDescription = ("Create User Credential");
2276 TimeTaken tt = trans.start(cmdDescription, Env.SUB);
2279 Result<CredDAO.Data> rcred = mapper.cred(trans, from, true);
2280 if (rcred.isOKhasData()) {
2281 byte[] rawCred = rcred.value.cred.array();
2282 rcred = ques.userCredSetup(trans, rcred.value);
2284 final ServiceValidator v = new ServiceValidator();
2286 if (v.cred(trans, trans.org(),rcred,true).err()) { // Note: Creates have stricter Validations
2287 return Result.err(Status.ERR_BadData,v.errs());
2291 // 2016-4 Jonathan, New Behavior - If MechID is not registered with Org, deny creation
2292 Identity mechID = null;
2293 Organization org = trans.org();
2295 mechID = org.getIdentity(trans, rcred.value.id);
2296 } catch (Exception e1) {
2297 trans.error().log(e1,rcred.value.id,"cannot be validated at this time");
2299 if (mechID==null || !mechID.isFound()) {
2300 return Result.err(Status.ERR_Policy,"MechIDs must be registered with %s before provisioning in AAF",org.getName());
2303 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rcred.value.ns);
2304 if (nsr.notOKorIsEmpty()) {
2305 return Result.err(Status.ERR_NsNotFound,"Cannot provision %s on non-existent Namespace %s",mechID.id(),rcred.value.ns);
2309 boolean firstID = false;
2312 CassExecutor exec = new CassExecutor(trans, func);
2313 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readID(trans, rcred.value.id);
2314 if (rlcd.isOKhasData()) {
2315 if (!org.canHaveMultipleCreds(rcred.value.id)) {
2316 return Result.err(Status.ERR_ConflictAlreadyExists, "Credential exists");
2319 for (CredDAO.Data curr : rlcd.value) {
2320 // May not use the same password in the list
2321 // Note: ASPR specifies character differences, but we don't actually store the
2322 // password to validate char differences.
2324 rb = ques.userCredCheck(trans, curr, rawCred);
2326 return Result.err(rb);
2327 } else if (rb.value){
2328 return Result.err(Status.ERR_Policy, "Credential content cannot be reused.");
2329 } else if (Chrono.dateOnlyStamp(curr.expires).equals(Chrono.dateOnlyStamp(rcred.value.expires)) && curr.type==rcred.value.type) {
2330 return Result.err(Status.ERR_ConflictAlreadyExists, "Credential with same Expiration Date exists, use 'reset'");
2335 // 2016-04-12 Jonathan If Caller is the Sponsor and is also an Owner of NS, allow without special Perm
2336 String theMechID = rcred.value.id;
2337 Boolean otherMechIDs = false;
2338 // find out if this is the only mechID. other MechIDs mean special handling (not automated)
2339 for (CredDAO.Data cd : ques.credDAO.readNS(trans,nsr.value.get(0).name).value) {
2340 if (!cd.id.equals(theMechID)) {
2341 otherMechIDs = true;
2346 // We can say "ID does not exist" here
2347 if ((reason=org.validate(trans, Policy.CREATE_MECHID, exec, theMechID,trans.user(),otherMechIDs.toString()))!=null) {
2348 return Result.err(Status.ERR_Denied, reason);
2351 } catch (Exception e) {
2352 return Result.err(e);
2356 mc = new MayCreateCred(trans, rcred.value, exec);
2358 final CredDAO.Data cdd = rcred.value;
2359 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from, rcred.value,false, // may want to enable in future.
2362 public String get() {
2363 return cmdDescription + " [" +
2366 + cdd.expires + ']';
2373 Result<String> rfc = func.createFuture(trans, fd.value,
2374 rcred.value.id + '|' + rcred.value.type.toString() + '|' + rcred.value.expires,
2375 trans.user(), nsr.value.get(0), FUTURE_OP.C);
2377 return Result.err(Status.ACC_Future, "Credential Request [%s|%s|%s] is saved for future processing",
2379 Integer.toString(rcred.value.type),
2380 rcred.value.expires.toString());
2382 return Result.err(rfc);
2384 case Status.ACC_Now:
2387 // && !nsr.value.get(0).isAdmin(trans.getUserPrincipal().getName())) {
2388 Result<List<String>> admins = func.getAdmins(trans, nsr.value.get(0).name, false);
2389 // OK, it's a first ID, and not by NS Admin, so let's set TempPassword length
2390 // Note, we only do this on First time, because of possibility of
2391 // prematurely expiring a production id
2392 if (admins.isOKhasData() && !admins.value.contains(trans.user())) {
2393 rcred.value.expires = org.expiration(null, Expiration.TempPassword).getTime();
2396 } catch (Exception e) {
2397 trans.error().log(e, "While setting expiration to TempPassword");
2399 Result<?>udr = ques.credDAO.create(trans, rcred.value);
2403 return Result.err(udr);
2405 return Result.err(fd);
2409 return Result.err(rcred);
2418 path = "/authn/creds/ns/:ns",
2419 params = {"ns|string|true"},
2421 errorCodes = {403,404,406},
2422 text = { "Return all IDs in Namespace :ns"
2426 public Result<USERS> getCredsByNS(AuthzTrans trans, String ns) {
2427 final Validator v = new ServiceValidator();
2428 if (v.ns(ns).err()) {
2429 return Result.err(Status.ERR_BadData,v.errs());
2432 // check if user is allowed to view NS
2433 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
2435 return Result.err(rnd);
2437 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
2439 return Result.err(rnd);
2442 TimeTaken tt = trans.start("MAP Creds by NS to Creds", Env.SUB);
2444 USERS users = mapper.newInstance(API.USERS);
2445 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readNS(trans, ns);
2448 if (!rlcd.isEmpty()) {
2449 return mapper.cred(rlcd.value, users);
2451 return Result.ok(users);
2453 return Result.err(rlcd);
2463 path = "/authn/creds/id/:ns",
2464 params = {"id|string|true"},
2466 errorCodes = {403,404,406},
2467 text = { "Return all IDs in for ID"
2468 ,"(because IDs are multiple, due to multiple Expiration Dates)"
2472 public Result<USERS> getCredsByID(AuthzTrans trans, String id) {
2473 final Validator v = new ServiceValidator();
2474 if (v.nullOrBlank("ID",id).err()) {
2475 return Result.err(Status.ERR_BadData,v.errs());
2478 String ns = Question.domain2ns(id);
2479 // check if user is allowed to view NS
2480 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
2482 return Result.err(rnd);
2484 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
2486 return Result.err(rnd);
2489 TimeTaken tt = trans.start("MAP Creds by ID to Creds", Env.SUB);
2491 USERS users = mapper.newInstance(API.USERS);
2492 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readID(trans, id);
2495 if (!rlcd.isEmpty()) {
2496 return mapper.cred(rlcd.value, users);
2498 return Result.ok(users);
2500 return Result.err(rlcd);
2510 path = "/authn/certs/id/:id",
2511 params = {"id|string|true"},
2513 errorCodes = {403,404,406},
2514 text = { "Return Cert Info for ID"
2518 public Result<CERTS> getCertInfoByID(AuthzTrans trans, HttpServletRequest req, String id) {
2519 TimeTaken tt = trans.start("Get Cert Info by ID", Env.SUB);
2521 CERTS certs = mapper.newInstance(API.CERTS);
2522 Result<List<CertDAO.Data>> rlcd = ques.certDAO.readID(trans, id);
2525 if (!rlcd.isEmpty()) {
2526 return mapper.cert(rlcd.value, certs);
2528 return Result.ok(certs);
2530 return Result.err(rlcd);
2540 path = "/authn/cred",
2543 errorCodes = {300,403,404,406},
2544 text = { "Reset a Credential Password. If multiple credentials exist for this",
2545 "ID, you will need to specify which entry you are resetting in the",
2546 "CredRequest object"
2550 public Result<Void> changeUserCred(final AuthzTrans trans, REQUEST from) {
2551 final String cmdDescription = "Update User Credential";
2552 TimeTaken tt = trans.start(cmdDescription, Env.SUB);
2554 Result<CredDAO.Data> rcred = mapper.cred(trans, from, true);
2555 if (rcred.isOKhasData()) {
2556 rcred = ques.userCredSetup(trans, rcred.value);
2558 final ServiceValidator v = new ServiceValidator();
2560 if (v.cred(trans, trans.org(),rcred,false).err()) {// Note: Creates have stricter Validations
2561 return Result.err(Status.ERR_BadData,v.errs());
2563 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readID(trans, rcred.value.id);
2564 if (rlcd.notOKorIsEmpty()) {
2565 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2568 MayChange mc = new MayChangeCred(trans, rcred.value);
2569 Result<?> rmc = mc.mayChange();
2571 return Result.err(rmc);
2574 Result<Integer> ri = selectEntryIfMultiple((CredRequest)from, rlcd.value);
2576 return Result.err(ri);
2578 int entry = ri.value;
2581 final CredDAO.Data cred = rcred.value;
2583 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from, rcred.value,false,
2586 public String get() {
2587 return cmdDescription + " [" +
2590 + cred.expires + ']';
2595 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, rcred.value.ns);
2596 if (nsr.notOKorIsEmpty()) {
2597 return Result.err(nsr);
2602 Result<String> rfc = func.createFuture(trans, fd.value,
2603 rcred.value.id + '|' + rcred.value.type.toString() + '|' + rcred.value.expires,
2604 trans.user(), nsr.value.get(0), FUTURE_OP.U);
2606 return Result.err(Status.ACC_Future, "Credential Request [%s|%s|%s]",
2608 Integer.toString(rcred.value.type),
2609 rcred.value.expires.toString());
2611 return Result.err(rfc);
2613 case Status.ACC_Now:
2614 Result<?>udr = null;
2615 // If we are Resetting Password on behalf of someone else (am not the Admin)
2616 // use TempPassword Expiration time.
2618 if (ques.isAdmin(trans, trans.user(), nsr.value.get(0).name)) {
2619 exp = Expiration.Password;
2621 exp = Expiration.TempPassword;
2624 Organization org = trans.org();
2625 CredDAO.Data current = rlcd.value.get(entry);
2626 // If user resets password in same day, we will have a primary key conflict, so subtract 1 day
2627 if (current.expires.equals(rcred.value.expires)
2628 && rlcd.value.get(entry).type==rcred.value.type) {
2629 GregorianCalendar gc = org.expiration(null, exp,rcred.value.id);
2630 gc = Chrono.firstMomentOfDay(gc);
2631 gc.set(GregorianCalendar.HOUR_OF_DAY, org.startOfDay());
2632 rcred.value.expires = new Date(gc.getTimeInMillis() - DAY_IN_MILLIS);
2634 rcred.value.expires = org.expiration(null,exp).getTime();
2636 // Copy in other fields 10/21/2016
2637 rcred.value.notes=current.notes;
2639 udr = ques.credDAO.create(trans, rcred.value);
2641 udr = ques.credDAO.delete(trans, rlcd.value.get(entry),false);
2647 return Result.err(udr);
2649 return Result.err(fd);
2652 return Result.err(rcred);
2660 * Codify the way to get Either Choice Needed or actual Integer from Credit Request
2662 private Result<Integer> selectEntryIfMultiple(final CredRequest cr, List<CredDAO.Data> lcd) {
2664 if (lcd.size() > 1) {
2665 String inputOption = cr.getEntry();
2666 if (inputOption == null) {
2667 String message = selectCredFromList(lcd, false);
2668 String[] variables = buildVariables(lcd);
2669 return Result.err(Status.ERR_ChoiceNeeded, message, variables);
2671 entry = Integer.parseInt(inputOption) - 1;
2673 if (entry < 0 || entry >= lcd.size()) {
2674 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2677 return Result.ok(entry);
2682 path = "/authn/cred/:days",
2683 params = {"days|string|true"},
2685 errorCodes = {300,403,404,406},
2686 text = { "Extend a Credential Expiration Date. The intention of this API is",
2687 "to avoid an outage in PROD due to a Credential expiring before it",
2688 "can be configured correctly. Measures are being put in place ",
2689 "so that this is not abused."
2693 public Result<Void> extendUserCred(final AuthzTrans trans, REQUEST from, String days) {
2694 TimeTaken tt = trans.start("Extend User Credential", Env.SUB);
2696 Result<CredDAO.Data> cred = mapper.cred(trans, from, false);
2697 Organization org = trans.org();
2698 final ServiceValidator v = new ServiceValidator();
2699 if (v.notOK(cred).err() ||
2700 v.nullOrBlank(cred.value.id, "Invalid ID").err() ||
2701 v.user(org,cred.value.id).err()) {
2702 return Result.err(Status.ERR_BadData,v.errs());
2707 if ((reason=org.validate(trans, Policy.MAY_EXTEND_CRED_EXPIRES, new CassExecutor(trans,func)))!=null) {
2708 return Result.err(Status.ERR_Policy,reason);
2710 } catch (Exception e) {
2712 trans.error().log(e, msg="Could not contact Organization for User Validation");
2713 return Result.err(Status.ERR_Denied, msg);
2716 // Get the list of Cred Entries
2717 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readID(trans, cred.value.id);
2718 if (rlcd.notOKorIsEmpty()) {
2719 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2722 //Need to do the "Pick Entry" mechanism
2723 Result<Integer> ri = selectEntryIfMultiple((CredRequest)from, rlcd.value);
2725 return Result.err(ri);
2728 CredDAO.Data found = rlcd.value.get(ri.value);
2729 CredDAO.Data cd = cred.value;
2730 // Copy over the cred
2732 cd.cred = found.cred;
2733 cd.other = found.other;
2734 cd.type = found.type;
2735 cd.notes = found.notes;
2737 cd.expires = org.expiration(null, Expiration.ExtendPassword,days).getTime();
2739 cred = ques.credDAO.create(trans, cd);
2743 return Result.err(cred);
2749 private String[] buildVariables(List<CredDAO.Data> value) {
2750 // ensure credentials are sorted so we can fully automate Cred regression test
2751 Collections.sort(value, (cred1, cred2) -> cred1.expires.compareTo(cred2.expires));
2752 String [] vars = new String[value.size()+1];
2754 for (int i = 0; i < value.size(); i++) {
2755 vars[i+1] = value.get(i).id + " " + value.get(i).type
2756 + " |" + value.get(i).expires;
2761 private String selectCredFromList(List<CredDAO.Data> value, boolean isDelete) {
2762 StringBuilder errMessage = new StringBuilder();
2763 String userPrompt = isDelete?"Select which cred to delete (set force=true to delete all):":"Select which cred to update:";
2764 int numSpaces = value.get(0).id.length() - "Id".length();
2766 errMessage.append(userPrompt + '\n');
2767 errMessage.append(" Id");
2768 for (int i = 0; i < numSpaces; i++) {
2769 errMessage.append(' ');
2771 errMessage.append(" Type Expires" + '\n');
2772 for (int i=0;i<value.size();++i) {
2773 errMessage.append(" %s\n");
2775 errMessage.append("Run same command again with chosen entry as last parameter");
2777 return errMessage.toString();
2783 path = "/authn/cred",
2786 errorCodes = {300,403,404,406},
2787 text = { "Delete a Credential. If multiple credentials exist for this",
2788 "ID, you will need to specify which entry you are deleting in the",
2789 "CredRequest object."
2793 public Result<Void> deleteUserCred(AuthzTrans trans, REQUEST from) {
2794 final Result<CredDAO.Data> cred = mapper.cred(trans, from, false);
2795 final Validator v = new ServiceValidator();
2796 if (v.nullOrBlank("cred", cred.value.id).err()) {
2797 return Result.err(Status.ERR_BadData,v.errs());
2800 Result<List<CredDAO.Data>> rlcd = ques.credDAO.readID(trans, cred.value.id);
2801 if (rlcd.notOKorIsEmpty()) {
2802 // Empty Creds should have no user_roles.
2803 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByUser(trans, cred.value.id);
2805 for (UserRoleDAO.Data data : rlurd.value) {
2806 ques.userRoleDAO.delete(trans, data, false);
2809 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2811 boolean isLastCred = rlcd.value.size()==1;
2813 MayChange mc = new MayChangeCred(trans,cred.value);
2814 Result<?> rmc = mc.mayChange();
2816 return Result.err(rmc);
2820 if (!trans.requested(force)) {
2821 if (rlcd.value.size() > 1) {
2822 CredRequest cr = (CredRequest)from;
2823 String inputOption = cr.getEntry();
2824 if (inputOption == null) {
2825 String message = selectCredFromList(rlcd.value, true);
2826 String[] variables = buildVariables(rlcd.value);
2827 return Result.err(Status.ERR_ChoiceNeeded, message, variables);
2830 if (inputOption.length()>5) { // should be a date
2831 Date d = Chrono.xmlDatatypeFactory.newXMLGregorianCalendar(inputOption).toGregorianCalendar().getTime();
2833 for (CredDAO.Data cd : rlcd.value) {
2834 if (cd.type.equals(cr.getType()) && cd.expires.equals(d)) {
2840 entry = Integer.parseInt(inputOption) - 1;
2842 } catch (NullPointerException e) {
2843 return Result.err(Status.ERR_BadData, "Invalid Date Format for Entry");
2844 } catch (NumberFormatException e) {
2845 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2848 isLastCred = (entry==-1)?true:false;
2852 if (entry < -1 || entry >= rlcd.value.size()) {
2853 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2857 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from,cred.value,false,
2858 () -> "Delete Credential [" +
2863 Result<List<NsDAO.Data>> nsr = ques.nsDAO.read(trans, cred.value.ns);
2864 if (nsr.notOKorIsEmpty()) {
2865 return Result.err(nsr);
2870 Result<String> rfc = func.createFuture(trans, fd.value, cred.value.id,
2871 trans.user(), nsr.value.get(0), FUTURE_OP.D);
2874 return Result.err(Status.ACC_Future, "Credential Delete [%s] is saved for future processing",cred.value.id);
2876 return Result.err(rfc);
2878 case Status.ACC_Now:
2879 Result<?>udr = null;
2880 if (!trans.requested(force)) {
2881 if (entry<0 || entry >= rlcd.value.size()) {
2882 return Result.err(Status.ERR_BadData,"Invalid Choice [" + entry + "] chosen for Delete [%s] is saved for future processing",cred.value.id);
2884 udr = ques.credDAO.delete(trans, rlcd.value.get(entry),false);
2886 for (CredDAO.Data curr : rlcd.value) {
2887 udr = ques.credDAO.delete(trans, curr, false);
2889 return Result.err(udr);
2894 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByUser(trans, cred.value.id);
2896 for (UserRoleDAO.Data data : rlurd.value) {
2897 ques.userRoleDAO.delete(trans, data, false);
2902 return Result.err(Result.ERR_NotFound,"No User Data found");
2907 return Result.err(udr);
2909 return Result.err(fd);
2916 public Result<Date> doesCredentialMatch(AuthzTrans trans, REQUEST credReq) {
2917 TimeTaken tt = trans.start("Does Credential Match", Env.SUB);
2919 // Note: Mapper assigns RAW type
2920 Result<CredDAO.Data> data = mapper.cred(trans, credReq,false);
2921 if (data.notOKorIsEmpty()) {
2922 return Result.err(data);
2924 CredDAO.Data cred = data.value; // of the Mapped Cred
2925 if (cred.cred==null) {
2926 return Result.err(Result.ERR_BadData,"No Password");
2928 return ques.doesUserCredMatch(trans, cred.id, cred.cred.array());
2931 } catch (DAOException e) {
2932 trans.error().log(e,"Error looking up cred");
2933 return Result.err(Status.ERR_Denied,"Credential does not match");
2941 path = "/authn/basicAuth",
2944 errorCodes = { 403 },
2945 text = { "!!!! DEPRECATED without X509 Authentication STOP USING THIS API BY DECEMBER 2017, or use Certificates !!!!\n"
2946 + "Use /authn/validate instead\n"
2947 + "Note: Validate a Password using BasicAuth Base64 encoded Header. This HTTP/S call is intended as a fast"
2948 + " User/Password lookup for Security Frameworks, and responds 200 if it passes BasicAuth "
2949 + "security, and 403 if it does not." }
2951 private void basicAuth() {
2952 // This is a place holder for Documentation. The real BasicAuth API does not call Service.
2957 path = "/authn/validate",
2960 errorCodes = { 403 },
2961 text = { "Validate a Credential given a Credential Structure. This is a more comprehensive validation, can "
2962 + "do more than BasicAuth as Credential types exp" }
2965 public Result<Date> validateBasicAuth(AuthzTrans trans, String basicAuth) {
2966 //TODO how to make sure people don't use this in browsers? Do we care?
2967 TimeTaken tt = trans.start("Validate Basic Auth", Env.SUB);
2969 BasicPrincipal bp = new BasicPrincipal(basicAuth,trans.org().getRealm());
2970 Result<Date> rq = ques.doesUserCredMatch(trans, bp.getName(), bp.getCred());
2971 // Note: Only want to log problem, don't want to send back to end user
2975 trans.audit().log(rq.errorString());
2977 } catch (Exception e) {
2978 trans.warn().log(e);
2982 return Result.err(Status.ERR_Denied,"Bad Basic Auth");
2985 /***********************************
2987 ***********************************/
2990 path = "/authz/userRole",
2993 errorCodes = {403,404,406,409},
2994 text = { "Create a UserRole relationship (add User to Role)",
2995 "A UserRole is an object Representation of membership of a Role for limited time.",
2996 "If a shorter amount of time for Role ownership is required, use the 'End' field.",
2997 "** Note: Owners of Namespaces will be required to revalidate users in these roles ",
2998 "before Expirations expire. Namespace owners will be notified by email."
3002 public Result<Void> createUserRole(final AuthzTrans trans, REQUEST from) {
3003 TimeTaken tt = trans.start("Create UserRole", Env.SUB);
3005 Result<UserRoleDAO.Data> urr = mapper.userRole(trans, from);
3006 if (urr.notOKorIsEmpty()) {
3007 return Result.err(urr);
3009 final UserRoleDAO.Data userRole = urr.value;
3011 final ServiceValidator v = new ServiceValidator();
3012 if (v.user_role(userRole).err() ||
3013 v.user(trans.org(), userRole.user).err()) {
3014 return Result.err(Status.ERR_BadData,v.errs());
3019 // Check if user can change first
3020 Result<FutureDAO.Data> fd = mapper.future(trans,UserRoleDAO.TABLE,from,urr.value,true, // may request Approvals
3021 () -> "Add User [" + userRole.user + "] to Role [" +
3025 private Result<NsDAO.Data> nsd;
3027 public Result<?> mayChange() {
3029 RoleDAO.Data r = RoleDAO.Data.decode(userRole);
3030 nsd = ques.mayUser(trans, trans.user(), r, Access.write);
3035 Result<NsDAO.Data> nsr = ques.deriveNs(trans, userRole.role);
3036 if (nsr.notOKorIsEmpty()) {
3037 return Result.err(nsr);
3042 Result<String> rfc = func.createFuture(trans, fd.value, userRole.user+'|'+userRole.ns + '.' + userRole.rname,
3043 userRole.user, nsr.value, FUTURE_OP.C);
3045 return Result.err(Status.ACC_Future, "UserRole [%s - %s.%s] is saved for future processing",
3050 return Result.err(rfc);
3052 case Status.ACC_Now:
3053 return func.addUserRole(trans, userRole);
3055 return Result.err(fd);
3063 * getUserRolesByRole
3067 path = "/authz/userRoles/role/:role",
3068 params = {"role|string|true"},
3070 errorCodes = {404,406},
3071 text = { "List all Users that are attached to Role specified in :role",
3075 public Result<USERROLES> getUserRolesByRole(AuthzTrans trans, String role) {
3076 final Validator v = new ServiceValidator();
3077 if (v.nullOrBlank("Role",role).err()) {
3078 return Result.err(Status.ERR_BadData,v.errs());
3081 Result<RoleDAO.Data> rrdd;
3082 rrdd = RoleDAO.Data.decode(trans,ques,role);
3084 return Result.err(rrdd);
3086 // May Requester see result?
3087 Result<NsDAO.Data> ns = ques.mayUser(trans,trans.user(), rrdd.value,Access.read);
3089 return Result.err(ns);
3092 // boolean filter = true;
3093 // if (ns.value.isAdmin(trans.user()) || ns.value.isResponsible(trans.user()))
3096 // Get list of roles per user, then add to Roles as we go
3097 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3098 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByRole(trans, role);
3100 for (UserRoleDAO.Data data : rlurd.value) {
3105 @SuppressWarnings("unchecked")
3106 USERROLES users = (USERROLES) mapper.newInstance(API.USER_ROLES);
3107 // Checked for permission
3108 mapper.userRoles(trans, userSet, users);
3109 return Result.ok(users);
3112 * getUserRolesByRole
3116 path = "/authz/userRoles/user/:user",
3117 params = {"role|string|true"},
3119 errorCodes = {404,406},
3120 text = { "List all UserRoles for :user",
3124 public Result<USERROLES> getUserRolesByUser(AuthzTrans trans, String user) {
3125 final Validator v = new ServiceValidator();
3126 if (v.nullOrBlank("User",user).err()) {
3127 return Result.err(Status.ERR_BadData,v.errs());
3130 // Get list of roles per user, then add to Roles as we go
3131 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByUser(trans, user);
3132 if (rlurd.notOK()) {
3133 return Result.err(rlurd);
3138 * 2) is User's Supervisor
3139 * 3) Has special global access =read permission
3141 * If none of the 3, then filter results to NSs in which Calling User has Ns.access * read
3144 String callingUser = trans.getUserPrincipal().getName();
3145 NsDAO.Data ndd = new NsDAO.Data();
3147 if (user.equals(callingUser)) {
3150 Organization org = trans.org();
3152 Identity orgID = org.getIdentity(trans, user);
3153 Identity manager = orgID==null?null:orgID.responsibleTo();
3154 if (orgID!=null && (manager!=null && callingUser.equals(manager.fullID()))) {
3156 } else if (ques.isGranted(trans, callingUser, ROOT_NS, Question.ACCESS, "*", Access.read.name())) {
3161 } catch (OrganizationException e) {
3167 List<UserRoleDAO.Data> content;
3169 content = new ArrayList<>(rlurd.value.size()); // avoid multi-memory redos
3171 for (UserRoleDAO.Data data : rlurd.value) {
3173 Result<Data> mur = ques.mayUser(trans, callingUser, ndd, Access.read);
3180 content = rlurd.value;
3184 @SuppressWarnings("unchecked")
3185 USERROLES users = (USERROLES) mapper.newInstance(API.USER_ROLES);
3186 // Checked for permission
3187 mapper.userRoles(trans, content, users);
3188 return Result.ok(users);
3194 path = "/authz/userRole/user",
3197 errorCodes = {403,404,406},
3198 text = { "Set a User's roles to the roles specified in the UserRoleRequest object.",
3199 "WARNING: Roles supplied will be the ONLY roles attached to this user",
3200 "If no roles are supplied, user's roles are reset."
3204 public Result<Void> resetRolesForUser(AuthzTrans trans, REQUEST rreq) {
3205 Result<UserRoleDAO.Data> rurdd = mapper.userRole(trans, rreq);
3206 final ServiceValidator v = new ServiceValidator();
3207 if (rurdd.notOKorIsEmpty()) {
3208 return Result.err(rurdd);
3210 if (v.user(trans.org(), rurdd.value.user).err()) {
3211 return Result.err(Status.ERR_BadData,v.errs());
3214 Set<String> currRoles = new HashSet<>();
3215 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByUser(trans, rurdd.value.user);
3217 for (UserRoleDAO.Data data : rlurd.value) {
3218 currRoles.add(data.role);
3222 Result<Void> rv = null;
3224 if (rurdd.value.role==null) {
3225 roles = new String[0];
3227 roles = rurdd.value.role.split(",");
3230 for (String role : roles) {
3231 if (v.role(role).err()) {
3232 return Result.err(Status.ERR_BadData,v.errs());
3234 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3236 return Result.err(rrdd);
3239 rurdd.value.role(rrdd.value);
3241 Result<NsDAO.Data> nsd = ques.mayUser(trans, trans.user(), rrdd.value,Access.write);
3243 return Result.err(nsd);
3245 Result<NsDAO.Data> nsr = ques.deriveNs(trans, role);
3246 if (nsr.notOKorIsEmpty()) {
3247 return Result.err(nsr);
3250 if (currRoles.contains(role)) {
3251 currRoles.remove(role);
3253 rv = func.addUserRole(trans, rurdd.value);
3260 for (String role : currRoles) {
3261 rurdd.value.role(trans,ques,role);
3262 rv = ques.userRoleDAO.delete(trans, rurdd.value, false);
3264 trans.info().log(rurdd.value.user,"/",rurdd.value.role, "expected to be deleted, but does not exist");
3265 // return rv; // if it doesn't exist, don't error out
3276 path = "/authz/userRole/role",
3279 errorCodes = {403,404,406},
3280 text = { "Set a Role's users to the users specified in the UserRoleRequest object.",
3281 "WARNING: Users supplied will be the ONLY users attached to this role",
3282 "If no users are supplied, role's users are reset."
3286 public Result<Void> resetUsersForRole(AuthzTrans trans, REQUEST rreq) {
3287 Result<UserRoleDAO.Data> rurdd = mapper.userRole(trans, rreq);
3288 if (rurdd.notOKorIsEmpty()) {
3289 return Result.err(rurdd);
3291 final ServiceValidator v = new ServiceValidator();
3292 if (v.user_role(rurdd.value).err()) {
3293 return Result.err(Status.ERR_BadData,v.errs());
3296 RoleDAO.Data rd = RoleDAO.Data.decode(rurdd.value);
3298 Result<NsDAO.Data> nsd = ques.mayUser(trans, trans.user(), rd, Access.write);
3300 return Result.err(nsd);
3303 Result<NsDAO.Data> nsr = ques.deriveNs(trans, rurdd.value.role);
3304 if (nsr.notOKorIsEmpty()) {
3305 return Result.err(nsr);
3308 Set<String> currUsers = new HashSet<>();
3309 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByRole(trans, rurdd.value.role);
3311 for (UserRoleDAO.Data data : rlurd.value) {
3312 currUsers.add(data.user);
3316 // found when connected remotely to DEVL, can't replicate locally
3317 // inconsistent errors with cmd: role user setTo [nothing]
3318 // deleteUserRole --> read --> get --> cacheIdx(?)
3319 // sometimes returns idx for last added user instead of user passed in
3323 Result<Void> rv = null;
3324 String[] users = {};
3325 if (rurdd.value.user != null) {
3326 users = rurdd.value.user.split(",");
3329 for (String user : users) {
3330 if (v.user(trans.org(), user).err()) {
3331 return Result.err(Status.ERR_BadData,v.errs());
3333 rurdd.value.user = user;
3335 if (currUsers.contains(user)) {
3336 currUsers.remove(user);
3338 rv = func.addUserRole(trans, rurdd.value);
3345 for (String user : currUsers) {
3346 rurdd.value.user = user;
3347 rv = ques.userRoleDAO.delete(trans, rurdd.value, false);
3349 trans.info().log(rurdd.value, "expected to be deleted, but not exists");
3359 path = "/authz/userRole/extend/:user/:role",
3360 params = { "user|string|true",
3364 errorCodes = {403,404,406},
3365 text = { "Extend the Expiration of this User Role by the amount set by Organization",
3366 "Requestor must be allowed to modify the role"
3370 public Result<Void> extendUserRole(AuthzTrans trans, String user, String role) {
3371 Organization org = trans.org();
3372 final ServiceValidator v = new ServiceValidator();
3373 if (v.user(org, user)
3376 return Result.err(Status.ERR_BadData,v.errs());
3379 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
3381 return Result.err(rrdd);
3384 Result<NsDAO.Data> rcr = ques.mayUser(trans, trans.user(), rrdd.value, Access.write);
3385 boolean mayNotChange;
3386 if ((mayNotChange = rcr.notOK()) && !trans.requested(future)) {
3387 return Result.err(rcr);
3390 Result<List<UserRoleDAO.Data>> rr = ques.userRoleDAO.read(trans, user,role);
3392 return Result.err(rr);
3394 for (UserRoleDAO.Data userRole : rr.value) {
3395 if (mayNotChange) { // Function exited earlier if !trans.futureRequested
3396 FutureDAO.Data fto = new FutureDAO.Data();
3397 fto.target=UserRoleDAO.TABLE;
3398 fto.memo = "Extend User ["+userRole.user+"] in Role ["+userRole.role+"]";
3399 GregorianCalendar now = new GregorianCalendar();
3400 fto.start = now.getTime();
3401 fto.expires = org.expiration(now, Expiration.Future).getTime();
3403 fto.construct = userRole.bytify();
3404 } catch (IOException e) {
3405 trans.error().log(e, "Error while bytifying UserRole for Future");
3406 return Result.err(e);
3409 Result<String> rfc = func.createFuture(trans, fto,
3410 userRole.user+'|'+userRole.role, userRole.user, rcr.value, FUTURE_OP.U);
3412 return Result.err(Status.ACC_Future, "UserRole [%s - %s] is saved for future processing",
3416 return Result.err(rfc);
3419 return func.extendUserRole(trans, userRole, false);
3422 return Result.err(Result.ERR_NotFound,"This user and role doesn't exist");
3427 path = "/authz/userRole/:user/:role",
3428 params = { "user|string|true",
3432 errorCodes = {403,404,406},
3433 text = { "Remove Role :role from User :user."
3437 public Result<Void> deleteUserRole(AuthzTrans trans, String usr, String role) {
3438 Validator val = new ServiceValidator();
3439 if (val.nullOrBlank("User", usr)
3440 .nullOrBlank("Role", role).err()) {
3441 return Result.err(Status.ERR_BadData, val.errs());
3444 boolean mayNotChange;
3445 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
3447 return Result.err(rrdd);
3450 RoleDAO.Data rdd = rrdd.value;
3451 Result<NsDAO.Data> rns = ques.mayUser(trans, trans.user(), rdd, Access.write);
3453 // Make sure we don't delete the last owner of valid NS
3454 if (rns.isOKhasData() && Question.OWNER.equals(rdd.name) && ques.countOwner(trans,rdd.ns)<=1) {
3455 return Result.err(Status.ERR_Denied,"You may not delete the last Owner of " + rdd.ns );
3458 if (mayNotChange=rns.notOK()) {
3459 if (!trans.requested(future)) {
3460 return Result.err(rns);
3464 Result<List<UserRoleDAO.Data>> rulr;
3465 if ((rulr=ques.userRoleDAO.read(trans, usr, role)).notOKorIsEmpty()) {
3466 return Result.err(Status.ERR_UserRoleNotFound, "User [ "+usr+" ] is not "
3467 + "Assigned to the Role [ " + role + " ]");
3470 UserRoleDAO.Data userRole = rulr.value.get(0);
3471 if (mayNotChange) { // Function exited earlier if !trans.futureRequested
3472 FutureDAO.Data fto = new FutureDAO.Data();
3473 fto.target=UserRoleDAO.TABLE;
3474 fto.memo = "Remove User ["+userRole.user+"] from Role ["+userRole.role+"]";
3475 GregorianCalendar now = new GregorianCalendar();
3476 fto.start = now.getTime();
3477 fto.expires = trans.org().expiration(now, Expiration.Future).getTime();
3479 Result<String> rfc = func.createFuture(trans, fto,
3480 userRole.user+'|'+userRole.role, userRole.user, rns.value, FUTURE_OP.D);
3482 return Result.err(Status.ACC_Future, "UserRole [%s - %s] is saved for future processing",
3486 return Result.err(rfc);
3489 return ques.userRoleDAO.delete(trans, rulr.value.get(0), false);
3495 path = "/authz/userRole/:user/:role",
3496 params = {"user|string|true",
3497 "role|string|true"},
3499 errorCodes = {403,404,406},
3500 text = { "Returns the User (with Expiration date from listed User/Role) if it exists"
3504 public Result<USERS> getUserInRole(AuthzTrans trans, String user, String role) {
3505 final Validator v = new ServiceValidator();
3506 if (v.role(role).nullOrBlank("User", user).err()) {
3507 return Result.err(Status.ERR_BadData,v.errs());
3510 // Result<NsDAO.Data> ns = ques.deriveNs(trans, role);
3511 // if (ns.notOK()) return Result.err(ns);
3513 // Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), ns.value, Access.write);
3514 // May calling user see by virtue of the Role
3515 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3517 return Result.err(rrdd);
3519 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value,Access.read);
3521 return Result.err(rnd);
3524 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3525 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readUserInRole(trans, user, role);
3527 for (UserRoleDAO.Data data : rlurd.value) {
3532 @SuppressWarnings("unchecked")
3533 USERS users = (USERS) mapper.newInstance(API.USERS);
3534 mapper.users(trans, userSet, users);
3535 return Result.ok(users);
3540 path = "/authz/users/role/:role",
3541 params = {"user|string|true",
3542 "role|string|true"},
3544 errorCodes = {403,404,406},
3545 text = { "Returns the User (with Expiration date from listed User/Role) if it exists"
3549 public Result<USERS> getUsersByRole(AuthzTrans trans, String role) {
3550 final Validator v = new ServiceValidator();
3551 if (v.nullOrBlank("Role",role).err()) {
3552 return Result.err(Status.ERR_BadData,v.errs());
3555 // Result<NsDAO.Data> ns = ques.deriveNs(trans, role);
3556 // if (ns.notOK()) return Result.err(ns);
3558 // Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), ns.value, Access.write);
3559 // May calling user see by virtue of the Role
3560 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3562 return Result.err(rrdd);
3565 boolean contactOnly = false;
3566 // Allow the request of any valid user to find the contact of the NS (Owner)
3567 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value,Access.read);
3569 if (Question.OWNER.equals(rrdd.value.name)) {
3572 return Result.err(rnd);
3576 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3577 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByRole(trans, role);
3579 for (UserRoleDAO.Data data : rlurd.value) {
3580 if (contactOnly) { //scrub data
3581 // Can't change actual object, or will mess up the cache.
3582 UserRoleDAO.Data scrub = new UserRoleDAO.Data();
3584 scrub.rname = data.rname;
3585 scrub.role = data.role;
3586 scrub.user = data.user;
3594 @SuppressWarnings("unchecked")
3595 USERS users = (USERS) mapper.newInstance(API.USERS);
3596 mapper.users(trans, userSet, users);
3597 return Result.ok(users);
3601 * getUsersByPermission
3605 path = "/authz/users/perm/:type/:instance/:action",
3606 params = { "type|string|true",
3607 "instance|string|true",
3608 "action|string|true"
3611 errorCodes = {404,406},
3612 text = { "List all Users that have Permission specified by :type :instance :action",
3616 public Result<USERS> getUsersByPermission(AuthzTrans trans, String type, String instance, String action) {
3617 final Validator v = new ServiceValidator();
3618 if (v.nullOrBlank("Type",type)
3619 .nullOrBlank("Instance",instance)
3620 .nullOrBlank("Action",action)
3622 return Result.err(Status.ERR_BadData,v.errs());
3625 Result<NsSplit> nss = ques.deriveNsSplit(trans, type);
3627 return Result.err(nss);
3630 Result<List<NsDAO.Data>> nsd = ques.nsDAO.read(trans, nss.value.ns);
3632 return Result.err(nsd);
3635 boolean allInstance = ASTERIX.equals(instance);
3636 boolean allAction = ASTERIX.equals(action);
3637 // Get list of roles per Permission,
3638 // Then loop through Roles to get Users
3639 // Note: Use Sets to avoid processing or responding with Duplicates
3640 Set<String> roleUsed = new HashSet<>();
3641 Set<UserRoleDAO.Data> userSet = new HashSet<>();
3643 if (!nss.isEmpty()) {
3644 Result<List<PermDAO.Data>> rlp = ques.permDAO.readByType(trans, nss.value.ns, nss.value.name);
3645 if (rlp.isOKhasData()) {
3646 for (PermDAO.Data pd : rlp.value) {
3647 if ((allInstance || pd.instance.equals(instance)) &&
3648 (allAction || pd.action.equals(action))) {
3649 if (ques.mayUser(trans, trans.user(),pd,Access.read).isOK()) {
3650 for (String role : pd.roles) {
3651 if (!roleUsed.contains(role)) { // avoid evaluating Role many times
3653 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO.readByRole(trans, role.replace('|', '.'));
3654 if (rlurd.isOKhasData()) {
3655 for (UserRoleDAO.Data urd : rlurd.value) {
3666 @SuppressWarnings("unchecked")
3667 USERS users = (USERS) mapper.newInstance(API.USERS);
3668 mapper.users(trans, userSet, users);
3669 return Result.ok(users);
3672 /***********************************
3674 ***********************************/
3676 public Result<HISTORY> getHistoryByUser(final AuthzTrans trans, String user, final int[] yyyymm, final int sort) {
3677 final Validator v = new ServiceValidator();
3678 if (v.nullOrBlank("User",user).err()) {
3679 return Result.err(Status.ERR_BadData,v.errs());
3682 Result<NsDAO.Data> rnd;
3683 // Users may look at their own data
3684 if (trans.user().equals(user)) {
3685 // Users may look at their own data
3687 int at = user.indexOf('@');
3688 if (at>=0 && trans.org().getRealm().equals(user.substring(at+1))) {
3689 NsDAO.Data nsd = new NsDAO.Data();
3690 nsd.name = Question.domain2ns(user);
3691 rnd = ques.mayUser(trans, trans.user(), nsd, Access.read);
3693 return Result.err(rnd);
3696 rnd = ques.validNSOfDomain(trans, user);
3698 return Result.err(rnd);
3701 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3703 return Result.err(rnd);
3707 Result<List<HistoryDAO.Data>> resp = ques.historyDAO.readByUser(trans, user, yyyymm);
3709 return Result.err(resp);
3711 return mapper.history(trans, resp.value,sort);
3715 public Result<HISTORY> getHistoryByRole(AuthzTrans trans, String role, int[] yyyymm, final int sort) {
3716 final Validator v = new ServiceValidator();
3717 if (v.nullOrBlank("Role",role).err()) {
3718 return Result.err(Status.ERR_BadData,v.errs());
3721 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3723 return Result.err(rrdd);
3726 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value, Access.read);
3728 return Result.err(rnd);
3730 Result<List<HistoryDAO.Data>> resp = ques.historyDAO.readBySubject(trans, role, "role", yyyymm);
3732 return Result.err(resp);
3734 return mapper.history(trans, resp.value,sort);
3738 public Result<HISTORY> getHistoryByPerm(AuthzTrans trans, String type, int[] yyyymm, final int sort) {
3739 final Validator v = new ServiceValidator();
3740 if (v.nullOrBlank("Type",type)
3742 return Result.err(Status.ERR_BadData,v.errs());
3745 // May user see Namespace of Permission (since it's only one piece... we can't check for "is permission part of")
3746 Result<NsDAO.Data> rnd = ques.deriveNs(trans,type);
3748 return Result.err(rnd);
3751 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3753 return Result.err(rnd);
3755 Result<List<HistoryDAO.Data>> resp = ques.historyDAO.readBySubject(trans, type, "perm", yyyymm);
3757 return Result.err(resp);
3759 return mapper.history(trans, resp.value,sort);
3763 public Result<HISTORY> getHistoryByNS(AuthzTrans trans, String ns, int[] yyyymm, final int sort) {
3764 final Validator v = new ServiceValidator();
3765 if (v.nullOrBlank("NS",ns)
3767 return Result.err(Status.ERR_BadData,v.errs());
3770 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
3772 return Result.err(rnd);
3774 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3776 return Result.err(rnd);
3779 Result<List<HistoryDAO.Data>> resp = ques.historyDAO.readBySubject(trans, ns, "ns", yyyymm);
3781 return Result.err(resp);
3783 return mapper.history(trans, resp.value,sort);
3786 /***********************************
3788 ***********************************/
3790 public Result<Void> createDelegate(final AuthzTrans trans, REQUEST base) {
3791 return createOrUpdateDelegate(trans, base, Question.Access.create);
3795 public Result<Void> updateDelegate(AuthzTrans trans, REQUEST base) {
3796 return createOrUpdateDelegate(trans, base, Question.Access.write);
3800 private Result<Void> createOrUpdateDelegate(final AuthzTrans trans, REQUEST base, final Access access) {
3801 final Result<DelegateDAO.Data> rd = mapper.delegate(trans, base);
3802 final ServiceValidator v = new ServiceValidator();
3803 if (v.delegate(trans.org(),rd).err()) {
3804 return Result.err(Status.ERR_BadData,v.errs());
3807 final DelegateDAO.Data dd = rd.value;
3809 Result<List<DelegateDAO.Data>> ddr = ques.delegateDAO.read(trans, dd);
3810 if (access==Access.create && ddr.isOKhasData()) {
3811 return Result.err(Status.ERR_ConflictAlreadyExists, "[%s] already delegates to [%s]", dd.user, ddr.value.get(0).delegate);
3812 } else if (access!=Access.create && ddr.notOKorIsEmpty()) {
3813 return Result.err(Status.ERR_NotFound, "[%s] does not have a Delegate Record to [%s].",dd.user,access.name());
3815 Result<Void> rv = ques.mayUser(trans, dd, access);
3820 Result<FutureDAO.Data> fd = mapper.future(trans,DelegateDAO.TABLE,base, dd, false,
3822 StringBuilder sb = new StringBuilder();
3823 sb.append(access.name());
3824 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
3825 sb.append("Delegate ");
3826 sb.append(access==Access.create?"[":"to [");
3827 sb.append(rd.value.delegate);
3828 sb.append("] for [");
3829 sb.append(rd.value.user);
3831 return sb.toString();
3834 return Result.ok(); // Validate in code above
3839 Result<String> rfc = func.createFuture(trans, fd.value,
3840 dd.user, trans.user(),null, access==Access.create?FUTURE_OP.C:FUTURE_OP.U);
3842 return Result.err(Status.ACC_Future, "Delegate for [%s]",
3845 return Result.err(rfc);
3847 case Status.ACC_Now:
3848 if (access==Access.create) {
3849 Result<DelegateDAO.Data> rdr = ques.delegateDAO.create(trans, dd);
3853 return Result.err(rdr);
3856 return ques.delegateDAO.update(trans, dd);
3859 return Result.err(fd);
3864 public Result<Void> deleteDelegate(AuthzTrans trans, REQUEST base) {
3865 final Result<DelegateDAO.Data> rd = mapper.delegate(trans, base);
3866 final Validator v = new ServiceValidator();
3867 if (v.notOK(rd).nullOrBlank("User", rd.value.user).err()) {
3868 return Result.err(Status.ERR_BadData,v.errs());
3871 Result<List<DelegateDAO.Data>> ddl;
3872 if ((ddl=ques.delegateDAO.read(trans, rd.value)).notOKorIsEmpty()) {
3873 return Result.err(Status.ERR_DelegateNotFound,"Cannot delete non-existent Delegate");
3875 final DelegateDAO.Data dd = ddl.value.get(0);
3876 Result<Void> rv = ques.mayUser(trans, dd, Access.write);
3881 return ques.delegateDAO.delete(trans, dd, false);
3885 public Result<Void> deleteDelegate(AuthzTrans trans, String userName) {
3886 DelegateDAO.Data dd = new DelegateDAO.Data();
3887 final Validator v = new ServiceValidator();
3888 if (v.nullOrBlank("User", userName).err()) {
3889 return Result.err(Status.ERR_BadData,v.errs());
3892 Result<List<DelegateDAO.Data>> ddl;
3893 if ((ddl=ques.delegateDAO.read(trans, dd)).notOKorIsEmpty()) {
3894 return Result.err(Status.ERR_DelegateNotFound,"Cannot delete non-existent Delegate");
3896 dd = ddl.value.get(0);
3897 Result<Void> rv = ques.mayUser(trans, dd, Access.write);
3902 return ques.delegateDAO.delete(trans, dd, false);
3906 public Result<DELGS> getDelegatesByUser(AuthzTrans trans, String user) {
3907 final Validator v = new ServiceValidator();
3908 if (v.nullOrBlank("User", user).err()) {
3909 return Result.err(Status.ERR_BadData,v.errs());
3912 DelegateDAO.Data ddd = new DelegateDAO.Data();
3914 ddd.delegate = null;
3915 Result<Void> rv = ques.mayUser(trans, ddd, Access.read);
3917 return Result.err(rv);
3920 TimeTaken tt = trans.start("Get delegates for a user", Env.SUB);
3922 Result<List<DelegateDAO.Data>> dbDelgs = ques.delegateDAO.read(trans, user);
3924 if (dbDelgs.isOKhasData()) {
3925 return mapper.delegate(dbDelgs.value);
3927 return Result.err(Status.ERR_DelegateNotFound,"No Delegate found for [%s]",user);
3935 public Result<DELGS> getDelegatesByDelegate(AuthzTrans trans, String delegate) {
3936 final Validator v = new ServiceValidator();
3937 if (v.nullOrBlank("Delegate", delegate).err()) {
3938 return Result.err(Status.ERR_BadData,v.errs());
3941 DelegateDAO.Data ddd = new DelegateDAO.Data();
3942 ddd.user = delegate;
3943 Result<Void> rv = ques.mayUser(trans, ddd, Access.read);
3945 return Result.err(rv);
3948 TimeTaken tt = trans.start("Get users for a delegate", Env.SUB);
3950 Result<List<DelegateDAO.Data>> dbDelgs = ques.delegateDAO.readByDelegate(trans, delegate);
3952 if (dbDelgs.isOKhasData()) {
3953 return mapper.delegate(dbDelgs.value);
3955 return Result.err(Status.ERR_DelegateNotFound,"Delegate [%s] is not delegating for anyone.",delegate);
3962 /***********************************
3964 ***********************************/
3965 private static final String APPR_FMT = "actor=%s, action=%s, operation=\"%s\", requestor=%s, delegator=%s";
3967 public Result<Void> updateApproval(AuthzTrans trans, APPROVALS approvals) {
3968 Result<List<ApprovalDAO.Data>> rlad = mapper.approvals(approvals);
3970 return Result.err(rlad);
3972 int numApprs = rlad.value.size();
3974 return Result.err(Status.ERR_NoApprovals,"No Approvals sent for Updating");
3976 int numProcessed = 0;
3977 String user = trans.user();
3979 Result<List<ApprovalDAO.Data>> curr;
3980 Lookup<List<ApprovalDAO.Data>> apprByTicket=null;
3981 for (ApprovalDAO.Data updt : rlad.value) {
3982 if (updt.ticket!=null) {
3983 curr = ques.approvalDAO.readByTicket(trans, updt.ticket);
3984 if (curr.isOKhasData()) {
3985 final List<ApprovalDAO.Data> add = curr.value;
3986 // Store a Pre-Lookup
3987 apprByTicket = (trans1, noop) -> add;
3989 } else if (updt.id!=null) {
3990 curr = ques.approvalDAO.read(trans, updt);
3991 } else if (updt.approver!=null) {
3992 curr = ques.approvalDAO.readByApprover(trans, updt.approver);
3994 return Result.err(Status.ERR_BadData,"Approvals need ID, Ticket or Approval data to update");
3997 if (curr.isOKhasData()) {
3998 Map<String, Result<List<DelegateDAO.Data>>> delegateCache = new HashMap<>();
3999 Map<UUID, FutureDAO.Data> futureCache = new HashMap<>();
4000 FutureDAO.Data hasDeleted = new FutureDAO.Data();
4002 for (ApprovalDAO.Data cd : curr.value) {
4003 if ("pending".equals(cd.status)) {
4004 // Check for right record. Need ID, or (Ticket&Trans.User==Appr)
4006 boolean delegatedAction = ques.isDelegated(trans, user, cd.approver, delegateCache);
4007 String delegator = cd.approver;
4008 if (updt.id!=null ||
4009 (updt.ticket!=null && user.equals(cd.approver)) ||
4010 (updt.ticket!=null && delegatedAction)) {
4011 if (updt.ticket.equals(cd.ticket)) {
4012 Changed ch = new Changed();
4013 cd.id = ch.changed(cd.id,updt.id);
4014 // cd.ticket = changed(cd.ticket,updt.ticket);
4015 cd.user = ch.changed(cd.user,updt.user);
4016 cd.approver = ch.changed(cd.approver,updt.approver);
4017 cd.type = ch.changed(cd.type,updt.type);
4018 cd.status = ch.changed(cd.status,updt.status);
4019 cd.memo = ch.changed(cd.memo,updt.memo);
4020 cd.operation = ch.changed(cd.operation,updt.operation);
4021 cd.updated = ch.changed(cd.updated,updt.updated==null?new Date():updt.updated);
4022 if (updt.status.equals("denied")) {
4023 cd.last_notified = null;
4025 if (cd.ticket!=null) {
4026 FutureDAO.Data fdd = futureCache.get(cd.ticket);
4027 if (fdd==null) { // haven't processed ticket yet
4028 Result<FutureDAO.Data> rfdd = ques.futureDAO.readPrimKey(trans, cd.ticket);
4030 fdd = rfdd.value; // null is ok
4034 futureCache.put(cd.ticket, fdd); // processed this Ticket... don't do others on this ticket
4036 if (fdd==hasDeleted) { // YES, by Object
4038 cd.status = "ticketDeleted";
4039 ch.hasChanged(true);
4041 FUTURE_OP fop = FUTURE_OP.toFO(cd.operation);
4043 trans.info().printf("Approval Status %s is not actionable",cd.status);
4044 } else if (apprByTicket!=null) {
4045 Result<OP_STATUS> rv = func.performFutureOp(trans, fop, fdd, apprByTicket,func.urDBLookup);
4049 if (delegatedAction) {
4050 trans.audit().printf(APPR_FMT,user,updt.status,cd.memo,cd.user,delegator);
4052 futureCache.put(cd.ticket, hasDeleted);
4056 ch.hasChanged(true);
4057 trans.audit().printf(APPR_FMT,user,rv.value.desc(),cd.memo,cd.user,delegator);
4058 futureCache.put(cd.ticket, hasDeleted);
4063 trans.info().log(rv.toString());
4070 if (ch.hasChanged()) {
4071 ques.approvalDAO.update(trans, cd, true);
4080 if (numApprs==numProcessed) {
4083 return Result.err(Status.ERR_ActionNotCompleted,numProcessed + " out of " + numApprs + " completed");
4087 private static class Changed {
4088 private boolean hasChanged = false;
4090 public<T> T changed(T src, T proposed) {
4091 if (proposed==null || (src!=null && src.equals(proposed))) {
4098 public void hasChanged(boolean b) {
4102 public boolean hasChanged() {
4108 public Result<APPROVALS> getApprovalsByUser(AuthzTrans trans, String user) {
4109 final Validator v = new ServiceValidator();
4110 if (v.nullOrBlank("User", user).err()) {
4111 return Result.err(Status.ERR_BadData,v.errs());
4114 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO.readByUser(trans, user);
4116 return mapper.approvals(rapd.value);
4118 return Result.err(rapd);
4123 public Result<APPROVALS> getApprovalsByTicket(AuthzTrans trans, String ticket) {
4124 final Validator v = new ServiceValidator();
4125 if (v.nullOrBlank("Ticket", ticket).err()) {
4126 return Result.err(Status.ERR_BadData,v.errs());
4130 uuid = UUID.fromString(ticket);
4131 } catch (IllegalArgumentException e) {
4132 return Result.err(Status.ERR_BadData,e.getMessage());
4135 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO.readByTicket(trans, uuid);
4137 return mapper.approvals(rapd.value);
4139 return Result.err(rapd);
4144 public Result<APPROVALS> getApprovalsByApprover(AuthzTrans trans, String approver) {
4145 final Validator v = new ServiceValidator();
4146 if (v.nullOrBlank("Approver", approver).err()) {
4147 return Result.err(Status.ERR_BadData,v.errs());
4150 List<ApprovalDAO.Data> listRapds = new ArrayList<>();
4152 Result<List<ApprovalDAO.Data>> myRapd = ques.approvalDAO.readByApprover(trans, approver);
4153 if (myRapd.notOK()) {
4154 return Result.err(myRapd);
4157 listRapds.addAll(myRapd.value);
4159 Result<List<DelegateDAO.Data>> delegatedFor = ques.delegateDAO.readByDelegate(trans, approver);
4160 if (delegatedFor.isOK()) {
4161 for (DelegateDAO.Data dd : delegatedFor.value) {
4162 if (dd.expires.after(new Date())) {
4163 String delegator = dd.user;
4164 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO.readByApprover(trans, delegator);
4166 for (ApprovalDAO.Data d : rapd.value) {
4167 if (!d.user.equals(trans.user())) {
4176 return mapper.approvals(listRapds);
4180 * @see org.onap.aaf.auth.service.AuthzService#clearCache(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
4183 public Result<Void> cacheClear(AuthzTrans trans, String cname) {
4184 if (ques.isGranted(trans,trans.user(),ROOT_NS,CACHE,cname,"clear")) {
4185 return ques.clearCache(trans,cname);
4187 return Result.err(Status.ERR_Denied, "%s does not have AAF Permission '%s.%s|%s|clear",
4188 trans.user(),ROOT_NS,CACHE,cname);
4192 * @see org.onap.aaf.auth.service.AuthzService#cacheClear(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.Integer)
4195 public Result<Void> cacheClear(AuthzTrans trans, String cname, int[] segment) {
4196 if (ques.isGranted(trans,trans.user(),ROOT_NS,CACHE,cname,"clear")) {
4197 Result<Void> v=null;
4198 for (int i: segment) {
4199 v=ques.cacheClear(trans,cname,i);
4205 return Result.err(Status.ERR_Denied, "%s does not have AAF Permission '%s.%s|%s|clear",
4206 trans.user(),ROOT_NS,CACHE,cname);
4210 * @see org.onap.aaf.auth.service.AuthzService#dbReset(org.onap.aaf.auth.env.test.AuthzTrans)
4213 public void dbReset(AuthzTrans trans) {
4214 ques.historyDAO.reportPerhapsReset(trans, null);