/*** 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 adUtils = require("./adUtils");
















var profile = require("../../../util/profile");
var utils = require("../../../util/utils");
var util = require("util");
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 adUser = ad.category("user").description($("Commands to display Active Directory users"));


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



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

            return adUtils.listGraphObjects(client, "user", 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() { _(); }); }); }); });



  adUser.command("show").description($("Get an Active Directory user")).option("-u --upn <upn>", $("the principal name of the user to return")).option("-o --objectId <objectId>", $("the object id of the user to return")).option("-c --search <search>", $("search users with display name starting with the provided value")).execute(function __2(options, _) { var upn, objectId, search, subscription, client, progress, users, parameters, user; var __frame = { name: "__2", line: 51 }; return __func(_, this, arguments, __2, 1, __frame, function __$__2() {





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

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

      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      progress = cli.interaction.progress($("Getting Active Directory user"));
      users = [];
      parameters = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() { return (function __$__2(__then) {

              if (upn) {
                parameters = { filter: (("userPrincipalName eq '" + upn) + "'") };
                return client.users.list(parameters, __cb(_, __frame, 18, 31, function ___(__0, __1) { users = __1; __then(); }, true)); } else { return (function __$__2(__then) {
                  if (objectId) {
                    return client.users.get(objectId, __cb(_, __frame, 20, 34, function ___(__0, __2) { user = __2;
                      if (user) {
                        users.push(user); } ; __then(); }, true)); } else {


                    parameters = { filter: (("startswith(displayName,'" + search) + "')") };
                    return client.users.list(parameters, __cb(_, __frame, 26, 31, function ___(__0, __3) { users = __3; __then(); }, true)); } ; })(__then); } ; })(function __$__2() { _(null, null, 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 ((users.length > 0)) {
            adUtils.displayUsers(users, cli.interaction, log); }
           else {
            log.error($("No matching user was found")); } ; _(); }); }); }); });



  adUser.command("delete [upn-or-objectId]").description($("Deletes Active Directory user (work/school account also popularly known as org-id).")).usage("[options] <upn-or-objectId>").option("-u --upn-or-objectId <upn-or-objectId>", $("the user principal name or the objectId.")).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation).")).execute(function __3(upnOrObjectId, options, _) { var subscription, client, progress; var __frame = { name: "__3", line: 95 }; return __func(_, this, arguments, __3, 2, __frame, function __$__3() {





      if (!upnOrObjectId) {
        return _(null, cli.missingArgument("upn-or-objectId")); } ; return (function __$__3(_) {


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



          subscription = profile.current.getSubscription(options.subscription);
          client = adUtils.getADGraphClient(subscription);
          progress = cli.interaction.progress(util.format($("Deleting user: '%s'"), upnOrObjectId)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {

                return client.users.deleteMethod(upnOrObjectId, __cb(_, __frame, 13, 21, function __$__3() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() {

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



  adUser.command("create [upn] [display-name] [mail-nickname] [password]").description($(("Create Active Directory user (work/school account also popularly known as org-id). " + "For more information: https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#CreateUser"))).usage("[options] <upn> <display-name> <mail-nickname> <password>").option("-u --upn <upn>", $("the user principal name. example - 'someuser@contoso.com'.")).option("-i --immutableId <immutableId>", $("It needs to be specified only if you are using a federated domain for the user's user principal name (upn) property.")).option("-d --display-name <display-name>", $("the name to display in the address book for the user. example 'Alex Wu'")).option("-m --mail-nickname <mail-nickname>", $("the mail alias for the user. example: 'Alexw'.")).option("-p --password <password>", $("password for the user. It must meet the tenant's password complexity requirements. It is recommended to set a strong password.")).option("-f --force-change-password-next-login", $(("It must be specified if the user must change the password on the next successful login (true). " + "Default behavior is (false) to not change the password on the next successful login."))).execute(function __4(upn, displayName, mailNickname, password, options, _) { var forceChangePasswordNextLogin, subscription, client, userParams, user; var __frame = { name: "__4", line: 125 }; return __func(_, this, arguments, __4, 5, __frame, function __$__4() {











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


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


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


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

      forceChangePasswordNextLogin = false;
      if ((options && options.forceChangePasswordNextLogin)) {
        forceChangePasswordNextLogin = true; } ;


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

      userParams = {
        accountEnabled: true,
        userPrincipalName: upn,
        displayName: displayName,
        mailNickname: mailNickname,
        passwordProfile: {
          password: password,
          forceChangePasswordNextLogin: forceChangePasswordNextLogin } };



      if ((options && options.immutableId)) {
        userParams.immutableId = options.immutableId; } ;


      return withProgress(util.format($("Creating Active Directory user: '%s'."), upn), function __1(log, _) { var __frame = { name: "__1", line: 165 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.users.create(userParams, __cb(_, __frame, 1, 28, _, true)); }); }, __cb(_, __frame, 39, 17, function ___(__0, __2) { user = __2;

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




  adUser.command("set [upn-or-objectId]").description($(("Update an existing Active Directory user (work/school account also popularly known as org-id). " + "For more information: https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#UpdateUser"))).usage("[options] <upn-or-objectId>").option("-u --upn-or-objectId <upn-or-objectId>", $("the user principal name ('someuser@contoso.com') or the objectId (a Guid) for which the properties need to be updated.")).option("-a --enable-account <enable-account>", $("true for enabling the account; otherwise, false.")).option("-d --display-name <display-name>", $("new name to display in the address book for the user. example 'Alex Wu'")).option("-m --mail-nickname <mail-nickname>", $("new mail alias for the user. example: 'Alexw'.")).option("-p --password <password>", $("new password for the user. It must meet the tenant's password complexity requirements. It is recommended to set a strong password.")).option("-f --force-change-password-next-login", $(("It must be specified only when you are updating the password. Otherwise it will be ignored. " + "It must be specified if the user must change the password on the next successful login (true). Default behavior is (false) to not change the password on the next successful login."))).execute(function __5(upnOrObjectId, options, _) { var displayName, mailNickname, password, accountEnabled, userUpdateParams, forceChangePasswordNextLogin, subscription, client, progress; var __frame = { name: "__5", line: 186 }; return __func(_, this, arguments, __5, 2, __frame, function __$__5() {











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

      displayName = options.displayName;
      mailNickname = options.mailNickname;
      password = options.password;
      accountEnabled = options.enableAccount;
      userUpdateParams = { };

      if (displayName) {
        userUpdateParams.displayName = displayName; } ;


      if (mailNickname) {
        userUpdateParams.mailNickname = mailNickname; } ;


      if (accountEnabled) {
        if (((accountEnabled.toLowerCase() === "true") || (accountEnabled.toLowerCase() === "false"))) {
          userUpdateParams.accountEnabled = ((accountEnabled.toLowerCase() === "true") ? true : false); }
         else {
          return _(new Error(util.format("Valid values for --enable-account are: true, false. The provides value %s is invalid.", accountEnabled))); } ; } ;



      if (password) {
        userUpdateParams.passwordProfile = { };
        userUpdateParams.passwordProfile.password = password;
        forceChangePasswordNextLogin = false;
        if ((options && options.forceChangePasswordNextLogin)) {
          forceChangePasswordNextLogin = true; } ;

        userUpdateParams.passwordProfile.forceChangePasswordNextLogin = forceChangePasswordNextLogin; } ;


      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      progress = cli.interaction.progress(util.format($("Updating Active Directory user: '%s'."), upnOrObjectId)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() {

            return client.users.update(upnOrObjectId, userUpdateParams, __cb(_, __frame, 41, 21, 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() { _(); }); }); }); });



  var adMemberGroups = adUser.category("memberGroups").description($("Commands to display member groups of the Active Directory user."));


  adMemberGroups.command("list [objectId]").description($("Provides a lit of Object IDs of the groups of which the user is a member.")).usage("[options] <objectId>").option("-o --objectId <objectId>", $("the objectId of the user")).option("-e --securityEnabledOnly", $("If true, only membership in security enabled groups will be checked. Otherwise membership in all groups will be checked. Default: false")).execute(function __6(objectId, options, _) { var subscription, client, securityEnabledOnly, groups, i, group; var __frame = { name: "__6", line: 241 }; return __func(_, this, arguments, __6, 2, __frame, function __$__6() {





      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ;

      securityEnabledOnly = false;
      if ((options && options.securityEnabledOnly)) {
        securityEnabledOnly = true; } ;


      return withProgress($(("Getting member groups for the user: " + objectId)), function __1(log, _) { var __frame = { name: "__1", line: 253 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.users.getMemberGroups(objectId, securityEnabledOnly, __cb(_, __frame, 1, 28, _, true)); }); }, __cb(_, __frame, 11, 19, function ___(__0, __2) { groups = __2;


        if ((groups.length === 0)) {
          if (options.json) {
            log.json(groups); }
           else {
            log.data("No member groups found."); } ; } ;



        i = 0; var __6 = false; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__6() { __more = false; if (__6) { i++; } else { __6 = true; } ; var __5 = (i < groups.length); if (__5) {
              return client.groups.get(groups[i], __cb(_, __frame, 25, 34, function ___(__0, __3) { group = __3;
                return adUtils.displayGroupMembers([group,], cli.interaction, log, __cb(_, __frame, 26, 16, function __$__6() { while (__more) { __loop(); }; __more = true; }, true)); }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(_); }, true)); }); });};
