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

Wersja 970, 4.1 KB (wprowadzona przez marek, 16 years temu)

re #215 - dodanie nowego projektu z lepsza nazwa i typem projektu

Line 
1using System;
2using System.Linq;
3using System.Web.Mvc;
4using System.Web.Security;
5using adMoto.Payments.Core;
6using adMoto.Payments.Core.Data;
7using adMoto.Payments.Core.Interfaces;
8using Platnosci.Models;
9
10namespace Platnosci.Controllers
11{
12    [HandleError]
13    public class AccountController : Controller
14    {
15        private readonly IRepository<Invoice> _repository;
16        private readonly FunkcjePlatnosci _funkcjePlatnosci;
17
18        public IFormsAuthentication FormsAuth
19        {
20            get;
21            private set;
22        }
23
24        public AccountController()
25        {
26            _repository = new Repository<Invoice>(new DataContext());
27            FormsAuth = new FormsAuthenticationService();
28            _funkcjePlatnosci = new FunkcjePlatnosci();
29        }
30
31        public AccountController(IFormsAuthentication formsAuth, IRepository<Invoice> repository, FunkcjePlatnosci func)
32        {
33            _repository = repository;
34            FormsAuth = formsAuth ?? new FormsAuthenticationService();
35            _funkcjePlatnosci = func;
36        }
37
38        public ActionResult LogOn(string language)
39        {
40            if (language != "pl" && language != "en" && language != "de")
41                return RedirectToAction("LogOn", "Account", new { language = "pl" });
42
43            _funkcjePlatnosci.SetLanguage(language);
44            return View();
45        }
46
47        [AcceptVerbs(HttpVerbs.Post)]
48        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
49            Justification = "Needs to take same parameter type as Controller.Redirect()")]
50        public ActionResult LogOn(string nip, string numerFaktury, string returnUrl, string language)
51        {
52            _funkcjePlatnosci.SetLanguage(language);
53            var platnosc = new Invoice();
54            if (!ValidateLogOn(nip, numerFaktury))
55            {
56                return View();
57            }
58           
59            var login = _repository.FindInvoiceByNipNumber(nip, numerFaktury).SingleOrDefault();
60            platnosc = login;
61            if (platnosc == null) return View();
62            _funkcjePlatnosci.SetUserLogger(nip, numerFaktury);
63
64            FormsAuth.SignIn(nip, false);
65
66            if (!String.IsNullOrEmpty(returnUrl))
67            {
68                return Redirect(returnUrl);
69            }
70           
71            return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury });
72        }
73
74        public ActionResult LogOff()
75        {
76            FormsAuth.SignOut();
77            return RedirectToAction("LogOn", "Account");
78        }
79
80        private bool ValidateLogOn(string nip, string numerFaktury)
81        {
82            var errNip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
83            var errFaktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
84            var errTxt = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
85   
86            if (String.IsNullOrEmpty(nip))
87                ModelState.AddModelError("nip", errNip);
88            if (String.IsNullOrEmpty(numerFaktury))
89                ModelState.AddModelError("numerFaktury", errFaktura);
90           
91            if (ModelState.IsValid)
92            {
93                var daneOk = _repository.Exists(u => u.Faktura_Numer == numerFaktury && u.nip == nip);
94                if (daneOk == false)
95                    ModelState.AddModelError("_FORM", errTxt);
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.