コード例 #1
0
        public async Task <IActionResult> Create(AccountEditViewModel vm)
        {
            RemoveNavigationPropertiesFromModelState <AccountEditViewModel>();
            if (ModelState.IsValid)
            {
                ViewHelpers.RemoveAllNavigationProperties(vm);

                if (string.IsNullOrEmpty(vm.Password))
                {
                    ModelState.AddModelError(nameof(AccountEditViewModel.Password), "A Password must be set");
                    return(View(vm));
                }

                if (!await IsCurrentUserSuperAdminAsync().ConfigureAwait(false))
                {
                    vm.IsAdmin      = false;
                    vm.IsSuperAdmin = false;
                }

                var result = await _mapper.CreateAccountAsync(vm).ConfigureAwait(false);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError(string.Empty, result.Errors.First().Description);
                    return(View(vm));
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vm));
        }
コード例 #2
0
        public async Task CreateAccounts(GrowDbContext context, AccountVmMapper mapper)
        {
            var errors = new List <string>();
            var random = new Random();

            TemporaryPasswords = new Dictionary <string, string>();

            foreach (var(email, teamName) in Emails)
            {
                var teamId = context.Teams.FirstOrDefault(t => t.Name.Equals(teamName, StringComparison.InvariantCultureIgnoreCase))?.Id;
                if (teamId == null)
                {
                    errors.Add($"Email {email} was skipped since corresponding team was not found in the database");
                    continue;
                }

                var account = new AccountEditViewModel()
                {
                    Email        = email,
                    Name         = email,
                    Password     = random.Next(268435456, int.MaxValue).ToString("X"), //produces 8-char hex string
                    IsAdmin      = false,
                    IsSuperAdmin = false,
                    IsActive     = true,
                    TeamId       = teamId
                };

                var result = await mapper.CreateAccountAsync(account).ConfigureAwait(false);

                if (!result.Succeeded)
                {
                    errors.Add(result.Errors.First().Description);
                }
                else
                {
                    TemporaryPasswords.Add(email, account.Password);
                }
            }
        }