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

Wersja 871, 4.4 KB (wprowadzona przez alina, 16 years temu)

re #215

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