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));
        }
Пример #2
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"));
        }