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

Wersja 866, 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>();
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 = _context.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               return RedirectToAction("Show", "Platnosc", new { id = platnosc.ID_faktury});
80            }
81        }
82        public ActionResult LogOff()
83        {
84
85            FormsAuth.SignOut();
86            return RedirectToAction("LogOn", "Account");
87        }
88
89        private bool ValidateLogOn(string nip, string numer_faktury)
90        {
91            if (String.IsNullOrEmpty(nip))
92            {
93                string err_nip = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_nip").ToString();
94                ModelState.AddModelError("nip", err_nip);
95            }
96            if (String.IsNullOrEmpty(numer_faktury))
97            {
98                string err_faktura = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_faktura").ToString();
99                ModelState.AddModelError("numer_faktury", err_faktura);
100            }
101            if (ModelState.IsValid)
102            {
103                bool DaneOk = _repository.Exists(u => u.Faktura_Numer == numer_faktury && u.nip == nip);
104                if (DaneOk == false)
105                {
106                    string err_logowanie = HttpContext.GetGlobalResourceObject("tlumaczenia", "err_logowanie").ToString();
107                    ModelState.AddModelError("_FORM", err_logowanie);
108                }               
109            }
110            return ModelState.IsValid;
111        }
112    }
113    public interface IFormsAuthentication
114    {
115        void SignIn(string nip, bool createPersistentCookie);
116        void SignOut();
117    }
118
119    public class FormsAuthenticationService : IFormsAuthentication
120    {
121        public void SignIn(string nip, bool createPersistentCookie)
122        {
123            FormsAuthentication.SetAuthCookie(nip, createPersistentCookie);
124        }
125        public void SignOut()
126        {
127            FormsAuthentication.SignOut();
128        }
129    }
130}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.