/// <summary>
 /// 更新用户
 /// </summary>
 public async Task UpdateUser(EditUserInput input, int tenantId)
 {
     using (CurrentUnitOfWork.SetTenantId(tenantId))
     {
         await _userAppService.UpdateUser(input);
     }
 }
Example #2
0
        public async Task EditRole(EditUserInput input)
        {
            var id   = input.Id;
            var role = input.MapTo <Role>();

            role.TenantId = AbpSession.TenantId;
            if (id == null)
            {
                CheckErrors(await _roleManager.CreateAsync(role));
            }
            else
            {
                var isRepetition = await _roleManager.CheckDuplicateRoleNameAsync(id, input.Name, input.DisplayName);

                CheckErrors(isRepetition);
                if (isRepetition.Succeeded == true)
                {
                    var roleUpdate = await _roleManager.GetRoleByIdAsync(Convert.ToInt32(id));

                    roleUpdate.Name        = input.Name;
                    roleUpdate.DisplayName = input.DisplayName;
                    roleUpdate.IsStatic    = input.IsStatic;
                    await _roleManager.UpdateAsync(roleUpdate);
                }
            }
            //var role = input.MapTo<Role>();
            //await _roleRepository.InsertOrUpdateAsync(role);
        }
Example #3
0
        /// <summary>
        /// 更新操作员列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateSysOperator(EditUserInput input)
        {
            var account = Map <EditUserInput, Account>(input);

            await _accountRepository
            .UpdateAsync(account, d => d.UserName, d => d.BirthOn, d => d.FullName,
                         d => d.Avatar, d => d.IsDisplay, d => d.Phone, d => d.RoleId);

            await _accountRepository.UnitOfWork.SaveChangesAsync();
        }
Example #4
0
        public Task <IActionResult> Update(string id, [FromBody] EditUserInput input)
        {
            return(Task.Run <IActionResult>(() =>
            {
                // Check if user exist.
                var user = _db.Single <User>(u => u.Id == id);

                if (user == null)
                {
                    return new StatusCodeResult((int)HttpStatusCode.BadRequest);
                }


                // If the username changes, check if the new one is not taken.
                input.Username = input.Username.ToLower();
                if (input.Username != null && input.Username != user.Username)
                {
                    var u = _db.Single <User>(U => U.Username == input.Username);
                    if (u != null)
                    {
                        return new StatusCodeResult((int)HttpStatusCode.Conflict);
                    }
                }

                try
                {
                    var dic = input.ToDictionary();

                    // If changing the password.
                    if (input.Password != null)
                    {
                        if (!ValidatePassword(input.Password))
                        {
                            return BadRequest(new
                            {
                                Message = "Password not valid"
                            });
                        }

                        dic["Password"] = BCrypt.Net.BCrypt.HashPassword(input.Password);
                    }

                    _db.Update <User>(user.Id, dic);

                    return Ok(new
                    {
                        success = true
                    });
                }
                catch (Exception)
                {
                    return new StatusCodeResult((int)HttpStatusCode.InternalServerError);
                }
            }));
        }
Example #5
0
        [AbpAuthorize("CanEditUsers")] //An example of permission checking
        public async Task EditUser(EditUserInput input)
        {
            if (AbpSession.MultiTenancySide == MultiTenancySides.Host)
            {
                CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, input.TenantId);
                var user = _userRepository.Get(input.Id);
                Mapper.CreateMap<EditUserInput, User>();
                input.MapTo<EditUserInput, User>(user);

                await _userRepository.UpdateAsync(user);
            }
        }
Example #6
0
        [AbpAuthorize("CanEditUsers")] //An example of permission checking
        public async Task EditUser(EditUserInput input)
        {
            if (AbpSession.MultiTenancySide == MultiTenancySides.Host)
            {
                CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, input.TenantId);
                var user = _userRepository.Get(input.Id);
                Mapper.CreateMap <EditUserInput, User>();
                input.MapTo <EditUserInput, User>(user);

                await _userRepository.UpdateAsync(user);
            }
        }
