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));
            }
        }
Ejemplo n.º 2
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));
            }
        }