root/trunk/eCard/eCardMVC/adMoto.Payments.Web/Controllers/AccountController.cs @ 984

Wersja 984, 4.1 KB (wprowadzona przez alina, 16 years temu)

re #215 ujednolicenie nazw obiektow, modyfikacje dotyczace testow interfejsowych (dodanie tymczasowych rekowrdow do bazy na czas testowania )

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 adMoto.Payments.Web.Models;
9
10namespace adMoto.Payments.Web.Controllers
11{
12    [HandleError]
13    public class AccountController : Controller
14    {
15        private readonly IRepository<Invoice> _repository;
16        private readonly PaymentsUtils _paymentsUtils;
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            _paymentsUtils = new PaymentsUtils();
29        }
30
31        public AccountController(IFormsAuthentication formsAuth, IRepository<Invoice> repository, PaymentsUtils paymentsUtils)
32        {
33            _repository = repository;
34            FormsAuth = formsAuth ?? new FormsAuthenticationService();
35            _paymentsUtils = paymentsUtils;
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            _paymentsUtils.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            _paymentsUtils.SetLanguage(language);
53            var invoice = new Invoice();
54           
55            if (!ValidateLogOn(nip, numerFaktury))
56                return View();           
57           
58            invoice = _repository.FindInvoiceByNipNumber(nip, numerFaktury).SingleOrDefault();
59           
60            if (invoice == null)
61                return View();
62
63            _paymentsUtils.SetUserLogger(nip, numerFaktury);
64
65            FormsAuth.SignIn(nip, false);
66
67            if (!String.IsNullOrEmpty(returnUrl))
68                return Redirect(returnUrl);
69           
70            return RedirectToAction("Show", "Platnosc", new { id = invoice.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           
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
101    public interface IFormsAuthentication
102    {
103        void SignIn(string nip, bool createPersistentCookie);
104        void SignOut();
105    }
106
107    public class FormsAuthenticationService : IFormsAuthentication
108    {
109        public void SignIn(string nip, bool createPersistentCookie)
110        {
111            FormsAuthentication.SetAuthCookie(nip, createPersistentCookie);
112        }
113        public void SignOut()
114        {
115            FormsAuthentication.SignOut();
116        }
117    }
118}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.