| 1 | using System;
|
|---|
| 2 | using System.Web.Mvc;
|
|---|
| 3 | using Wierszowki.Core.Interfaces;
|
|---|
| 4 | using Wierszowki.Core.Linq;
|
|---|
| 5 | using Wierszowki.Models;
|
|---|
| 6 | using Wierszowki.Models.Interfaces;
|
|---|
| 7 |
|
|---|
| 8 | namespace Wierszowki.Controllers
|
|---|
| 9 | {
|
|---|
| 10 | public class AccountController : Controller
|
|---|
| 11 | {
|
|---|
| 12 | private readonly IRepository<User> _repository;
|
|---|
| 13 |
|
|---|
| 14 | public IFormsAuthentication FormsAuth
|
|---|
| 15 | {
|
|---|
| 16 | get;
|
|---|
| 17 | private set;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | public AccountController()
|
|---|
| 21 | {
|
|---|
| 22 | _repository = new LinqRepository<User>();
|
|---|
| 23 | FormsAuth = new FormsAuthenticationService();
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public AccountController(IFormsAuthentication formsAuth, IRepository<User> repository)
|
|---|
| 27 | {
|
|---|
| 28 | _repository = repository;
|
|---|
| 29 | FormsAuth = formsAuth ?? new FormsAuthenticationService();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public ActionResult Login()
|
|---|
| 33 | {
|
|---|
| 34 | return View();
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 38 | public ActionResult Login(string login, string password, bool rememberMe, string returnUrl)
|
|---|
| 39 | {
|
|---|
| 40 | if (!ValidateLogOn(login, password))
|
|---|
| 41 | return View();
|
|---|
| 42 |
|
|---|
| 43 | FormsAuth.SignIn(login, rememberMe);
|
|---|
| 44 |
|
|---|
| 45 | if (!String.IsNullOrEmpty(returnUrl))
|
|---|
| 46 | return Redirect(returnUrl);
|
|---|
| 47 |
|
|---|
| 48 | return RedirectToAction("Index", "Home");
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public ActionResult LogOff()
|
|---|
| 52 | {
|
|---|
| 53 | FormsAuth.SignOut();
|
|---|
| 54 | return RedirectToAction("Index", "Home");
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | private bool ValidateLogOn(string login, string password)
|
|---|
| 58 | {
|
|---|
| 59 | if (string.IsNullOrEmpty(login))
|
|---|
| 60 | ModelState.AddModelError("username", "Proszê podaæ login.");
|
|---|
| 61 | if (string.IsNullOrEmpty(password))
|
|---|
| 62 | ModelState.AddModelError("password", "Proszê podaæ has³o.");
|
|---|
| 63 |
|
|---|
| 64 | if (ModelState.IsValid)
|
|---|
| 65 | {
|
|---|
| 66 | if (!_repository.Exists(u => u.Login == login && u.Password == password))
|
|---|
| 67 | ModelState.AddModelError("_FORM", "Podano nieprawid³owy login lub has³o.");
|
|---|
| 68 | }
|
|---|
| 69 | return ModelState.IsValid;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | } |
|---|