/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ "use strict"; var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__tryCatch=__rt.__tryCatch; var util = require("util");

















var adUtils = require("./adUtils");
var profile = require("../../../util/profile");
var utils = require("../../../util/utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var withProgress = cli.interaction.withProgress.bind(cli.interaction);

  var ad = cli.category("ad").description($("Commands to display Active Directory objects"));

  var adGroup = ad.category("group").description($("Commands to display Active Directory groups"));


  adGroup.command("list").description($("Get Active Directory groups in current subscription's tenant")).option("| more", $("Provides paging support. Press 'Enter' for more information.")).execute(function __1(objectId, options, _) { var subscription, client, progress; var __frame = { name: "__1", line: 37 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {



      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      progress = cli.interaction.progress($("Listing Active Directory groups")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() {

            return adUtils.listGraphObjects(client, "group", cli.interaction, log, __cb(_, __frame, 5, 16, function __$__1() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__1() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__1() { _(); }); }); }); });



  adGroup.command("show").description($("Get Active Directory groups")).option("-o --objectId <objectId>", $("the object Id of the group to return")).option("-c --search <search>", $("Search by display name which starts with the provided value")).execute(function __2(options, _) { var objectId, search, subscription, client, progress, groups; var __frame = { name: "__2", line: 52 }; return __func(_, this, arguments, __2, 1, __frame, function __$__2() {




      objectId = options.objectId;
      search = options.search;

      adUtils.validateParameters({
        objectId: objectId,
        search: search });


      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      progress = cli.interaction.progress($("Getting group list")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {



            return getSpecificGroups(client, objectId, search, __cb(_, __frame, 15, 17, function ___(__0, __1) { groups = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__2() {


          if ((groups.length > 0)) {
            adUtils.displayGroups(groups, cli.interaction, log); }
           else {
            log.data($("No matching group was found")); } ; _(); }); }); }); });



  adGroup.command("create [display-name] [mail-nickname]").description($("Creates an Active Directory group in the tenant.")).usage("[options] <display-name> <mail-nickname>").option("-d --display-name <display-name>", $("The name to display in the address book for the group.")).option("-m --mail-nickname <mail-nickname>", $("The mail alias for the group.")).execute(function __3(displayName, mailNickname, options, _) { var parameters, subscription, client, group; var __frame = { name: "__3", line: 84 }; return __func(_, this, arguments, __3, 3, __frame, function __$__3() {





      if (!displayName) {
        return _(null, cli.missingArgument("display-name")); } ;


      if (!mailNickname) {
        return _(null, cli.missingArgument("mail-nickname")); } ;





      parameters = {
        displayName: displayName,
        mailNickname: mailNickname };


      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      return withProgress(util.format($("Creating Active Directory group: '%s'."), displayName), function __1(log, _) { var __frame = { name: "__1", line: 104 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.groups.create(parameters, __cb(_, __frame, 1, 29, _, true)); }); }, __cb(_, __frame, 19, 18, function ___(__0, __2) { group = __2;

        cli.interaction.formatOutput(group, function(data) {
          if (data) {
            adUtils.displayAGroup(data, log, true); } ; }); _(); }, true)); }); });




  adGroup.command("delete [objectId]").description($("Deletes an Active Directory group.")).usage("[options] <objectId>").option("-o --objectId <objectId>", $("the object Id of the group to delete")).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation)")).execute(function __4(objectId, options, _) { var subscription, client, progress; var __frame = { name: "__4", line: 119 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {






      adUtils.validateParameters({
        objectId: objectId }); return (function __$__4(_) {


        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete group '%s' ? [y/n] "), objectId), __cb(_, __frame, 6, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -118, 17, function ___(__0, __2) { return (function __$__4(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$__4() {



          subscription = profile.current.getSubscription(options.subscription);
          client = adUtils.getADGraphClient(subscription);
          progress = cli.interaction.progress($(("Deleting group with objectId: " + objectId))); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {


                return client.groups.deleteMethod(objectId, __cb(_, __frame, 15, 22, function __$__4() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__4() {

                  progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__4() { _(); }); }); }); }, true)); }); });



  var adGroupMember = adGroup.category("member").description($("Commands to provide an Active Directory sub group or member info"));


  adGroupMember.command("list [objectId]").description($("Provides an Active Directory sub group or member info")).usage("[options] <objectId>").option("-o --objectId <objectId>", $("Object id of group whose members to return.")).execute(function __5(objectId, options, _) { var subscription, client, progress; var __frame = { name: "__5", line: 147 }; return __func(_, this, arguments, __5, 2, __frame, function __$__5() {




      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ;

      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);

      progress = cli.interaction.progress($("Getting group members")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() {

            return adUtils.listGroupMembers(client, objectId, cli.interaction, log, __cb(_, __frame, 9, 16, function __$__5() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__5() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__5() { _(); }); }); }); });



  adGroupMember.command("add [objectId] [member-objectId]").description($("Adds a member to an Active Directory group.")).usage("[options] <objectId> <member-objectId>").option("-o --objectId <objectId>", $("Object id of group to which the member needs to be added.")).option("-m --member-objectId <member-objectId>", $(("Object id of the member (application, user, servicePrincipal, " + "another-group) to be added to this group."))).option("-e --graph-endpoint <graph-endpoint>", $("The graph endpoint which will be the part of the member url created by the command. Default value: 'https://graph.windows.net'.")).option("-t --tenant <tenant>", $("TenantId (in a GUID format) to which the member belongs. Default value is the current tenant of the logged in user/sp.")).execute(function __6(objectId, memberObjectId, options, _) { var subscription, tenant, endpoint, memberUrl, progress, client; var __frame = { name: "__6", line: 170 }; return __func(_, this, arguments, __6, 3, __frame, function __$__6() {








      if (!options) { options = { }; } ;
      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ;


      if (!memberObjectId) {
        return _(null, cli.missingArgument("member-objectId")); } ;

      subscription = profile.current.getSubscription(options.subscription);
      tenant = options.tenant;
      if (!tenant) { tenant = subscription.tenantId; } ;
      endpoint = options.graphEndpoint;
      if (!endpoint) { endpoint = "https://graph.windows.net"; } ;
      memberUrl = util.format("%s/%s/directoryObjects/%s", endpoint, tenant, memberObjectId);
      progress = cli.interaction.progress(util.format($("Adding member '%s' to the Group '%s'."), memberUrl, objectId));
      client = adUtils.getADGraphClient(subscription); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__6() {

            return client.groups.addMember(objectId, memberUrl, __cb(_, __frame, 18, 22, function __$__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__6() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__6() { _(); }); }); }); });



  adGroupMember.command("delete [objectId] [member-objectId]").description($("Deletes a member from an Active Directory group.")).usage("[options] <objectId> <member-objectId>").option("-o --objectId <objectId>", $("Object id of group from which the member needs to be deleted.")).option("-m --member-objectId <member-objectId>", $(("Object id of the member (application, user, servicePrincipal, " + "another-group) to be deleted from this group."))).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation)")).execute(function __7(objectId, memberObjectId, options, _) { var subscription, client, progress; var __frame = { name: "__7", line: 201 }; return __func(_, this, arguments, __7, 3, __frame, function __$__7() {







      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ;


      if (!memberObjectId) {
        return _(null, cli.missingArgument("member-objectId")); } ; return (function __$__7(_) {


        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete member from the group '%s' ? [y/n] "), objectId), __cb(_, __frame, 9, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -200, 17, function ___(__0, __2) { return (function __$__7(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$__7() {


          subscription = profile.current.getSubscription(options.subscription);
          client = adUtils.getADGraphClient(subscription);

          progress = cli.interaction.progress(util.format($("Deleting member '%s' from Group '%s'."), memberObjectId, objectId)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__7() {

                return client.groups.removeMember(objectId, memberObjectId, __cb(_, __frame, 17, 22, function __$__7() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__7() {

                  progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__7() { _(); }); }); }); }, true)); }); });



  adGroupMember.command("check [objectId] [member-objectId]").description($("Checks whether the specified user, group, contact, or service principal is a direct or a transitive member of the specified Active Directory group.")).usage("[options] <objectId> <member-objectId>").option("-o --objectId <objectId>", $("Object id of group against which the membership needs to be checked.")).option("-m --member-objectId <member-objectId>", $(("Object id of the member (application, user, servicePrincipal, " + "another-group) for which membership needs to be checked in the specified group."))).execute(function __8(objectId, memberObjectId, options, _) { var progress, subscription, client, result, parameters; var __frame = { name: "__8", line: 230 }; return __func(_, this, arguments, __8, 3, __frame, function __$__8() {






      if (!options) { options = { }; } ;
      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ;


      if (!memberObjectId) {
        return _(null, cli.missingArgument("member-objectId")); } ;

      progress = cli.interaction.progress(util.format($("Checking membership of member '%s' in the Group '%s'."), memberObjectId, objectId));
      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__8() {


            parameters = { groupId: objectId, memberId: memberObjectId };
            return client.groups.isMemberOf(parameters, __cb(_, __frame, 15, 31, function ___(__0, __1) { result = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__8() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__8() {

          if (options.json) {
            log.json(result); }
           else {
            log.data($("IsMember:    "), (result.value ? "true" : "false")); } ; _(); }); }); }); });};




function getSpecificGroups(client, objectId, search, _) { var parameters, group; var __frame = { name: "getSpecificGroups", line: 257 }; return __func(_, this, arguments, getSpecificGroups, 3, __frame, function __$getSpecificGroups() { return (function __$getSpecificGroups(__then) {
      if (search) {
        parameters = { filter: (("startswith(displayName,'" + search) + "')") };
        return client.groups.list(parameters, __cb(_, __frame, 3, 25, _, true)); } else {

        return client.groups.get(objectId, __cb(_, __frame, 5, 30, function ___(__0, __2) { group = __2;
          return _(null, (group ? [group,] : [])); }, true)); } ; })(_); });};
