コード例 #1
0
        public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                // Don't reveal that the user does not exist
                return(RedirectToAction(nameof(ResetPasswordConfirmation)));
            }
            var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);

            if (result.Succeeded && DomainUserExists(null, model.Email))
            {
                // update domain model User
                var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
                var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);
                unitOfWork.BeginTransaction();
                var domainUser = userRepository.GetUserByEmail(model.Email);
                domainUser.SetPassword(model.Password);
                unitOfWork.Commit();

                return(RedirectToAction(nameof(ResetPasswordConfirmation)));
            }
            AddErrors(result);
            return(View());
        }
コード例 #2
0
        public void Login(string username, string password)
        {
            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            try
            {
                unitOfWork.BeginTransaction();

                var user = userRepository.GetUserByUsername(username);
                if (user.IsSamePassword(password))
                {
                    LoggedInUserId  = user.Id;
                    _isUserLoggedIn = true;
                    unitOfWork.Commit();

                    ShowMainWindow();
                }
                else
                {
                    throw new Exception("Username or password is not correct!");
                }
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();

                _isUserLoggedIn = false;
                ShowLoginWindow();
                MessageBox.Show(ex.Message, "TravelBuddy");
            }
        }
コード例 #3
0
        public async Task <IActionResult> Create(AddTravelViewModel viewModel)
        {
            var travel = viewModel.ToTravel();
            var user   = await GetCurrentDomainUserAsync();

            var unitOfWork         = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository     = RepositoriesFactory.CreateUserRepository(unitOfWork);
            var travelRepository   = RepositoriesFactory.CreateTravelRepository(unitOfWork);
            var currencyRepository = RepositoriesFactory.CreateCurrencyRepository(unitOfWork);

            unitOfWork.BeginTransaction();
            travel.Traveller = userRepository.GetUser(user.Id);
            var currency = currencyRepository.GetById(viewModel.BudgetCurrencyId);

            if (viewModel.BudgetValue > 0 && currency != null)
            {
                travel.Budget = new MoneyValue
                {
                    Value    = viewModel.BudgetValue,
                    Currency = currency
                };
            }
            travelRepository.AddTravel(travel);
            unitOfWork.Commit();

            return(RedirectToAction("Index"));
        }
コード例 #4
0
        public void OpenTravelListWindow()
        {
            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            var travelController = new TravelController(this, _formsFactory);

            try
            {
                unitOfWork.BeginTransaction();

                var user    = userRepository.GetUser(LoggedInUserId);
                var travels = new List <Travel>(user.Travels);

                unitOfWork.Commit();

                var travelListView = _formsFactory.CreateTravelListView(travelController, travels);
                travelListView.ShowModaless();
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();

                ShowRegisterWindow();
                MessageBox.Show(ex.Message, "TravelBuddy");
            }
        }
コード例 #5
0
        public void Register(string username, string email, string password)
        {
            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            try
            {
                unitOfWork.BeginTransaction();

                var user = new User
                {
                    Username = username,
                    Email    = email,
                };

                user.SetPassword(password);

                userRepository.AddUser(user);
                LoggedInUserId  = user.Id;
                _isUserLoggedIn = true;

                unitOfWork.Commit();

                ShowMainWindow();
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();

                _isUserLoggedIn = false;
                ShowRegisterWindow();
                MessageBox.Show(ex.Message, "TravelBuddy");
            }
        }
コード例 #6
0
        public void AddTravel(AddTravelViewModel model)
        {
            var unitOfWork       = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository   = RepositoriesFactory.CreateUserRepository(unitOfWork);
            var travelRepository = RepositoriesFactory.CreateTravelRepository(unitOfWork);

            try
            {
                unitOfWork.BeginTransaction();

                var user = userRepository.GetUser(_mainController.LoggedInUserId);

                var travel = new Travel
                {
                    Name        = model.Name,
                    Description = model.Description,
                    DateStart   = model.DateStart,
                    DateEnd     = model.DateEnd,
                    Budget      = model.Budget,
                    Traveller   = user
                };

                travelRepository.AddTravel(travel);

                unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();

                MessageBox.Show(ex.Message, "TravelBuddy");
            }
        }
コード例 #7
0
        private bool DomainUserExists(string username, string email)
        {
            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            unitOfWork.BeginTransaction();
            var exists = userRepository.DoesUserExist(email: email, username: username);

            unitOfWork.Commit();
            _logger.LogInformation($"Checking if domain user exists... {exists}");
            return(exists);
        }
コード例 #8
0
        private async Task <User> GetCurrentDomainUserAsync()
        {
            var appUser = await GetCurrentApplicationUserAsync();

            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            unitOfWork.BeginTransaction();
            var domainUser = userRepository.GetUserByEmail(appUser.Email);

            unitOfWork.Commit();
            return(domainUser);
        }
コード例 #9
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // create a domain model User
                    var domainUser     = new User(model.Username, model.Email, model.Password);
                    var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
                    var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);
                    unitOfWork.BeginTransaction();
                    try
                    {
                        userRepository.AddUser(domainUser);
                        unitOfWork.Commit();
                    }
                    catch (DuplicateUserException ex)
                    {
                        unitOfWork.Rollback();
                        await _userManager.DeleteAsync(user);

                        ModelState.AddModelError(string.Empty, ex.Message);
                        return(View(model));
                    }

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
コード例 #10
0
        public async Task <IActionResult> Index()
        {
            var user = await GetCurrentDomainUserAsync();

            var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
            var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);

            unitOfWork.BeginTransaction();
            user = userRepository.GetUser(user.Id);
            var travels = user.Travels.ToList().Select(t => new IndexTravelViewModel(t));

            unitOfWork.Commit();

            return(View(travels));
        }
コード例 #11
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid && DomainUserExists(model.Username, model.Email))
            {
                // check domain model user
                var unitOfWork     = UnitOfWorkFactory.CreateUnitOfWork();
                var userRepository = RepositoriesFactory.CreateUserRepository(unitOfWork);
                unitOfWork.BeginTransaction();
                var domainUser      = userRepository.GetUserByEmail(model.Email) ?? userRepository.GetUserByUsername(model.Username);
                var correctPassword = domainUser.IsSamePassword(model.Password);
                unitOfWork.Commit();
                if (!correctPassword)
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    _logger.LogInformation($"Incorrect password by {domainUser.Username}");
                    return(View(model));
                }

                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(domainUser.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToAction(nameof(Lockout)));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }