public async Task <IActionResult> UploadPicture(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return(Content("file not selected"));
            }

            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            var path = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot",
                user.Email + file.FileName);

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            user.Picture = user.Email + file.FileName;
            var response = await _usersWriteService.UpdateUserAsync(user);

            if (response.Status == ResponseStatus.Failed)
            {
                return(BadRequest());
            }
            else if (response.Status == ResponseStatus.Unauthorized)
            {
                return(Unauthorized());
            }

            return(Ok(user.Picture));
        }
Пример #2
0
        public async Task <IdentityResult> UpdateAsync(TravelUser user, CancellationToken cancellationToken)
        {
            var result = await _usersWriteService.UpdateUserAsync(user);

            if (result.Status == ResponseStatus.Succeeded)
            {
                return(IdentityResult.Success);
            }
            return(IdentityResult.Failed());
        }
        public async Task <IActionResult> Put(string id, [FromBody] UserViewModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            try
            {
                var editor = await _userManager.FindByEmailAsync(User.Identity.Name);

                var roles = await _userManager.GetRolesAsync(editor);

                if (user.Role == "admin" && !roles.Contains("admin"))
                {
                    return(Unauthorized());
                }

                var oldUser = _usersReadService.GetUserById(id).Result.User;
                oldUser.EmailConfirmed = user.EmailConfirmed;
                oldUser.Role           = user.Role;
                if (!user.isLocked)
                {
                    oldUser.LockoutEnd = null;
                }
                else
                {
                    oldUser.LockoutEnd = new DateTimeOffset(DateTime.Now, new TimeSpan(400, 0, 0, 0, 0));
                }

                var result = await _usersWriteService.UpdateUserAsync(oldUser);

                if (result.Status == ResponseStatus.Unauthorized)
                {
                    return(Unauthorized());
                }
                if (result.Status == ResponseStatus.Failed)
                {
                    return(BadRequest());
                }

                var updatedUser = result.User;

                if (updatedUser == null)
                {
                    return(NotFound());
                }

                return(Ok(MapToUserToUserVM(updatedUser)));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }