示例#1
0
        public ActionResult EditCompany(CompanyAccountVM model)
        {
            var user  = User as MPrincipal;
            var login = user.UserDetails.Login;

            ViewBag.UserName = user.UserDetails.Login;
            ViewBag.UserRole = 4;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var baseCompEmail = db.Companies.FirstOrDefault(x => x.Email == model.Email);
                var baseCustEmail = db.Customers.FirstOrDefault(x => x.Email == model.Email);

                var comp = db.Companies.FirstOrDefault(u => u.Login.Equals(login));
                if (comp != null)
                {
                    // loginu nie można zmienić, więc tutaj tylko email nie może się powtórzyć z takim jaki jużjest w bazie
                    if ((baseCompEmail == null && baseCustEmail == null) || model.Email == comp.Email)
                    {
                        comp.CompanyName = model.CompanyName ?? "";
                        comp.PhoneNumer  = model.PhoneNumber ?? "";
                        comp.Email       = model.Email ?? "";
                        db.SaveChanges();
                        return(RedirectToAction("EditCompany"));
                    }
                    else
                    {
                        ModelState.AddModelError("Email", "Użytkownik o podanym emailu już istnieje");
                    }
                }
            }
            return(RedirectToAction("EditCompany"));
        }
示例#2
0
        public ActionResult EditCompany()
        {
            ViewBag.CityList = CitiesQuery();
            var user  = User as MPrincipal;
            var login = user.UserDetails.Login;

            ViewBag.UserName = user.UserDetails.Login;
            ViewBag.UserRole = 4;


            CompanyAccountVM companyEditVM = null;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var comp = db.Companies.FirstOrDefault(u => u.Login.Equals(login));
                ViewBag.UserName          = comp.Login;
                ViewBag.Role              = comp.RoleId;
                companyEditVM             = new CompanyAccountVM();
                companyEditVM.CompanyName = comp.CompanyName;
                companyEditVM.PhoneNumber = comp.PhoneNumer;
                companyEditVM.Email       = comp.Email;
            }

            return(View("EditCompany", companyEditVM));
        }
示例#3
0
        public ActionResult RegisterCompany(CompanyAccountVM _model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    var compEmail = db.Companies.FirstOrDefault(x => x.Email == _model.Email);
                    var compLogin = db.Companies.FirstOrDefault(x => x.Login == _model.Login);
                    var custEmail = db.Customers.FirstOrDefault(x => x.Email == _model.Email);
                    var custLogin = db.Customers.FirstOrDefault(x => x.Login == _model.Login);
                    if ((compEmail == null) && (compLogin == null) && (custEmail == null) && (custLogin == null))
                    {
                        Company company = new Company();
                        company.CompanyName = _model.CompanyName;
                        company.Login       = _model.Login;
                        company.PhoneNumer  = _model.PhoneNumber;
                        company.Email       = _model.Email;
                        company.Password    = Security.sha512encrypt(_model.Password);
                        company.RoleId      = 4;
                        company.IsDelete    = false;
                        company.CityId      = 1;
                        db.Companies.Add(company);
                        db.SaveChanges();
                        return(RedirectToAction("LoginCompany"));
                    }
                    else if ((compEmail != null) || (custEmail != null))
                    {
                        ModelState.AddModelError("Email", "Użytkownik o podanym emailu już istnieje");
                    }
                    else if ((compLogin != null) || (custLogin != null))
                    {
                        ModelState.AddModelError("Login", "Użytkownik o podanym loginie już istnieje");
                    }
                }
            }

            return(View(_model));
        }
示例#4
0
        public ActionResult LoginCompany(CompanyAccountVM _model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                bool validEmail = db.Companies.Any(x => x.Email == _model.Email);
                bool validLogin = db.Companies.Any(x => x.Login == _model.Login);

                if (!(validEmail || validLogin))
                {
                    ModelState.AddModelError("Password", "Niepoprawny login lub hasło");
                    return(View(_model));
                }

                _model.Password = Security.sha512encrypt(_model.Password);
                ViewBag.Title   = "Logowanie klienta";

                Company company = db.Companies.FirstOrDefault(u => u.Login.Equals(_model.Login) && u.Password.Equals(_model.Password));

                string authId = Guid.NewGuid().ToString();

                Session["AuthID"] = authId;
                var cookie = new HttpCookie("AuthID");
                cookie.Value = authId;
                Response.Cookies.Add(cookie);

                if (company != null)
                {
                    FormsAuthentication.SetAuthCookie(company.Login, false);
                    var authTicket = new FormsAuthenticationTicket(1, company.Login, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(60), false, "");
                    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
                    authCookie.Expires = DateTime.UtcNow.AddMinutes(60);
                    Response.SetCookie(authCookie);
                    return(RedirectToAction("Index", "Home"));
                }
                return(View(_model));
            }
        }