예제 #1
0
        public async Task <IActionResult> AddAsync([FromForm] AddUserInputModel model)
        {
            var service = Ioc.Get <IUserService>();

            if (await service.IsAccountExist(model.Account))
            {
                return(Ok(new StandardResult().Fail(StandardCode.LogicError, "账号已存在")));
            }

            var salt = PasswordHelper.GenerateSalt();
            var user = new User
            {
                Id          = Guid.NewGuid().ToString(),
                RealName    = model.RealName,
                Account     = model.Account,
                Salt        = salt,
                Password    = PasswordHelper.ComputeHash("123456", salt),
                IdCard      = model.IdCard,
                Email       = model.Email,
                Birthday    = model.Birthday,
                Mobile      = model.Mobile,
                Gender      = model.Gender,
                CreatedTime = DateTime.Now,
                CreatedBy   = CurrentUserId,
                Enabled     = model.Enabled
            };
            await service.AddAsync(user);

            return(Ok(new StandardResult().Succeed("添加成功")));
        }
예제 #2
0
        public async Task <IdentityResult> AddUserAsync(AddUserInputModel input, ClaimsPrincipal adminUser)
        {
            var user = new ApplicationUser
            {
                FirstName = input.FirstName,
                LastName  = input.LastName,
                UserName  = input.FirstName,
                Email     = input.Email,
            };
            var admin = await this.userManager.GetUserAsync(adminUser);

            user.AdminId   = admin.Id;
            user.CompanyId = admin.CompanyId;
            var result = await this.userManager.CreateAsync(user, input.Password);

            if (!result.Succeeded)
            {
                return(result);
            }

            // var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user);
            // code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
            // var callbackUrl = this.Url.Page(
            //    "/Account/ConfirmEmail",
            //    pageHandler: null,
            //    values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
            //    protocol: this.Request.Scheme);

            // await this.emailSender.SendEmailAsync(this.Input.Email, "Confirm your email",
            //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
            await this.userManager.AddToRoleAsync(user, RoleNames.User.ToString());

            return(result);
        }
        public async Task AddUserAsync_ThrowsException_NullInput()
        {
            //arrange
            IEntityGateway     entityGateway = InMemoryEntityGatewayFactory.CreateEntityGateway();
            IAddUserPresenter  presenter     = new FakeAddUserPresenter(_output);
            IAddUserInteractor interactor    = new AddUserInteractor(presenter, entityGateway);
            AddUserInputModel  inputModel    = null;

            //act
            async Task <CommandResult <Application.UseCase.Out.AddUserOutputModel> > function() => await interactor.AddUserAsync(inputModel);

            //assert
            await Assert.ThrowsAsync <ArgumentNullException>(function);
        }
        public async Task AddUserAsync_FailedModelValidation_NullUsername()
        {
            //arrange
            IEntityGateway     entityGateway = InMemoryEntityGatewayFactory.CreateEntityGateway();
            IAddUserPresenter  presenter     = new FakeAddUserPresenter(_output);
            IAddUserInteractor interactor    = new AddUserInteractor(presenter, entityGateway);
            AddUserInputModel  inputModel    = new AddUserInputModel();

            //act
            var result = await interactor.AddUserAsync(inputModel);

            //assert
            Assert.NotNull(result);
            Assert.True(result.Status == Application.Common.CommandResultStatusCode.FailedModelValidation);
            Assert.True(result.ModelValidationErrors.ContainsKey(Domain.Common.ModelValidationStatusCode.RequiredInformationMissing));
        }
        //methods
        public async Task <CommandResult <AddUserOutputModel> > AddUserAsync(AddUserInputModel inputModel)
        {
            _inputModel = inputModel ?? throw new ArgumentNullException(nameof(inputModel));
            _userToAdd  = new User(inputModel.ToUserModel());

            if (!_userToAdd.IsValid)
            {
                return(PresentValidationErrors());
            }

            if (await IsUserAlreadyStoredAsync(_userToAdd))
            {
                return(PresentDuplicatedResult(new AddUserOutputModel(_userToAdd.GetModel())));
            }

            _userToAdd = await SaveToStorageAsync();

            return(PresentSuccessfulResult());
        }
        public async Task AddUserAsync_CommandResult_Success()
        {
            //arrange
            IEntityGateway     entityGateway = InMemoryEntityGatewayFactory.CreateEntityGateway();
            IAddUserPresenter  presenter     = new FakeAddUserPresenter(_output);
            IAddUserInteractor interactor    = new AddUserInteractor(presenter, entityGateway);
            AddUserInputModel  inputModel    = new AddUserInputModel {
                Username = "******"
            };

            //act
            var result = await interactor.AddUserAsync(inputModel);

            //assert
            Assert.NotNull(result);
            Assert.True(result.Status == Application.Common.CommandResultStatusCode.Success);
            Assert.Null(result.ModelValidationErrors);
            Assert.NotNull(result.OutputModel);
            Assert.True(result.OutputModel.Id != Guid.Empty);
        }
예제 #7
0
        public async Task <IActionResult> AddUser(AddUserInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Json(new { isValid = false, html = this.View(input) }));
            }

            var result = await this.usersService.AddUserAsync(input, this.User);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    this.ModelState.AddModelError(string.Empty, error.Description);
                }

                return(this.Json(new { isValid = false, html = this.View(input) }));
            }

            return(this.Json(new { isValid = true, redirectToUrl = this.Url.Action("Index", "Users") }));
        }
        public async Task AddUserAsyncExistingUserUsernameDifferentCase_CommandResult_DuplicateEntry()
        {
            //arrange
            string duplicatedUsername = "******";
            var    users = new User[] { new User {
                                            Username = duplicatedUsername
                                        } };
            IEntityGateway entityGateway = await InMemoryEntityGatewayFactory.CreateEntityGatewayAsync(users);

            IAddUserPresenter  presenter  = new FakeAddUserPresenter(_output);
            IAddUserInteractor interactor = new AddUserInteractor(presenter, entityGateway);
            AddUserInputModel  inputModel = new AddUserInputModel {
                Username = duplicatedUsername.ToUpperInvariant()
            };

            //act
            var result = await interactor.AddUserAsync(inputModel);

            //assert
            Assert.NotNull(result);
            Assert.True(result.Status == Application.Common.CommandResultStatusCode.DuplicateEntry);
        }