Ejemplo n.º 1
0
        public async Task <ApplicationUserEditDto> UpdateUserAsync(string id, ApplicationUserEditDto dto)
        {
            await EnsureUserExists(id);
            await ValidateApplicationUserEditDto(id, dto);

            var user = await Manager.FindByIdAsync(id);

            if (!string.IsNullOrEmpty(dto.Password))
            {
                var token = await Manager.GeneratePasswordResetTokenAsync(user);

                var result = await Manager.ResetPasswordAsync(user, token, dto.Password);

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

            user.ContestantId   = dto.ContestantId;
            user.ContestantName = dto.ContestantName;
            await Manager.UpdateAsync(user);

            var pairs = new List <KeyValuePair <bool, string> >
            {
                new KeyValuePair <bool, string>(dto.IsAdministrator.GetValueOrDefault(), ApplicationRoles.Administrator),
                new KeyValuePair <bool, string>(dto.IsUserManager.GetValueOrDefault(), ApplicationRoles.UserManager),
                new KeyValuePair <bool, string>
                    (dto.IsContestManager.GetValueOrDefault(), ApplicationRoles.ContestManager),
                new KeyValuePair <bool, string>
                    (dto.IsSubmissionManager.GetValueOrDefault(), ApplicationRoles.SubmissionManager)
            };

            var roles = new List <string>();

            foreach (var pair in pairs)
            {
                if (pair.Key)
                {
                    roles.Add(pair.Value);
                    if (!await Manager.IsInRoleAsync(user, pair.Value))
                    {
                        await Manager.AddToRoleAsync(user, pair.Value);
                    }
                }
                else
                {
                    if (await Manager.IsInRoleAsync(user, pair.Value))
                    {
                        await Manager.RemoveFromRoleAsync(user, pair.Value);
                    }
                }
            }

            await LogInformation($"UpdateUser Id={user.Id} ContestantId={user.ContestantId} " +
                                 $"ContestantName={user.ContestantName} Roles={roles}");

            return(new ApplicationUserEditDto(user, await Manager.GetRolesAsync(user)));
        }
Ejemplo n.º 2
0
        private async Task ValidateApplicationUserEditDto(string id, ApplicationUserEditDto dto)
        {
            if (await Manager.Users.AnyAsync(u => u.Id != id && u.ContestantId == dto.ContestantId))
            {
                throw new ValidationException("Contestant ID already taken.");
            }

            if (dto.ContestantId.Length > 50)
            {
                throw new ValidationException("Contestant ID must be shorter than 50 characters.");
            }

            if (dto.ContestantName.Length > 20)
            {
                throw new ValidationException("Contestant Name must be shorter than 20 characters.");
            }
        }
Ejemplo n.º 3
0
 public async Task <ActionResult <ApplicationUserEditDto> > UpdateUser(string id, ApplicationUserEditDto dto)
 {
     try
     {
         return(Ok(await _service.UpdateUserAsync(id, dto)));
     }
     catch (NotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (ValidationException e)
     {
         return(BadRequest(e.Message));
     }
 }