예제 #1
0
        public async Task <IHttpActionResult> CreateUser([FromBody] AccountCreationModel createUserModel)
        {
            var            user          = createUserModel.CreationToDomain();
            IdentityResult addUserResult = await this._userManager.CreateAsync(user, createUserModel.Password);

            if (!addUserResult.Succeeded)
            {
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addRoleResult = await this._userManager.AddToRolesAsync(user.Id, "User");

            if (!addRoleResult.Succeeded)
            {
                return(GetErrorResult(addRoleResult));
            }

            string code = await this._userManager.GenerateEmailConfirmationTokenAsync(user.Id);

            //var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            /*await this._userManager.SendEmailAsync(user.Id,
             *                                      "Confirm your account",
             *                                      "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");*/

            return(Created("", user.DomainToView()));
        }
예제 #2
0
        public async Task <AccountModel> CreateAccount(AccountCreationModel accountCreationModel)
        {
            if (accountCreationModel == null)
            {
                _logger.LogError($"Creating an account failed - no data was passed");
                throw new BadRequestException($"No data for account creation was passed");
            }
            var userId = accountCreationModel.UserId;

            _logger.LogInformation($"Creating an account for user {userId}");
            var user = await _usersProvider.GetUser(userId);

            if (user == null)
            {
                _logger.LogError($"Creating an account for user {userId} failed - no user found");
                throw new NotFoundException($"No user {userId} has been found. Not possible to create the account");
            }
            var accountExists = await IsAccountCreated(userId);

            if (accountExists)
            {
                _logger.LogError($"Account for the user {userId} is already created");
                throw new BadRequestException($"Account for the user {userId} already exists");
            }
            var account = await CreateNewAccount(accountCreationModel, user);

            _logger.LogInformation($"The account for user {userId} has been created");
            return(_mapper.Map <AccountModel>(account));
        }
예제 #3
0
        public async Task <ActionResult <AccountModel> > Post([FromBody] AccountCreationModel accountCreationModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var result = await _accountsProvider.CreateAccount(accountCreationModel);

            return(Ok(result));
        }
예제 #4
0
 public static ApplicationUser CreationToDomain(this AccountCreationModel @this)
 {
     return(new ApplicationUser
     {
         UserName = @this.Username,
         Email = @this.Email,
         FirstName = @this.FirstName,
         LastName = @this.LastName,
         JoinDate = DateTime.Now.Date,
     });
 }
예제 #5
0
        public ActionResult Registration([Bind(Include = "Login, Password")] AccountCreationModel model,
                                         string returnUrl)
        {
            if (ModelState.IsValid && !accountLogic.AccountExist(model.Login))
            {
                var account = new Account(model.Login, model.Password)
                {
                    Role = Role.User,
                };
                accountLogic.Create(account);

                return(RedirectToAction("Login", routeValues: returnUrl));
            }
            TempData["Error message"] = "Such login exist";
            return(View(model));
        }
예제 #6
0
        private async Task <Account> CreateNewAccount(AccountCreationModel accountCreationModel, UserModel user)
        {
            var account = new Account
            {
                FirstName    = accountCreationModel.FirstName,
                SecondName   = accountCreationModel.SecondName,
                CreationDate = DateTime.UtcNow,
                UserId       = user.Id
            };

            _dbContext.Accounts.Add(account);
            await _dbContext.SaveChangesAsync();

            var result = await _dbContext.Accounts.Include(a => a.User).FirstOrDefaultAsync(x => x.Id == account.Id);

            return(result);
        }
예제 #7
0
 public ActionResult Login([Bind(Include = "Login, Password")] AccountCreationModel model,
                           string returnUrl)
 {
     if (ModelState.IsValid && accountLogic.AccountExist(model.Login))
     {
         var account = accountLogic.Get(model.Login);
         if ((account.Login, account.Password) == (model.Login, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.Login, true);
             if (string.IsNullOrWhiteSpace(returnUrl))
             {
                 return(Redirect("~"));
             }
             return(Redirect(returnUrl));
         }
     }
     TempData["Error message"] = "Uncorrect login or password";
     return(View(model));
 }
예제 #8
0
        public async Task CreateAccount_UserDoesntExist()
        {
            //Arrange
            var context           = _fixture.ZipPayDbContext;
            var mockUsersProvider = new Mock <IUsersProvider>();
            var accountsProvider  = new AccountsProvider(context, _logger.Object, _mapper, mockUsersProvider.Object);

            //Act
            var accountCreationModel = new AccountCreationModel
            {
                UserId     = 1000,
                FirstName  = "Test",
                SecondName = "TestSecondName"
            };

            //Assert
            await Assert.ThrowsAsync <NotFoundException>(() =>
                                                         accountsProvider.CreateAccount(accountCreationModel));
        }
예제 #9
0
        public async Task CreateAccount_AccountCreated()
        {
            //Arrange
            var context          = _fixture.ZipPayDbContext;
            var userProvider     = new UsersProvider(context, new Mock <ILogger <UsersProvider> >().Object, _mapper);
            var accountsProvider = new AccountsProvider(context, _logger.Object, _mapper, userProvider);

            //Act
            var accountCreationModel = new AccountCreationModel
            {
                UserId     = 8,
                FirstName  = "Test",
                SecondName = "TestSecondName"
            };
            var result = await accountsProvider.CreateAccount(accountCreationModel);

            //Assert
            Assert.Equal("TestSecondName", result.SecondName);
        }
예제 #10
0
 public AccountCreationViewModel()
 {
     NewAccount      = new AccountCreationModel();
     ValidateCommand = new ButtonCommand(this.ValidateAccount, true);
     CreateCommand   = new ButtonCommand(this.CreateAccount, true);
 }