Example #7
0
        public async Task EditUserAsync_Throws_KeyNotFoundException()
        {
            // Arrange
            var guid      = Guid.NewGuid().ToString();
            var editInput = new EditUserInput {
                Email = "*****@*****.**", UserName = "******"
            };

            _userManager.Setup(um => um.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(null as ApplicationUser);

            // Act
            var userService = new UserService(_userManager.Object, _logger.Object, _mapper);
            await Assert.ThrowsExceptionAsync <KeyNotFoundException>(async() => await userService.EditUserAsync(guid, editInput));
        }
Example #8
0
        /// <summary>
        /// 更新用户
        /// </summary>
        public async Task UpdateUser(EditUserInput input)
        {
            var user = await UserManager.GetUserByIdAsync(input.Id);

            if (user == null)
            {
                throw new UserFriendlyException($"用户ID:[{input.Id}]不存在!");
            }

            input.MapTo(user);
            CheckErrors(await UserManager.UpdateAsync(user));

            //通知
            await this._etupirkaPortalNotifier.WelcomeToTheApplicationAsync(user);
        }
        //POST :/api/EditUserProfile
        public async Task <IActionResult> EditProfile(EditUserInput input)
        {
            string userId = User.Claims.First(c => c.Type == "UserID").Value;
            var    user   = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                user.Email    = input.Email;
                user.FullName = input.FullName;
                user.UserName = input.UserName;
                var result = await _userManager.UpdateAsync(user);

                return(Ok(result));
            }
            return(BadRequest(new { message = "Error by editing user" }));
        }
        public async Task Put_Returns_409_Conflict_When_User_Could_Not_Be_Updated()
        {
            // Arrange
            var guid       = Guid.NewGuid().ToString();
            var updateUser = new EditUserInput {
                Email = "*****@*****.**", UserName = "******"
            };

            _userService.Setup(us => us.EditUserAsync(It.IsAny <string>(), It.IsAny <EditUserInput>())).Throws <InvalidOperationException>();

            // Act
            var controller = new UsersController(_logger.Object, _userService.Object);

            var result = (StatusCodeResult)await controller.Put(guid, updateUser);

            // Assert
            Assert.AreEqual(StatusCodes.Status409Conflict, result.StatusCode);
        }
Example #11
0
        private async Task <RequestResult <UserOutput> > CreateAsync(EditUserInput input)
        {
            RequestResult <UserOutput> rst = new RequestResult <UserOutput>();

            try
            {
                var sysUser = ObjectMapper.Map <SysUser>(input);
                sysUser = await _sysUserRepository.InsertAsync(sysUser).ConfigureAwait(false);

                rst.ResultData = ObjectMapper.Map <UserOutput>(sysUser);
                rst.Flag       = true;
            }
            catch
            {
                rst.Flag = false;
            }

            return(rst);
        }
Example #12
0
        public async Task EditUserAsync_Throws_InvalidOperationException()
        {
            // Arrange
            var guid      = Guid.NewGuid().ToString();
            var editInput = new EditUserInput {
                Email = "*****@*****.**", UserName = "******"
            };

            _userManager.Setup(um => um.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser {
                Id = guid, Email = "*****@*****.**", UserName = "******"
            });
            _userManager.Setup(um => um.UpdateAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(IdentityResult.Failed());

            // Act
            var userService = new UserService(_userManager.Object, _logger.Object, _mapper);

            // Assert
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await userService.EditUserAsync(guid, editInput));
        }
Example #13
0
        private async Task <RequestResult <UserOutput> > UpdateAsync(EditUserInput input)
        {
            RequestResult <UserOutput> rst = new RequestResult <UserOutput>();

            try
            {
                var user = await _sysUserRepository.FirstOrDefaultAsync(u => u.Id == input.Id).ConfigureAwait(false);

                input.CreationTime  = user.CreationTime;
                input.CreatorUserId = user.CreatorUserId;
                ObjectMapper.Map(input, user);
                rst.Flag       = true;
                rst.ResultData = ObjectMapper.Map <UserOutput>(user);;
            }
            catch
            {
                rst.Flag = false;
            }
            return(rst);
        }
