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