// 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"));
        }
        // Tar bort en borrower och konto om det finns
        public ActionResult Remove(BorrowerWithBorrows bwb)
        {
            if (new Auth((BorrowerWithUser)Session["User"]).HasAdminPermission())
            {
                if (!BorrowerService.RemoveBorrower(bwb.BorrowerWithUser.Borrower))
                {
                    TempData["Alert"] = AlertView.Build("Det gick inte att ta bort låntagare. Kontrollera att inga aktiva lån finns.", AlertType.Danger);

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

                TempData["Alert"] = AlertView.Build("Låntagare med PersonId " + bwb.BorrowerWithUser.Borrower.PersonId + " är nu borttagen", AlertType.Success);

                return(Redirect("Start"));
            }
            return(Redirect("/Error/Code/403"));
        }
Exemplo n.º 3
0
        private static BorrowerWithBorrows mapBorrowerWithBorrows(borrower b)
        {
            BorrowerWithBorrows borrowerwithborrows = new BorrowerWithBorrows();

            borrowerwithborrows.BorrowerWithUser = new BorrowerWithUser();

            borrowerwithborrows.BorrowerWithUser.Borrower = b;
            borrowerwithborrows.Borrows               = new ActiveAndHistoryBorrows();
            borrowerwithborrows.Borrows.Active        = BorrowService.GetActiveBorrowedBooks(b.PersonId);
            borrowerwithborrows.Borrows.History       = BorrowService.GetHistoryBorrowedBooks(b.PersonId);
            borrowerwithborrows.Categories            = CategoryService.GetCategories();
            borrowerwithborrows.BorrowerWithUser.User = UserRepository.GetUserByPersonId(b.PersonId);

            if (borrowerwithborrows.BorrowerWithUser.User == null)
            {
                borrowerwithborrows.BorrowerWithUser.User = new user();
            }

            borrowerwithborrows.Roles = RoleRepository.GetRoles();
            return(borrowerwithborrows);
        }