Example #14
0
        public async Task EditUserAsync_Returns_Edited_User()
        {
            // Arrange
            var guid      = Guid.NewGuid().ToString();
            var editInput = new EditUserInput {
                Email = "*****@*****.**", UserName = "******"
            };

            _userManager.Setup(um => um.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser {
                Id = guid, Email = "*****@*****.**", UserName = "******"
            });
            _userManager.Setup(um => um.UpdateAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(IdentityResult.Success);

            // Act
            var userService = new UserService(_userManager.Object, _logger.Object, _mapper);
            var result      = await userService.EditUserAsync(guid, editInput);

            Assert.AreEqual(editInput.Email, result.Email);
            Assert.AreEqual(guid, result.Id);
        }
Example #15
0
        public async Task <UserDto> EditUserAsync(string id, EditUserInput input)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                throw new KeyNotFoundException($"The user with id: {id} does not exist.");
            }

            _mapper.Map(input, user);

            var result = await _userManager.UpdateAsync(user);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Could not update user with id: {id}");
            }

            return(_mapper.Map <UserDto>(user));
        }
        public async Task Put_Returns_200_OK_When_User_Updated()
        {
            // Arrange
            var guid       = Guid.NewGuid().ToString();
            var updateUser = new EditUserInput {
                Email = "*****@*****.**", UserName = "******"
            };
            var outputuser = new UserDto {
                Email = "*****@*****.**", UserName = "******", Id = guid
            };

            _userService.Setup(us => us.EditUserAsync(It.IsAny <string>(), It.IsAny <EditUserInput>())).Returns(() => Task.FromResult(outputuser));

            // Act
            var controller = new UsersController(_logger.Object, _userService.Object);

            var result = (ObjectResult)await controller.Put(guid, updateUser);

            // Assert
            Assert.AreEqual(StatusCodes.Status200OK, result.StatusCode);
            Assert.AreEqual(updateUser.UserName, ((UserDto)result.Value).UserName);
        }
Example #17
0
        public async Task <IActionResult> Put(string id, [FromBody] EditUserInput userInput)
        {
            _logger.LogInformation($"Update user with id: {id} started.");
            try
            {
                var updatedUser = await _userService.EditUserAsync(id, userInput);

                _logger.LogInformation($"User with id: {id} updated successfully.");

                return(StatusCode(StatusCodes.Status200OK, updatedUser));
            }
            catch (KeyNotFoundException ex)
            {
                _logger.LogInformation(ex.Message);
                return(StatusCode(StatusCodes.Status404NotFound));
            }
            catch (InvalidOperationException ex)
            {
                _logger.LogInformation(ex.Message);
                return(StatusCode(StatusCodes.Status409Conflict));
            }
        }
Example #18
0
        public async Task <RequestResult <UserOutput> > CreateOrUpdateAsync(EditUserInput input)
        {
            if (input == null)
            {
                return(new RequestResult <UserOutput>());
            }
            RequestResult <UserOutput> rst;
            LoginUserOutput            CurrentUser = base.GetCurrentUser();

            if (input.Id != null)
            {
                input.LastModificationTime = DateTime.Now;
                input.LastModifierUserId   = CurrentUser.Id;
                rst = await this.UpdateAsync(input).ConfigureAwait(false);
            }
            else
            {
                input.CreationTime  = DateTime.Now;
                input.CreatorUserId = CurrentUser.Id;
                rst = await this.CreateAsync(input).ConfigureAwait(false);
            }
            return(rst);
        }
        public async Task <ApiResult> Edit(EditUserInput input)
        {
            await passportService.UpdateSysOperator(input);

            return(ApiResult.Success);
        }