public async Task <IActionResult> Index(SetupViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (_userManager.Users.Any())
            {
                ModelState.AddModelError(String.Empty, Texts.Backend_Identity_Setup_Status_UsersAlreadyConfigured);
                return(View(model));
            }

            if (!_credentialGenerator.CheckPassword(model.NewPassword))
            {
                ModelState.AddModelError(String.Empty, Texts.Backend_Identity_Setup_Status_PasswordComplexity);
                return(View(model));
            }

            var adminRole = await _roleManager.FindByNameAsync(CommonConstants.AdministratorsRole);

            if (adminRole == null)
            {
                _logger.LogInformation("No Administrator-Role has been found.");

                ModelState.AddModelError(String.Empty, Texts.Backend_General_Error_Label);
                return(View(model));
            }

            var adminUser = await _userManager.FindByNameAsync(model.EMailAddress);

            if (adminUser == null)
            {
                adminUser = new OspUser {
                    UserName = model.EMailAddress, Email = model.EMailAddress
                };

                await _userManager.CreateAsync(adminUser, model.NewPassword);

                await _userManager.AddToRoleAsync(adminUser, adminRole.NormalizedName);
            }

            return(RedirectToAction("Index", "Home"));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddAdminUser([FromBody] AdminUserDto adminUserDto)
        {
            if (_userManager.Users.Any())
            {
                return(NotFound("The request is not valid for this configuration."));
            }

            if (!_credentialGenerator.CheckPassword(adminUserDto.Password))
            {
                _logger.LogInformation("The password does not comply with the minimum requirements.");
                return(StatusCode(StatusCodes.Status406NotAcceptable));
            }

            var adminRole = await _roleManager.FindByNameAsync(CommonConstants.AdministratorsRole);

            if (adminRole == null)
            {
                _logger.LogInformation("No Administrator-Role has been found.");
                return(StatusCode(StatusCodes.Status406NotAcceptable));
            }

            var adminUser = await _userManager.FindByNameAsync(adminUserDto.EMail);

            if (adminUser == null)
            {
                adminUser = new OspUser {
                    UserName = adminUserDto.EMail, Email = adminUserDto.EMail
                };

                await _userManager.CreateAsync(adminUser, adminUserDto.Password);

                await _userManager.AddToRoleAsync(adminUser, adminRole.Id.ToString());
            }

            return(Ok());
        }