Exemple #1
0
        public async Task <IActionResult> RegisterAppsToOrgAsync([FromQuery] string identifier, [FromQuery] string appShortNames)
        {
            try
            {
                if (!await UserIsEnvAdminAsync())
                {
                    return(new UnauthorizedResult());
                }

                //pretend this is an 'automated' user...
                Cartomatic.Utils.Identity.ImpersonateGhostUserViaHttpContext();
                Cartomatic.Utils.Identity.ImpersonateGhostUser();

                Organization org = null;

                if (Guid.TryParse(identifier, out var orgId))
                {
                    org = await _db.Organizations.FirstOrDefaultAsync(o => o.Uuid == orgId);
                }
                else
                {
                    org = await _db.Organizations.FirstOrDefaultAsync(o => o.Slug == identifier);
                }

                if (org == null)
                {
                    return(BadRequest("Org not found"));
                }

                var shortNames = (appShortNames ?? string.Empty).Split(',');
                var apps       = await _db.Applications.Where(app => shortNames.Contains(app.ShortName)).ToListAsync();

                if (!apps.Any())
                {
                    return(BadRequest("Apps not found"));
                }

                foreach (var app in apps)
                {
                    org.AddLink(app);
                }
                await org.UpdateAsync(_db);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }
Exemple #2
0
        /// <summary>
        /// Registers apps with an organization - adds the apps to an organization as links
        /// </summary>
        /// <param name="org"></param>
        /// <param name="appsToAdd"></param>
        /// <returns></returns>
        protected async Task RegisterAppsWithOrgAsync(Organization org, string[] appsToAdd)
        {
            if (org == null)
            {
                ConsoleEx.WriteErr("Cannot add apps to org - organization missing!");
                return;
            }

            if (appsToAdd.Length == 0)
            {
                ConsoleEx.WriteErr("Cannot add apps to org - apps not specified!");
                return;
            }

            var apps = GetApps().Where(a => appsToAdd.Contains(a.ShortName));

            ConsoleEx.WriteLine(
                $"Registering apps: {string.Join(", ", apps.Select(a => a.ShortName))} with an org: {org.DisplayName} ({org.Slug})... ",
                ConsoleColor.DarkYellow);

            if (RemoteMode)
            {
                await RegisterAppsWithOrgRemoteAsync(org, apps);
            }
            else
            {
                foreach (var a in apps)
                {
                    org.AddLink(a);
                }

                using (var dbCtx = GetMapHiveDbContext())
                {
                    await org.UpdateAsync(dbCtx);
                }
            }

            ConsoleEx.WriteOk($"{string.Join(", ", apps.Select(a => a.ShortName))} registered!" + Environment.NewLine);
        }