示例#1
0
        public ViewResult AddUser(RegularUser usr)  //add user in DB by applying validations with the help of uservalidation class.
        {
            if (ModelState.IsValid)
            {
                bool isExist         = UserValidations.isUserExist(usr.Username.ToLower());     //check for username already exist
                bool checkEmailExist = UserValidations.isEmailExist(usr.Email.ToLower());       //check for email already exist
                bool isValid         = UserValidations.isUsernameValid(usr.Username.ToLower()); //check for username validation
                if (!isValid)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Username: Only letters, digits, @, _ and . are allowed !");
                    return(View());
                }
                if (isExist)
                {
                    ModelState.AddModelError(string.Empty, "Username already exist !");
                    return(View());
                }
                if (checkEmailExist)
                {
                    ModelState.AddModelError(string.Empty, "Email already exist !");
                    return(View());
                }
                List <RegularUser> userData = UserRepository.ReturnUsers();
                if (usr.Password != usr.anotherPassword)    //password confirmation
                {
                    ModelState.AddModelError(string.Empty, "Password confirmation failed !");
                    return(View());
                }

                if (usr.profilePicture != null)                                                                    //upload profile picture if user add it in view.
                {
                    var    uploadeFolder = Path.Combine(Environment.CurrentDirectory, "wwwroot/Images");           //combines the resident path.
                    string sourcefile    = usr.Username + "-" + "profile_pic" + "-" + usr.profilePicture.FileName; //makes filename
                    usr.picAddress = Path.Combine("~/images/", sourcefile);                                        //combine both addresses
                    string destinationPath = Path.Combine(uploadeFolder, sourcefile);                              //combines both folder + filename
                    using (var filestream = new FileStream(destinationPath, FileMode.Create))
                    {
                        usr.profilePicture.CopyTo(filestream);  //saves picture with filestream object.
                    }
                }
                //add user credentials except password in lower format.
                usr.Email    = usr.Email.ToLower();
                usr.Username = usr.Username.ToLower();
                UserRepository.AddUser(usr);
                userData = UserRepository.ReturnUsers();
                List <RegularUser> newData = checkForAdmins(userData);
                return(View("AdminPanel", newData));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Some data is missing !");
                return(View());
            }
        }
        public IActionResult Login(RegularUser regUsr) //checks for the credentials and validations and allow user accordingly as
        {                                              //admin or normal user.
            List <RegularUser> userData = UserRepository.ReturnUsers();

            if (!string.IsNullOrEmpty(regUsr.Username) && !string.IsNullOrEmpty(regUsr.Password)) //self validtions instead of
            {                                                                                     //ModelState.IdValid.
                regUsr.Username = regUsr.Username.ToLower();
                bool isExist = UserValidations.isUserExist(regUsr.Username.ToLower());            //checks for user exist
                bool isValid = UserValidations.isUsernameValid(regUsr.Username.ToLower());        //username validation.
                if (!isValid)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Username: Only letters, digits, @, _ and . are allowed !");
                    return(View());
                }
                if (!isExist)
                {
                    ModelState.AddModelError(string.Empty, "Username does not exist !");
                    return(View());
                }
                foreach (RegularUser usr in userData)
                {
                    if (usr.Username == regUsr.Username && usr.Password == regUsr.Password) //if matches with any record in DB.
                    {                                                                       //below is the check for admin.
                        if (regUsr.Username[0] == 'a' && regUsr.Username[1] == 'd' && regUsr.Username[2] == 'm' && regUsr.Username[3] == 'i' && regUsr.Username[4] == 'n')
                        {
                            HttpContext.Session.SetString("CurrentAdmin", usr.Username); //make session for admin here.
                            List <RegularUser> newData = AdminController.checkForAdmins(userData);
                            return(RedirectToAction("AdminPanel", "Admin", newData));
                        }
                        else                                                            //if entered credentials are correct and of some normal user except admin.
                        {
                            HttpContext.Session.SetString("CurrentUser", usr.Username); //makes session for user.
                            List <Post> postData = PostRepository.ReturnPosts();
                            AdminController.manageProfilePic(ref postData);
                            postData.Reverse();
                            ViewBag.Id = usr.Id;
                            return(RedirectToAction("AtHome", "General", postData));
                        }
                    }
                }
                ModelState.AddModelError(string.Empty, "Login credentials do not matched !");
                return(View());
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Some data is missing !");
                return(View());
            }
        }
 public IActionResult Signup(RegularUser usr)   //simply add a new user by taking inputs and applying validations.
 {
     if (ModelState.IsValid)
     {
         List <RegularUser> userData = UserRepository.ReturnUsers();
         bool isExist         = UserValidations.isUserExist(usr.Username.ToLower());     //checks whether same username already exist?
         bool checkEmailExist = UserValidations.isEmailExist(usr.Email.ToLower());       //checks whther same email already exist?
         bool isValid         = UserValidations.isUsernameValid(usr.Username.ToLower()); //username validations.
         if (!isValid)
         {
             ModelState.AddModelError(string.Empty, "Invalid Username: Only letters, digits, @, _ and . are allowed !");
             return(View());
         }
         if (isExist)
         {
             ModelState.AddModelError(string.Empty, "Username already exist !");
             return(View());
         }
         if (checkEmailExist)
         {
             ModelState.AddModelError(string.Empty, "Email already exist !");
             return(View());
         }
         if (usr.Password != usr.anotherPassword)
         {
             ModelState.AddModelError(string.Empty, "Password confirmation failed !");
             return(View());
         }
         usr.Username = usr.Username.ToLower();
         usr.Email    = usr.Email.ToLower();
         UserRepository.AddUser(usr);
         return(View("Congrats", usr));
     }
     else
     {
         ModelState.AddModelError(string.Empty, "Some data is missing !");
         return(View());
     }
 }
        public ViewResult Profile(RegularUser usr)  //manage the profile and all the fields updated by user.
        {
            string             oldUsername = null;
            List <RegularUser> userData    = UserRepository.ReturnUsers();
            RegularUser        rus         = userData.Find(ru => ru.Username == HttpContext.Session.GetString("CurrentUser"));

            if (ModelState.IsValid)
            {                       //some validations for username and email.
                bool isExist         = UserValidations.checkUserExist(usr.Username.ToLower(), rus.Username);
                bool isValid         = UserValidations.isUsernameValid(usr.Username.ToLower());
                bool checkEmailExist = UserValidations.checkEmailExist(usr.Email.ToLower(), rus.Email);
                if (!isValid || isExist || checkEmailExist)  //to save default profile pic.
                {
                    ViewBag.Id = rus.Id;
                    if (string.IsNullOrEmpty(rus.picAddress))
                    {
                        rus.picAddress = "~/images/temp.jpg";
                    }
                }
                if (!isValid)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Username: Only letters, digits, @, _ and . are allowed !");
                    return(View("Profile", rus));
                }
                if (isExist)
                {
                    ModelState.AddModelError(string.Empty, "Username already exist !");
                    return(View("Profile", rus));
                }
                if (checkEmailExist)
                {
                    ModelState.AddModelError(string.Empty, "Email already exist !");
                    return(View("Profile", rus));
                }
                RegularUser ru = userData.Find(ru => ru.Id == usr.Id);      //password confirmation here.
                if (ru.Password == usr.Password)
                {
                    oldUsername = ru.Username;
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Incorrect old password !");
                    ViewBag.Id = ru.Id;
                    if (string.IsNullOrEmpty(rus.picAddress))
                    {
                        rus.picAddress = "~/images/temp.jpg";
                    }
                    return(View("Profile", rus));
                }

                if (!string.IsNullOrEmpty(ru.picAddress) && usr.profilePicture != null)                                    //in case of updated pic, old will be deleted.
                {
                    string[] listStr = ru.picAddress.Split("~/");                                                          //address will be splited as we need only second part
                    var      path    = Path.Combine(Environment.CurrentDirectory, "wwwroot", listStr[listStr.Length - 1]); //combines path
                    System.IO.File.Delete(path);
                }

                if (usr.profilePicture != null) //to upload profile picture same as in admin controller, add user action method.
                {
                    var    uploadeFolder = Path.Combine(Environment.CurrentDirectory, "wwwroot/Images");
                    string sourcefile    = HttpContext.Session.GetString("CurrentUser") + "-" + "profile_pic" + "-" + usr.profilePicture.FileName;
                    usr.picAddress = Path.Combine("~/images/", sourcefile);
                    string destinationPath = Path.Combine(uploadeFolder, sourcefile);
                    using (var filestream = new FileStream(destinationPath, FileMode.Create))
                    {
                        usr.profilePicture.CopyTo(filestream);
                    }
                }

                usr.Email    = usr.Email.ToLower();
                usr.Username = usr.Username.ToLower();
                UserRepository.UpdateUser(usr);
                HttpContext.Session.SetString("CurrentUser", usr.Username); //update session here for new username.
                List <Post> postData = PostRepository.ReturnPosts();
                foreach (Post p in postData)                                //change username on posts as well.
                {
                    if (p.Usr == oldUsername)
                    {
                        p.Usr = usr.Username;
                        PostRepository.UpdatePost(p);
                    }
                }
                postData = PostRepository.ReturnPosts();
                userData = UserRepository.ReturnUsers();
                AdminController.manageProfilePic(ref postData);
                ru         = userData.Find(ru => ru.Username == HttpContext.Session.GetString("CurrentUser"));
                ViewBag.Id = ru.Id;
                postData.Reverse();
                return(View("AtHome", postData));
            }
            else     //in case of invalid inputs. same way in the all other action methods and controllers with some changes.
            {
                ModelState.AddModelError(string.Empty, "Please enter correct data !");
                if (string.IsNullOrEmpty(rus.picAddress))
                {
                    rus.picAddress = "~/images/temp.jpg";
                }
                ViewBag.Id = rus.Id;
                return(View("Profile", rus));
            }
        }
