// Lägger till användarkonto till en borrower
        public ActionResult AddUser(user u)
        {
            if (new Auth((BorrowerWithUser)Session["User"]).HasAdminPermission())
            {
                BorrowerWithBorrows b = BorrowerService.GetBorrowerWithBorrows(u.PersonId);

                if (ModelState.IsValid)
                {
                    if (PasswordValidaton.IsValid(u.Password))
                    {
                        if (!UserService.EmailExists(u.Email))
                        {
                            AuthService.CreateUser(u);
                            TempData["Alert"] = AlertView.Build("Du har skapat ett användarkonto till låntagaren.", AlertType.Success);
                            return RedirectToAction("Borrower", new { id = u.PersonId });
                        }

                        TempData["Alert"] = AlertView.Build("Konto med emailen " + u.Email + " existerar. Ange en annan!", AlertType.Danger);

                        return View("Borrower", b);
                    }

                    TempData["Alert"] = AlertView.Build(PasswordValidaton.ErrorMessage, AlertType.Danger);

                    return RedirectToAction("Borrower", new { id = u.PersonId });
                }


                TempData["Alert"] = AlertView.BuildErrors(ViewData);

                return RedirectToAction("Borrower", new { id = u.PersonId });
            }

            return Redirect("/Error/Code/403");
        }
Example #2
0
 public static void CreateUser(user u) {
     u.Password = PasswordService.CreateHash(u.Password);
     UserRepository.CreateUser(u);
 }
        public ActionResult GetAcountInfo(user user, borrower borrower, string newpassword = null)
        {
            //Knyter samman user och borrower -objekten
            BorrowerWithUser borrowerWithUser = new BorrowerWithUser()
            {
                User = user,
                Borrower = borrower
            };

            Auth _auth = new Auth((BorrowerWithUser)Session["User"]);

            if (_auth.HasUserPermission())
            {
                if (ModelState.IsValid)
                {
                    if (user.Password != null && PasswordService.VerifyPassword(user.Password, _auth.LoggedInUser.User.Password))
                    {
                        if (UserService.EmailExists(user.Email) && _auth.LoggedInUser.User.Email != user.Email)
                        {
                            borrowerWithUser.PushAlert(AlertView.Build("Email existerar. Försök igen!", AlertType.Danger));
                            return View(borrowerWithUser);
                        }

                        if (!_auth.IsSameAs(borrowerWithUser, newpassword))
                        {
                            if (newpassword == "")
                            {
                                UserService.Update(borrowerWithUser, user.Password);
                            }
                            else
                            {
                                if (!PasswordValidaton.IsValid(newpassword))
                                {
                                    borrowerWithUser.PushAlert(AlertView.Build(PasswordValidaton.ErrorMessage, AlertType.Danger));
                                    return View(borrowerWithUser);
                                }

                                UserService.Update(borrowerWithUser, newpassword);

                            }

                            borrowerWithUser.PushAlert(AlertView.Build("Du har uppdaterat ditt konto.", AlertType.Success));
                            Session["User"] = BorrowerService.GetBorrowerWithUserByPersonId(user.PersonId);

                            return View(borrowerWithUser);
                        }
                        else
                        {
                            borrowerWithUser.PushAlert(AlertView.Build("Inget har uppdaterats.", AlertType.Info));
                            return View(borrowerWithUser);
                        }
                    }

                    borrowerWithUser.PushAlert(AlertView.Build("Du måste ange ditt eget lösenord.", AlertType.Danger));
                    return View(borrowerWithUser);
                }

                return View(borrowerWithUser);
            }
            return Redirect("/Error/Code/403");               
        }