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

Wersja 950, 4.1 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 DataContext());
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            if (language != "pl" && language != "en" && language != "de")
40                return RedirectToAction("LogOn", "Account", new { language = "pl" });
41
42            _funkcjePlatnosci.SetLanguage(language);
43            return View();
44        }
45
46        [AcceptVerbs(HttpVerbs.Post)]
47        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
48            Justification = "Needs to take same parameter type as Controller.Redirect()")]
49        public ActionResult LogOn(string nip, string numerFaktury, string returnUrl, string language)
50        {
51            _funkcjePlatnosci.SetLanguage(language);
52            var platnosc = new vPlatnosciEcard();
53            if (!ValidateLogOn(nip, numerFaktury))
54            {
55                return View();
56            }
57           
58            var login = _repository.FindInvoiceByNipNumber(nip, numerFaktury).SingleOrDefault();
59            platnosc = login;
60            if (platnosc == null) return View();
61            _funkcjePlatnosci.SetUserLogger(nip, numerFaktury);
62
63            FormsAuth.SignIn(nip, false);
64
65            if (!String.IsNullOrEmpty(returnUrl))
66            {
67                return Redirect(returnUrl);
68            }
69           
70            return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury });
71        }
72
73        public ActionResult LogOff()
74        {
75            FormsAuth.SignOut();
76            return RedirectToAction("LogOn", "Account");
77        }
78
79        private bool ValidateLogOn(string nip, string numerFaktury)
80        {
81            var errNip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
82            var errFaktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
83            var errTxt = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
84   
85            if (String.IsNullOrEmpty(nip))
86                ModelState.AddModelError("nip", errNip);
87            if (String.IsNullOrEmpty(numerFaktury))
88                ModelState.AddModelError("numerFaktury", errFaktura);
89           
90            if (ModelState.IsValid)
91            {
92                var daneOk = _repository.Exists(u => u.Faktura_Numer == numerFaktury && u.nip == nip);
93                if (daneOk == false)
94                    ModelState.AddModelError("_FORM", errTxt);
95            }
96            return ModelState.IsValid;
97        }
98    }
99    public interface IFormsAuthentication
100    {
101        void SignIn(string nip, bool createPersistentCookie);
102        void SignOut();
103    }
104
105    public class FormsAuthenticationService : IFormsAuthentication
106    {
107        public void SignIn(string nip, bool createPersistentCookie)
108        {
109            FormsAuthentication.SetAuthCookie(nip, createPersistentCookie);
110        }
111        public void SignOut()
112        {
113            FormsAuthentication.SignOut();
114        }
115    }
116}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.