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

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