Beispiel #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 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());
     }
 }