示例#5
0
        public ViewResult UpdateUser(RegularUser usr)   //update user with much validations same as above adduser.
        {
            List <RegularUser> userData    = UserRepository.ReturnUsers();
            RegularUser        regUsr      = userData.Find(regUsr => regUsr.Id == usr.Id);
            string             oldUsername = regUsr.Username;
            string             oldEmail    = regUsr.Email; //below the self validations by me as ModelValidations are not applicable here.

            if (!string.IsNullOrEmpty(usr.Username) && !string.IsNullOrEmpty(usr.Email) && !string.IsNullOrEmpty(usr.anotherPassword))
            {
                bool isExist         = UserValidations.checkUserExist(usr.Username.ToLower(), oldUsername); //same validations for adduser,
                bool isValid         = UserValidations.isUsernameValid(usr.Username.ToLower());             //but old username and emails are
                bool checkEmailExist = UserValidations.checkEmailExist(usr.Email.ToLower(), oldEmail);      //sent along with new.
                if (!isValid || isExist || checkEmailExist)                                                 //to add default picture.
                {
                    if (string.IsNullOrEmpty(regUsr.picAddress))
                    {
                        regUsr.picAddress = "~/images/temp.jpg";
                    }
                }
                if (!isValid)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Username: Letters, digits, @, _ and . are allowed !");
                    return(View("UpdateUser", regUsr));
                }
                if (isExist)
                {
                    ModelState.AddModelError(string.Empty, "Username already exist !");
                    return(View("UpdateUser", regUsr));
                }
                if (checkEmailExist)
                {
                    ModelState.AddModelError(string.Empty, "Email already exist !");
                    return(View("UpdateUser", regUsr));
                }

                if (!string.IsNullOrEmpty(regUsr.picAddress) && usr.profilePicture != null) //removes previous picture if present
                {                                                                           //in case of new pic uploaded by user.
                    string[] listStr = regUsr.picAddress.Split("~/");
                    var      path    = Path.Combine(Environment.CurrentDirectory, "wwwroot", listStr[listStr.Length - 1]);
                    System.IO.File.Delete(path);
                }

                if (usr.profilePicture != null) //to upload profile picture same as in adduser.
                {
                    var    uploadeFolder = Path.Combine(Environment.CurrentDirectory, "wwwroot/Images");
                    string sourcefile    = usr.Username + "-" + "profile_pic" + "-" + usr.profilePicture.FileName;
                    usr.picAddress = Path.Combine("~/images/", sourcefile);
                    string destinationPath = Path.Combine(uploadeFolder, sourcefile);
                    using (var filestream = new FileStream(destinationPath, FileMode.Create))
                    {
                        usr.profilePicture.CopyTo(filestream);
                    }
                }

                usr.Email    = usr.Email.ToLower();
                usr.Username = usr.Username.ToLower();
                UserRepository.UpdateUser(usr);
                List <Post> postData = PostRepository.ReturnPosts();
                foreach (Post p in postData)        //update posts usernames.
                {
                    if (p.Usr == oldUsername)
                    {
                        p.Usr = usr.Username;
                        PostRepository.UpdatePost(p);
                    }
                }
                userData = UserRepository.ReturnUsers();
                List <RegularUser> newData = checkForAdmins(userData);
                return(View("AdminPanel", newData));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Some data is missing !");
                if (string.IsNullOrEmpty(regUsr.picAddress))
                {
                    regUsr.picAddress = "~/images/temp.jpg";
                }
                return(View("UpdateUser", regUsr));
            }
        }