Exemplo n.º 1
0
        /// <summary>
        /// Checks if the deployment page can be displayed
        /// </summary>
        /// <returns>true if the page can be displayed, else false</returns>
        private async Task <bool> DisplayPage()
        {
            if (string.IsNullOrEmpty(_firstTimeDeploymentPassword))
            {
                return(false);
            }

            // Check if an Admin already exists
            try
            {
                bool adminUserExists = await _userDbAccess.DoesAdminUserExist();

                if (adminUserExists)
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while checking if first time deployment page should be displayed");
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Configures the admin account
        /// </summary>
        /// <returns>Task</returns>
        private static async Task ConfigureAdminAccount()
        {
            ServiceProvider serviceProvider = ServiceProviderBuilder.BuildServiceProvider();

            IUserDbAccess userDbAccess = serviceProvider.GetService <IUserDbAccess>();

            if (await userDbAccess.DoesAdminUserExist())
            {
                MessageService.PrintAdminAccountExistMessage();
                return;
            }

            string email;

            do
            {
                try
                {
                    string displayName = MessageService.GetAdminAccountDisplayName();
                    email = MessageService.GetAdminAccountEMail();
                    string password = MessageService.GetAdminAccountPassword();

                    IUserCreator   userCreator = serviceProvider.GetService <IUserCreator>();
                    IdentityResult result      = await userCreator.CreateUser(new MockUrlHelper(), "https", displayName, email, password, RoleNames.Administrator);

                    if (!result.Succeeded)
                    {
                        throw new Exception(string.Join(',', result.Errors.Select(e => e.Description)));
                    }

                    MessageService.PrintSuccessCreatingAdminAccount();
                    break;
                }
                catch (Exception ex)
                {
                    MessageService.PrintErrorCreatingAdminAccount(ex);
                }
            }while(true);

            try
            {
                UserManager <GoNorthUser> userManager = serviceProvider.GetService <UserManager <GoNorthUser> >();
                GoNorthUser adminUser = await userManager.FindByEmailAsync(email);

                List <string> rolesToAdd = RoleNames.GetAllRoleNames().Where(r => r != RoleNames.Administrator).ToList();

                IdentityResult result = await userManager.AddToRolesAsync(adminUser, rolesToAdd);

                if (!result.Succeeded)
                {
                    throw new Exception(string.Join(',', result.Errors.Select(e => e.Description)));
                }
            }
            catch (Exception ex)
            {
                MessageService.PrintErrorAssignAllRolesToUser(ex);
            }
        }