public ActionResult Login(String login, String password)
        {
            bool          status   = false;
            List <string> messages = new List <string>();

            if (InputHelper.Empty(login))
            {
                messages.Add("Login field is empty.");
            }
            if (!InputHelper.Length(login, 1, 50))
            {
                messages.Add("Login at max 50 characters.");
            }
            if (InputHelper.Empty(password))
            {
                messages.Add("Password field is empty.");
            }
            if (!InputHelper.Length(password, 1, 50))
            {
                messages.Add("Password at max 50 characters.");
            }
            if (messages.Count == 0)
            {
                var obj = PMS.BAL.UserBO.ValidateUser(login, password);
                if (obj != null)
                {
                    Session["user"] = obj;
                    if (obj.IsAdmin)
                    {
                        return(Redirect("~/Home/Admin"));
                    }
                    else
                    {
                        return(Redirect("~/Home/NormalUser"));
                    }
                }
                messages.Add("Login/Password combination doesn't match.");
                ViewBag.Login = login;
            }
            ViewBag.Messages = messages;
            ViewBag.Status   = status;
            return(View());
        }
        public ActionResult Save(UserDTO userDTO)
        {
            List <string> messages = new List <string>();
            bool          status   = false;

            if (InputHelper.Empty(userDTO.Name))
            {
                messages.Add("Name field is empty.");
            }
            if (!InputHelper.Length(userDTO.Name, 1, 50))
            {
                messages.Add("Name at max 50 characters.");
            }
            if (InputHelper.Empty(userDTO.Login))
            {
                messages.Add("Login field is empty.");
            }
            if (!InputHelper.Length(userDTO.Login, 1, 50))
            {
                messages.Add("Login at max 50 characters.");
            }
            if (!InputHelper.Length(userDTO.Password, 1, 50))
            {
                messages.Add("Password at max 50 characters.");
            }
            userDTO.IsActive = true;
            if (messages.Count == 0)
            {
                string uniqueName = "";
                if (Request.Files["Image"] != null)
                {
                    var file = Request.Files["Image"];
                    if (file.FileName != "")
                    {
                        string ext = System.IO.Path.GetExtension(file.FileName);
                        uniqueName = Guid.NewGuid().ToString() + ext;
                        string rootPath     = Server.MapPath("~/userpics");
                        string fileSavePath = System.IO.Path.Combine(rootPath, uniqueName);
                        file.SaveAs(fileSavePath);
                        userDTO.PictureName = uniqueName;
                        int id = PMS.BAL.UserBO.Save(userDTO);
                        if (id > 0)
                        {
                            status = true;
                            messages.Add("Registration completed.");
                            userDTO = new UserDTO();
                        }
                        else
                        {
                            messages.Add("Registeration failed.");
                        }
                    }
                }
                else
                {
                    messages.Add("Choose an image.");
                }
            }

            ViewBag.Status   = status;
            ViewBag.Messages = messages;

            return(View("Register", userDTO));
        }
        public ActionResult ForgotPassword(string login, string email)
        {
            bool          status   = false;
            List <string> messages = new List <string>();

            if (InputHelper.Empty(login))
            {
                messages.Add("Login field is empty.");
            }
            if (!InputHelper.Length(login, 1, 50))
            {
                messages.Add("Login at max 50 characters.");
            }
            if (InputHelper.Empty(email))
            {
                messages.Add("Email field is empty.");
            }
            if (!InputHelper.MatchPattern(email, @"^[\w.%+\-]+@[\w.\-]+\.[A-Za-z]{2,6}$"))
            {
                messages.Add("Invalid email format.");
            }
            if (messages.Count == 0)
            {
                if (PMS.BAL.UserBO.ValidateLogin(login) != null)
                {
                    try
                    {
                        MailMessage mail = new MailMessage();
                        MailAddress to   = new MailAddress(email);
                        mail.To.Add(to);
                        MailAddress from = new MailAddress("*****@*****.**", "Sher Ali");
                        mail.From    = from;
                        mail.Subject = "Reset Code";
                        string resetCode = Guid.NewGuid().ToString();
                        Session["Login"]     = login;
                        Session["ResetCode"] = resetCode;
                        mail.Body            = "Reset Code: " + resetCode;
                        var sc = new SmtpClient("smtp.gmail.com", 587)
                        {
                            Credentials = new System.Net.NetworkCredential("ead.csf15", "EAD_csf15m"),
                            EnableSsl   = true
                        };
                        sc.Send(mail);
                        return(RedirectToAction("ResetCode"));
                    }
                    catch (Exception ex)
                    {
                        messages.Add("Unable to send reset code.");
                    }
                }
                else
                {
                    messages.Add("Invalid login.");
                }
            }
            ViewBag.Login    = login;
            ViewBag.Email    = email;
            ViewBag.Status   = status;
            ViewBag.Messages = messages;
            return(View());
        }
        public ActionResult Edit2(UserDTO userDTO)
        {
            if (SessionManager.IsValidUser)
            {
                bool          status   = false;
                List <string> messages = new List <string>();
                if (InputHelper.Empty(userDTO.Name))
                {
                    messages.Add("Name field is empty.");
                }
                if (!InputHelper.Length(userDTO.Name, 1, 50))
                {
                    messages.Add("Name at max 50 characters long.");
                }
                if (InputHelper.Empty(userDTO.Login))
                {
                    messages.Add("Login field is empty.");
                }
                if (!InputHelper.Length(userDTO.Login, 1, 50))
                {
                    messages.Add("Login at max 50 characters long.");
                }

                userDTO.IsActive = true;

                if (messages.Count == 0)
                {
                    string uniqueName = "";

                    if (Request.Files["Image"] != null)
                    {
                        var file = Request.Files["Image"];
                        if (file.FileName != "")
                        {
                            string ext = System.IO.Path.GetExtension(file.FileName);
                            uniqueName = Guid.NewGuid().ToString() + ext;
                            string rootPath = Server.MapPath("~/userpics");

                            if (userDTO.PictureName != null)
                            {
                                System.IO.File.Delete(System.IO.Path.Combine(rootPath, userDTO.PictureName));
                            }

                            string fileSavePath = System.IO.Path.Combine(rootPath, uniqueName);
                            file.SaveAs(fileSavePath);

                            userDTO.PictureName = uniqueName;
                        }
                    }
                }

                int id = PMS.BAL.UserBO.Save(userDTO);
                if (id > 0)
                {
                    status = true;
                    messages.Add("Profile has been updated.");
                }
                else
                {
                    messages.Add("Profile was not updated.");
                }

                ViewBag.Status   = status;
                ViewBag.Messages = messages;
                return(View("Edit", userDTO));
            }
            else
            {
                return(Redirect("~/User/Login"));
            }
        }
        public ActionResult EditUser(UserDTO userDTO)
        {
            if (Session["Id"] == null)
            {
                return(RedirectToAction("Login"));
            }

            userDTO.IsCricket = Request["IsCricket"] == "on";
            userDTO.Hockey    = Request["Hockey"] == "on";
            userDTO.Chess     = Request["Chess"] == "on";

            bool          status   = false;
            List <string> messages = new List <string>();

            if (UserBAL.DuplicateLoginWithId(userDTO))
            {
                messages.Add("Login already exist.");
            }
            if (InputHelper.Empty(userDTO.Name))
            {
                messages.Add("Name field is empty.");
            }
            if (!InputHelper.Length(userDTO.Name, 1, 50))
            {
                messages.Add("Name at max 50 characters long.");
            }
            if (InputHelper.Empty(userDTO.Login))
            {
                messages.Add("Login field is empty.");
            }
            if (!InputHelper.Length(userDTO.Login, 1, 50))
            {
                messages.Add("Login at max 50 characters long.");
            }
            if (!InputHelper.MatchPattern(userDTO.Password, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                messages.Add("Password must be 8-15 characters including letters, numbers, special characters.");
            }
            if (!InputHelper.MatchPattern(userDTO.Email, @"^[\w.%+\-]+@[\w.\-]+\.[A-Za-z]{2,6}$"))
            {
                messages.Add("Invalid email format.");
            }
            if (!InputHelper.Gender(userDTO.Gender))
            {
                messages.Add("Invalid gender.");
            }
            if (!InputHelper.Length(userDTO.Address, 1, 45))
            {
                messages.Add("Address at max 50 characters long.");
            }
            if (!InputHelper.Age(userDTO.Age))
            {
                messages.Add("Age is invalid.");
            }
            if (!InputHelper.MatchPattern(userDTO.NIC, @"^\d{5}-\d{7}-\d{1}$"))
            {
                messages.Add("NIC is invalid.");
            }
            if (messages.Count == 0)
            {
                string uniqueName = "";

                if (Request.Files["Image"] != null)
                {
                    var file = Request.Files["Image"];
                    if (file.FileName != "")
                    {
                        string ext = System.IO.Path.GetExtension(file.FileName);
                        uniqueName = Guid.NewGuid().ToString() + ext;
                        string rootPath = Server.MapPath("~/Images");

                        System.IO.File.Delete(System.IO.Path.Combine(rootPath, userDTO.ImageName));

                        string fileSavePath = System.IO.Path.Combine(rootPath, uniqueName);
                        file.SaveAs(fileSavePath);

                        userDTO.ImageName = uniqueName;
                    }
                }

                if (UserBAL.UpdateUser(userDTO))
                {
                    status = true;
                    messages.Add("User with ID: " + userDTO.UserID + " has been updated.");
                }
                else
                {
                    messages.Add("User was not updated.");
                }
            }

            ViewBag.Status   = status;
            ViewBag.Messages = messages;

            return(View(userDTO));
        }
Esempio n. 6
0
        public ActionResult CreateUser()
        {
            ViewBag.Name     = Request["name"];
            ViewBag.Login    = Request["login"];
            ViewBag.Password = Request["password"];
            ViewBag.Email    = Request["email"];
            ViewBag.Gender   = Request["gender"];
            ViewBag.Address  = Request["address"];
            ViewBag.Age      = Request["age"];
            ViewBag.NIC      = Request["nic"];
            ViewBag.DOB      = Request["dob"];
            ViewBag.Cricket  = Request["cricket"];
            ViewBag.Hockey   = Request["hockey"];
            ViewBag.Chess    = Request["chess"];

            UserDTO userDTO = new UserDTO();

            userDTO.Name      = Request["name"];
            userDTO.Login     = Request["login"];
            userDTO.Password  = Request["password"];
            userDTO.Email     = Request["email"];
            userDTO.Gender    = Convert.ToChar(Request["gender"]);
            userDTO.Address   = Request["address"];
            userDTO.Age       = Convert.ToInt32(Request["age"]);
            userDTO.NIC       = Request["nic"];
            userDTO.DOB       = Convert.ToDateTime(Request["dob"]);
            userDTO.IsCricket = (Request["cricket"] == "on") ? true : false;
            userDTO.Hockey    = (Request["hockey"] == "on") ? true : false;
            userDTO.Chess     = (Request["chess"] == "on") ? true : false;
            userDTO.CreatedOn = DateTime.Now;

            bool          status   = false;
            List <string> messages = new List <string>();

            if (UserBAL.DuplicateLogin(userDTO))
            {
                messages.Add("Login already exist.");
            }
            if (InputHelper.Empty(userDTO.Name))
            {
                messages.Add("Name field is empty.");
            }
            if (!InputHelper.Length(userDTO.Name, 1, 50))
            {
                messages.Add("Name at max 50 characters long.");
            }
            if (InputHelper.Empty(userDTO.Login))
            {
                messages.Add("Login field is empty.");
            }
            if (!InputHelper.Length(userDTO.Login, 1, 50))
            {
                messages.Add("Login at max 50 characters long.");
            }
            if (!InputHelper.MatchPattern(userDTO.Password, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                messages.Add("Password must be 8-15 characters including letters, numbers, special characters.");
            }
            if (!InputHelper.MatchPattern(userDTO.Email, @"^[\w.%+\-]+@[\w.\-]+\.[A-Za-z]{2,6}$"))
            {
                messages.Add("Invalid email format.");
            }
            if (!InputHelper.Gender(userDTO.Gender))
            {
                messages.Add("Invalid gender.");
            }
            if (!InputHelper.Length(userDTO.Address, 1, 45))
            {
                messages.Add("Address at max 50 characters long.");
            }
            if (!InputHelper.Age(userDTO.Age))
            {
                messages.Add("Age is invalid.");
            }
            if (!InputHelper.MatchPattern(userDTO.NIC, @"^\d{5}-\d{7}-\d{1}$"))
            {
                messages.Add("NIC is invalid.");
            }
            if (messages.Count == 0)
            {
                string uniqueName = "";

                if (Request.Files["image"] != null)
                {
                    var file = Request.Files["image"];
                    if (file.FileName != "")
                    {
                        string ext = System.IO.Path.GetExtension(file.FileName);
                        uniqueName = Guid.NewGuid().ToString() + ext;
                        string rootPath     = Server.MapPath("~/Images");
                        string fileSavePath = System.IO.Path.Combine(rootPath, uniqueName);
                        file.SaveAs(fileSavePath);

                        userDTO.ImageName = uniqueName;

                        int id = UserBAL.CreateUser(userDTO);
                        if (id > 0)
                        {
                            Session["Login"] = userDTO.Login;
                            return(RedirectToAction("Home"));
                        }
                        else
                        {
                            messages.Add("User was not added.");
                        }
                    }
                }
                else
                {
                    messages.Add("Choose an image.");
                }
            }

            ViewBag.Status   = status;
            ViewBag.Messages = messages;

            return(View("NewUser"));
        }