Example #1
0
        /// <summary>
        /// Remove a given user from the repository
        /// </summary>
        /// <param name="model"></param>
        public static void Delete(AccountViewModel model)
        {
            var account = Mapper.Map<Account>(model);

            if (!Account.Delete(account))
                throw new DataAccessException("Kunde inte ta bort användare");
        }
Example #2
0
        /// <summary>
        /// Tries to get an account entity model to map to a viewmodel from the repository
        /// Based on a given viewmodel
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static AccountViewModel Login(AccountViewModel model)
        {
           Account account;      

           if (!Account.GetAccount(out account, model.Username, model.Password))
               throw new DoesNotExistException("Fel lösenord / användarnamn ");

           return Mapper.Map<AccountViewModel>(account);
        }
Example #3
0
        // Creates a new account
        public static void Upsert(AccountViewModel model)
        {
            var account = Mapper.Map<Account>(model);
            // Update Password if changed
            if (model.NewPassword != null)
                account.Password = model.NewPassword;

            if (!Account.Upsert(account))
                throw new DataAccessException("Kunde inte skapa användare!");
        }
Example #4
0
        public ActionResult Login(AccountViewModel model)
        {
            AccountViewModel m;
            string errorMessage = null;
            try
            {
                m = AccountServices.Login(model);
            }
            catch(DoesNotExistException e)
            {
                errorMessage = e.Message;
                m = null;
            }

            if(AccountHelper.GetRetriesLeft(Session) > 0 &&  m != null)
            {
                // Setups the session indexes
                AccountHelper.SetupUserSession(Session, m.Username, m.RoleId);

                if(AccountHelper.HasAccess(Session, AccountHelper.Role.Admin))
                        return RedirectToAction("AdminPage");
                else if(AccountHelper.HasAccess(Session, AccountHelper.Role.User))
                        return RedirectToAction("UserPage");
            }
            else if(AccountHelper.CanRetryLogin(Session))
            {
                
                errorMessage += AccountHelper.GetRetriesLeft(Session).ToString() + " försök kvar\n";
            }
            else
            {
                errorMessage = "Du har överskridit max antal inloggningsförsök\n";
                errorMessage += "Vänligen vänta tills " + AccountHelper.GetDelay(Session).ToShortTimeString() + "\n";
            }


            ViewBag.loginError = errorMessage;
            return View();
        }
Example #5
0
        public ActionResult ChangePassword(AccountViewModel viewModel)
        {
            bool succesfull = false;
            try
            {
                if (ModelState.IsValid)
                {
                    AccountServices.Login(viewModel); //Throws error if login failed
                    AccountServices.Upsert(viewModel);
                    succesfull = true;
                }
            }
            catch (DataAccessException)
            {
                ViewBag.error = "Kunde inte byta lösenord";
            }
            catch (DoesNotExistException)
            {
                ViewBag.error = "Kunde inte byta lösenord";
            }

            if(succesfull)
                return RedirectToAction("UserPage", "Account");
            else
                return View(viewModel);
        }
Example #6
0
 public ActionResult ChangePassword()
 {
     AccountViewModel viewModel = new AccountViewModel();
     viewModel.Username = AccountHelper.GetUserName(this.Session);
     return View(viewModel);
 }
Example #7
0
        public ActionResult Account(AccountViewModel model)
        {
            if (AccountHelper.GetUserName(Session) == model.Username)
                ViewBag.currentUser = true;
            
                try
                {
                    if (model.NewPassword != null &&
                        model.Username != null &&
                        ModelState.IsValid &&
                        !(AccountServices.AccountExists(model.Username) && model.New))
                    {
                        AccountServices.Upsert(model);
                        model.New = false;
                    }
                    else if((AccountServices.AccountExists(model.Username) && model.New))
                    {
                        ViewBag.error = "Kan inte lägga lägga till existerande användare";
                    }
                }
                catch (DataAccessException e)
                {
                    ViewBag.error = e.Message;
                }


            getAccountList();
            return View(model);
        }
Example #8
0
        public ActionResult Account(string username)
        {
            getAccountList();
            AccountViewModel viewModel = new AccountViewModel() { Username = username, New = true };

            if(username != null)
            {
                if(AccountHelper.GetUserName(Session) == username)
                    ViewBag.currentUser = true;

                try
                {
                    if (AccountServices.AccountExists(username))
                    {
                        viewModel.New = false;
                    }
                }
                catch (DataAccessException e)
                {
                        ViewBag.error = e.Message;
                }
                
            }
            return View(viewModel);
        }
Example #9
0
 public BorrowerViewModel()
 {
     Borrows = new List<BorrowViewModel>();
     Account = new AccountViewModel();
 }