public LoginPageViewModel()
        {
            UserData = new LoginModel();

            SaveLoginDataCommand = new Command(async() =>
            {
                if (!UserValidations.IsnotEmpty(UserData.UserName))
                {
                    Result = "El nombre de usuario es requerido";
                }
                else if (!UserValidations.IsnotEmpty(UserData.Password))
                {
                    Result = "La contraseƱa es requerida.";
                }
                else
                {
                    await Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new ContactListPage()
                    {
                        BackgroundColor = Color.CadetBlue
                    }));
                }
            });

            ToRegistePage = new Command(async() =>
            {
                await Application.Current.MainPage.Navigation.PushAsync(new RegisterPage());
            });
        }
Example #2
0
 async Task RegisterValidations(RegisterModel register)
 {
     if (!UserValidations.IsnotEmpty(register.UserName))
     {
         Result = ErrorCodes.UserNameRequired;
     }
     else if (!UserValidations.IsnotEmpty(register.Password))
     {
         Result = ErrorCodes.PasswordRequired;
     }
     else if (!UserValidations.IsEqual(register.Password, ConfirmPassword))
     {
         Result = ErrorCodes.PassNoMatch;
     }
     else if (!UserValidations.IsnotEmpty(register.Email))
     {
         Result = ErrorCodes.UserEmailRequired;
     }
     else if (!UserValidations.NumberIsNotEmpty(register.Number))
     {
         Result = ErrorCodes.TelNumberRequired;
     }
     else
     {
         await ToHomePage();
     }
 }
        public RegisterPageViewModel()
        {
            RegisterData = new RegisterModel();

            SaveRegisterDataCommand = new Command(async() =>
            {
                if (!UserValidations.IsnotEmpty(RegisterData.UserName))
                {
                    Result = "El nombre de usuario es requerido";
                }
                else if (!UserValidations.IsnotEmpty(RegisterData.Email))
                {
                    Result = "El Email de usuario es requerido";
                }
                else if (!UserValidations.IsnotEmpty(RegisterData.Password) || !UserValidations.IsnotEmpty(RegisterData.ConfirmPassword))
                {
                    Result = "Las contraseƱa es requerida ";
                }
                else if (!UserValidations.IsEqual(RegisterData.Password, RegisterData.ConfirmPassword))
                {
                    Result = "Las contraseƱas no coinciden";
                }
                else
                {
                    await Application.Current.MainPage.Navigation.PushModalAsync(new HomePage());
                }
            });
        }
Example #4
0
        public object POST(AddUser Request)
        {
            var              user   = Request.NewUser;
            UserValidations  obj    = new UserValidations();
            ValidationResult result = obj.Validate(user);

            if (result.IsValid)
            {
                try
                {
                    var newUser = _userBll.RegisterUser(user);
                    if (newUser == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(newUser);
                    }
                }
                catch (Exception e)
                {
                    // _logger.Error("Exception Thrown", e);
                    throw e;
                }
            }
            else
            {
                return("Error");
            }
        }
Example #5
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());
            }
        }
Example #6
0
        static public List <RegularUser> checkForAdmins(List <RegularUser> userData)
        {
            List <RegularUser> newData = new List <RegularUser>();

            foreach (RegularUser u in userData)         //checks for admins.
            {
                bool isAdmin = UserValidations.isAdmin(u.Username);
                if (!isAdmin)
                {
                    newData.Add(u);
                }
            }
            return(newData);
        }
