root/trunk/eCard/eCardMVC/Platnosci/Controllers/AccountController.cs @ 933

Wersja 932, 4.0 KB (wprowadzona przez marek, 16 years temu)

re #215 - drobny refactoring

Line 
1using System;
2using System.Linq;
3using System.Web.Mvc;
4using System.Web.Security;
5using Platnosci.Core.Linq;
6using Platnosci.Models;
7using Platnosci.Core.Interface;
8
9namespace Platnosci.Controllers
10{
11    [HandleError]
12    public class AccountController : Controller
13    {
14        private readonly IRepository<vPlatnosciEcard> _repository;
15        private readonly FunkcjePlatnosci _funkcjePlatnosci;
16
17        public IFormsAuthentication FormsAuth
18        {
19            get;
20            private set;
21        }
22
23        public AccountController()
24        {
25            _repository = new Repository<vPlatnosciEcard>(new DataContext1());
26            FormsAuth = new FormsAuthenticationService();
27            _funkcjePlatnosci = new FunkcjePlatnosci();
28        }
29
30        public AccountController(IFormsAuthentication formsAuth, IRepository<vPlatnosciEcard> repository, FunkcjePlatnosci func)
31        {
32            _repository = repository;
33            FormsAuth = formsAuth ?? new FormsAuthenticationService();
34            _funkcjePlatnosci = func;
35        }
36
37        public ActionResult LogOn(string language)
38        {
39            _funkcjePlatnosci.setLanguage(language);
40            return View();
41        }
42
43        [AcceptVerbs(HttpVerbs.Post)]
44        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
45            Justification = "Needs to take same parameter type as Controller.Redirect()")]
46        public ActionResult LogOn(string nip, string numerFaktury, string returnUrl, string language)
47        {
48            _funkcjePlatnosci.setLanguage(language);
49            var platnosc = new vPlatnosciEcard();
50            if (!ValidateLogOn(nip, numerFaktury))
51            {
52                return View();
53            }
54           
55            var login = _repository.FindInvoiceByNipNumber(nip, numerFaktury).SingleOrDefault();
56            platnosc = login;
57            if (platnosc == null) return View();
58            _funkcjePlatnosci.SetUserLogger(nip, numerFaktury);
59
60            FormsAuth.SignIn(nip, false);
61
62            if (!String.IsNullOrEmpty(returnUrl))
63            {
64                return Redirect(returnUrl);
65            }
66           
67            return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury });
68        }
69
70        public ActionResult LogOff()
71        {
72            FormsAuth.SignOut();
73            return RedirectToAction("LogOn", "Account");
74        }
75
76        private bool ValidateLogOn(string nip, string numerFaktury)
77        {
78            if (String.IsNullOrEmpty(nip))
79            {
80                var errNip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
81                ModelState.AddModelError("nip", errNip);
82            }
83            if (String.IsNullOrEmpty(numerFaktury))
84            {
85                var errFaktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
86                ModelState.AddModelError("numerFaktury", errFaktura);
87            }
88            if (ModelState.IsValid)
89            {
90                var daneOk = _repository.Exists(u => u.Faktura_Numer == numerFaktury && u.nip == nip);
91                if (daneOk == false)
92                {
93                    var errLogowanie = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
94                    ModelState.AddModelError("_FORM", errLogowanie);
95                }
96            }
97            return ModelState.IsValid;
98        }
99    }
100    public interface IFormsAuthentication
101    {
102        void SignIn(string nip, bool createPersistentCookie);
103        void SignOut();
104    }
105
106    public class FormsAuthenticationService : IFormsAuthentication
107    {
108        public void SignIn(string nip, bool createPersistentCookie)
109        {
110            FormsAuthentication.SetAuthCookie(nip, createPersistentCookie);
111        }
112        public void SignOut()
113        {
114            FormsAuthentication.SignOut();
115        }
116    }
117}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.