Example #7
0
        public ActionResult Edit([Bind(Include = "Id,Name,Username,Password,Email,RepeatPassword,Biography,IsPrivate")] User user, HttpPostedFileBase profilePictureFile)
        {
            if (!UserValidations.ValidateEmail(user.Email))
            {
                ViewBag.Error     = "Your email is invalid. Please enter a valid one.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateUsername(user.Username))
            {
                ViewBag.Error     = "Your username is invalid. It should be between 3 and 50 characters long.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidatePassword(user.Password))
            {
                ViewBag.Error     = "Your password is invalid. It should be at least 8 characters long, contain at least one small and one big letter and one digit.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateRepeatedPassword(user.Password, user.RepeatPassword))
            {
                ViewBag.Error     = "Your passwords do not match.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateProfilePicture(profilePictureFile))
            {
                ViewBag.Error     = "You have not chosen a profile picture.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (ModelState.IsValid)
            {
                user.RegisterProfilePicture = PictureUtilities.PictureToByteArray(profilePictureFile);
                repo.Update(user);
                return(RedirectToAction("Details/" + AuthManager.GetAuthenticated().Id, "Users"));
            }
            return(View(user));
        }
        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());
            }
        }
 async Task LoginValidations(LoginModel login)
 {
     if (!UserValidations.IsnotEmpty(login.UserName))
     {
         Result = ErrorCodes.UserNameRequired;
     }
     else if (!UserValidations.IsnotEmpty(login.Password))
     {
         Result = ErrorCodes.PasswordRequired;
     }
     else
     {
         await ToHomePage();
     }
 }
Example #10
0
        public ContactPageViewModel()
        {
            contact             = new ContactModel();
            SaveContactsCommand = new Command(async() =>
            {
                if (UserValidations.NumberIsNotEmpty(contact.CelNumber))
                {
                    MessagingCenter.Send <ContactPageViewModel, ContactModel>(this, "SendContact", contact);

                    await Application.Current.MainPage.Navigation.PopModalAsync();
                }
                else
                {
                    Result = "El Numero telefonico es requerido.";
                }
            });
        }
Example #11
0
 public string GetUserId(string username, string password)
 {
     try
     {
         var validater = new UserValidations();
         validater.ValidateAndThrow(new User()
         {
             Username = username, Password = password
         }, ruleSet: RuleSets.User.crentials);
         return(base.Context.Set <User>().Where(u => u.Username == username & u.Password == password).Select(u => u.Id).FirstOrDefault());
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw e;
     }
 }
 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));
            }
        }
 public UserService(IUnitOfWork unitOfWork)
 {
     this._unitOfWork = unitOfWork;
     this.userValid   = new UserValidations();
 }
Example #15
0
 public UserController(IUserLogic userLogic, UserValidations userValidations)
 {
     _userLogic       = userLogic;
     _userValidations = userValidations;
 }
 public AuthenticationService(IUserRepository userRepository, UserValidations validations, IConfiguration configuration)
 {
     _userRepository = userRepository;
     _validations    = validations;
     _configuration  = configuration;
 }
Example #17
0
 public LoginService(IUnitOfWork _unitOfWork)
 {
     this._unitOfWork = _unitOfWork;
     this.userValid   = new UserValidations();
 }
Example #18
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));
            }
        }
Example #19
0
        public ActionResult Create([Bind(Include = "Id,Name,Username,Password,Email,RepeatPassword,Biography,IsPrivate")] User user, HttpPostedFileBase profilePictureFile)
        {
            if (!UserValidations.ValidateEmail(user.Email))
            {
                ViewBag.Error     = "Your email is invalid. Please enter a valid one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (UserUtilities.IsEmailTaken(user.Email, db))
            {
                ViewBag.Error     = "This email is already taken. Please register with another one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateUsername(user.Username))
            {
                ViewBag.Error     = "Your username is invalid. It should be between 3 and 50 characters long.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (UserUtilities.IsUserExisting(user.Username, db))
            {
                ViewBag.Error     = "This username is already taken. Please register with another one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidatePassword(user.Password))
            {
                ViewBag.Error     = "Your password is invalid. It should be at least 8 characters long, contain at least one small and one big letter and one digit.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateRepeatedPassword(user.Password, user.RepeatPassword))
            {
                ViewBag.Error     = "Your passwords do not match.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateProfilePicture(profilePictureFile))
            {
                ViewBag.Error     = "You have not chosen a profile picture.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (ModelState.IsValid)
            {
                user.RegisterProfilePicture = PictureUtilities.PictureToByteArray(profilePictureFile);
                db.Users.Add(user);
                db.SaveChanges();
                AuthManager.SetCurrentUser(user.Username, user.Password);
                return(RedirectToAction("Details/" + AuthManager.GetAuthenticated().Id, "Users"));
            }
            return(View(user));
        }