Beispiel #1
0
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < 5 || model.Username.Length > 20)
            {
                errors.Add($"Username '{model.Username}' is not valid. It must be between 5 and 20 characters long.");
            }

            if (!Regex.IsMatch(model.Email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
            {
                errors.Add($"Email {model.Email} is not a valid e-mail address.");
            }

            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                errors.Add("The provided password is not valid. It must be between 6 and 20 characters long.");
            }

            if (model.Password.Any(x => x == ' '))
            {
                errors.Add($"The provided password cannot contain whitespaces.");
            }

            if (model.Password != model.ConfirmPassword)
            {
                errors.Add($"Password and its confirmation are different.");
            }

            return(errors);
        }
Beispiel #2
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var errorsModel = this.validator.ValidateUser(model);

            if (this.context.Users.Any(u => u.Username == model.Username))
            {
                errorsModel.Add("This user is already registered.");
            }
            if (errorsModel.Any())
            {
                return(Error(errorsModel));
            }

            var hashedPassword = this.hasher.ComputeSha256Hash(model.Password);


            var user = new User
            {
                Username   = model.Username,
                Password   = hashedPassword,
                Email      = model.Email,
                IsMechanic = model.UserType == UserTypeMechanic
            };

            this.context.Users.Add(user);

            this.context.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #3
0
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < UserMinUsername || model.Username.Length > DefaultMaxLength)
            {
                errors.Add($"Username '{model.Username}' is not valid. It must be between {UserMinUsername} and {DefaultMaxLength} characters long.");
            }

            if (!Regex.IsMatch(model.Email, UserEmailRegularExpression))
            {
                errors.Add($"Email {model.Email} is not a valid e-mail address.");
            }

            if (model.Password.Length < UserMinPassword || model.Password.Length > UserMaxPassword)
            {
                errors.Add($"The provided password is not valid. It must be between {UserMinPassword} and {DefaultMaxLength} characters long.");
            }

            if (model.Password != model.ConfirmPassword)
            {
                errors.Add($"Password and its confirmation are different.");
            }

            return(errors);
        }
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUserRegistration(model);

            if (this.data.Users.Any(u => u.Username == model.Username))
            {
                modelErrors.Add($"Username '{model.Username}' already exists.");
            }

            if (this.data.Users.Any(u => u.Email == model.Email))
            {
                modelErrors.Add($"Email '{model.Email}' already in use.");
            }

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            var user = new User
            {
                Username   = model.Username,
                Password   = this.passwordHasher.HashPassword(model.Password),
                Email      = model.Email,
                IsMechanic = model.UserType == "Mechanic"
            };

            this.data.Users.Add(user);

            this.data.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #5
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUser(model);

            if (this.context.Users.Any(u => u.UserName == model.Username))
            {
                modelErrors.Add("This user is already registered.");
            }

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }
            var user = new User
            {
                UserName = model.Username,
                Password = this.hasher.ComputeSha256Hash(model.Password),
                Email    = model.Email
            };

            this.context.Users.Add(user);

            this.context.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #6
0
        public ICollection <string> ValidateUser(RegisterUserFormModel user)
        {
            var errors = new List <string>();

            if (user.Username == null || user.Username.Length < UserMinUsername || user.Username.Length > DefaultMaxLength)
            {
                errors.Add($"Username '{user.Username}' is not valid. It must be between {UserMinUsername} and {DefaultMaxLength} characters long.");
            }

            if (user.Email == null || !Regex.IsMatch(user.Email, UserEmailRegularExpression))
            {
                errors.Add($"Email '{user.Email}' is not a valid e-mail address.");
            }

            if (user.Password == null || user.Password.Length < UserMinPassword || user.Password.Length > DefaultMaxLength)
            {
                errors.Add($"The provided password is not valid. It must be between {UserMinPassword} and {DefaultMaxLength} characters long.");
            }

            if (user.Password != null && user.Password.Any(x => x == ' '))
            {
                errors.Add($"The provided password cannot contain whitespaces.");
            }

            if (user.Password != user.ConfirmPassword)
            {
                errors.Add("Password and its confirmation are different.");
            }

            return(errors);
        }
Beispiel #7
0
        public async Task <IActionResult> RegisterUser(RegisterUserFormModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new RegularUser
                {
                    UserName         = model.Username,
                    Email            = model.Email,
                    FirstName        = model.FirstName,
                    LastName         = model.LastName,
                    Gender           = model.Gender,
                    RegistrationDate = DateTime.UtcNow.ToLocalTime()
                };

                var result = await this.userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    this.logger.LogInformation("User created a new account with password.");

                    var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user);

                    await this.signInManager.SignInAsync(user, isPersistent : false);

                    this.logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            return(View(model));
        }
Beispiel #8
0
        public ICollection <string> RegisterUser(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUser(model);

            if (this.dbcontext.Users.Any(u => u.Username == model.Username))
            {
                modelErrors.Add($"User with '{model.Username}' username already exists.");
            }

            if (this.dbcontext.Users.Any(u => u.Email == model.Email))
            {
                modelErrors.Add($"User with '{model.Email}' e-mail already exists.");
            }

            if (modelErrors.Any())
            {
                return(modelErrors);
            }

            var user = new User
            {
                Username = model.Username,
                Password = this.passwordHasher.HashPassword(model.Password),
                Email    = model.Email,
            };

            dbcontext.Users.Add(user);

            dbcontext.SaveChanges();

            return(modelErrors);
        }
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < 5 || model.Username.Length > 20)
            {
                errors.Add($"Username '{model.Username}' is not valid. It must be between {5} and {20} characters long.");
            }

            if (!Regex.IsMatch(model.Email, UserEmailRegularExpression))
            {
                errors.Add($"Email {model.Email} is not a valid e-mail address.");
            }

            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                errors.Add($"The provided password is not valid. It must be between {6} and {20} characters long.");
            }

            if (model.Password.Any(x => x == ' '))
            {
                errors.Add($"The provided password cannot contain whitespaces.");
            }

            if (model.Password != model.ConfirmPassword)
            {
                errors.Add($"Password and its confirmation are different.");
            }

            return(errors);
        }
Beispiel #10
0
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < UserNameMinLength || model.Username.Length > UserNameMaxLength)
            {
                errors.Add($"Username have to be between {UserNameMinLength} and {UserNameMaxLength} charachters long.");
            }
            if (!Regex.IsMatch(model.Email, UserEmailRegex))
            {
                errors.Add("Invalid email.");
            }
            if (model.Password.Length < UserPasswordMinLength || model.Password.Length > UserPasswordMaxLength)
            {
                errors.Add($"Password have to be between {UserPasswordMinLength} and {UserPasswordMaxLength} charachters long.");
            }
            if (model.Password != model.ConfirmPassword)
            {
                errors.Add("Password and Confirm password are diffrent.");
            }
            if (string.IsNullOrWhiteSpace(model.Username) || string.IsNullOrWhiteSpace(model.Password))
            {
                errors.Add("Username or password is empty.");
            }

            return(errors);
        }
Beispiel #11
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelPass  = this.hasher.ComputeSha256Hash(model.Password);
            var errosModel = this.validator.ValidateUser(model);

            if (this.context.Users.Any(u => u.Username == model.Username &&
                                       u.Password == modelPass))
            {
                errosModel.Add("This user is already in db.");
            }
            if (errosModel.Any())
            {
                return(Error(errosModel));
            }

            var user = new User
            {
                Username = model.Username,
                Email    = model.Email,
                Password = modelPass
            };

            this.context.Users.Add(user);

            this.context.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #12
0
        public ICollection <string> ValidateUser(RegisterUserFormModel user)
        {
            var errors = new List <string>();

            if (user.Username == null || user.Username.Length < UsernameMinLength || user.Username.Length > DefaultMaxLength)
            {
                errors.Add($"Username '{user.Username}' must be between {UsernameMinLength} and {DefaultMaxLength} characters long");
            }

            if (user.Email == null || !Regex.IsMatch(user.Email, EmailRegularExpression))
            {
                errors.Add($"Email '{user.Email}' is not valid");
            }

            if (user.Password == null || user.Password.Length < PasswordMinLength || user.Password.Length > DefaultMaxLength)
            {
                errors.Add($"Password must be between {PasswordMinLength} and {DefaultMaxLength} characters long");
            }

            if (user.Password != user.ConfirmPassword)
            {
                errors.Add("Password and its confirmation are different.");
            }

            return(errors);
        }
Beispiel #13
0
        public ICollection <string> ValidateUserRegistration(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < 4 || model.Username.Length > 20)
            {
                errors.Add($"Username '{model.Username}' must be between 4 and 20 characters long.");
            }

            if (model.Password.Length < 5 || model.Password.Length > 20)
            {
                errors.Add($"Password must be between 5 and 20 characters long.");
            }

            if (model.Password != model.ConfirmPassword)
            {
                errors.Add($"Password does not match.");
            }

            if (model.UserType != "Mechanic" && model.UserType != "Client")
            {
                errors.Add("User must be either a Client or a Mechanic.");
            }

            return(errors);
        }
Beispiel #14
0
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors            = new List <string>();
            var regexPatternEmail = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            if (model.Username.Length < 5 || model.Username.Length > 20)
            {
                errors.Add("Username length should be between 5 and 20 symbols.");
            }

            if (!Regex.IsMatch(model.Email, regexPatternEmail))
            {
                errors.Add($"Email {model.Email} is not a valid e-mail address.");
            }

            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                errors.Add("Password length should be between 6 and 20 symbols.");
            }

            if (!model.ConfirmPassword.Equals(model.Password))
            {
                errors.Add("Password and confirm password must match");
            }

            return(errors);
        }
Beispiel #15
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUser(model);

            if (this.data.Users.Any(u => u.Username == model.Username))
            {
                modelErrors.Add($"User with '{model.Username}' username already exists.");
            }

            if (this.data.Users.Any(u => u.Email == model.Email))
            {
                modelErrors.Add($"User with '{model.Email}' e-mail already exists.");
            }

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            var user = new User
            {
                Username = model.Username,
                Password = this.passwordHasher.HashPassword(model.Password),
                Email    = model.Email
            };

            data.Users.Add(user);

            data.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #16
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var result = UserService.Register(model);

            if (result != null)
            {
                return(Error(result));
            }

            return(Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUser(model);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            this.userService.Create(model.Username, model.Email, model.Password, model.UserType);

            return(Redirect("/Users/Login"));
        }
Beispiel #18
0
        public void CreateUser(RegisterUserFormModel model)
        {
            var user = new User
            {
                Username   = model.Username,
                Password   = passwordHasher.HashPassword(model.Password),
                Email      = model.Email,
                IsMechanic = model.UserType == UserTypeMechanic
            };

            db.Users.Add(user);

            db.SaveChanges();
        }
Beispiel #19
0
        public ICollection <string> ValidateUser(RegisterUserFormModel user)
        {
            var errors = new List <string>();

            if (user.Username == null || user.Username.Length < UserMinUsername || user.Username.Length > DefaultMaxLength)
            {
                //errors.Add(string.Format(ErrorViewText, $"Username '{user.Username}' is not valid. It must be between {UserMinUsername} and {DefaultMaxLength} characters long."));

                errors.Add($"Username '{user.Username}' is not valid. It must be between {UserMinUsername} and {DefaultMaxLength} characters long.");
            }

            if (user.Email == null || !Regex.IsMatch(user.Email, UserEmailRegularExpression))
            {
                errors.Add($"Email '{user.Email}' is not a valid e-mail address.");
            }

            if (user.Password == null || user.Password.Length < UserMinPassword || user.Password.Length > DefaultMaxLength)
            {
                errors.Add($"The provided password is not valid. It must be between {UserMinPassword} and {DefaultMaxLength} characters long.");
            }

            if (user.Password != null && user.Password.Any(x => x == ' '))
            {
                errors.Add($"The provided password cannot contain whitespaces.");
            }

            if (user.Password != user.ConfirmPassword)
            {
                errors.Add("Password and its confirmation are different.");
            }

            if (user.UserType != UserTypeClient && user.UserType != UserTypeMechanic)
            {
                errors.Add($"The user can be either a '{UserTypeClient}' or a '{UserTypeMechanic}'.");
            }

            if (this.db.Users.Any(u => u.Username == user.Username))
            {
                errors.Add($"User with '{user.Username}' username already exists.");
            }

            if (this.db.Users.Any(u => u.Email == user.Email))
            {
                errors.Add($"User with '{user.Email}' e-mail already exists.");
            }

            return(errors);
        }
        public HttpResponse Register(RegisterUserFormModel model)
        {
            if (this.User.IsAuthenticated)
            {
                return(Redirect("/Repositories/All"));
            }

            var modelErrors = this.usersService.RegisterUser(model);

            if (modelErrors.Count > 0)
            {
                return(this.Error(modelErrors));
            }

            return(this.Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterUserFormModel user)
        {
            if (User.IsAuthenticated)
            {
                return(Error($"401 Unauthorized"));
            }

            var modelErrors = this.validator.ValidateUser(user);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            usersService.CreateUser(user);

            return(Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterUserFormModel model)
        {
            if (this.User.IsAuthenticated)
            {
                return(this.Redirect("/"));
            }

            var errors = this.validator.ValidateUser(model);

            if (errors.Any())
            {
                return(this.Error(errors));
            }

            this.userService.Create(model.Username, model.Email, model.Password);

            return(this.Redirect("/Users/Login"));
        }
Beispiel #23
0
        public List <string> Register(RegisterUserFormModel model)
        {
            var modelErrors = Validator.ValidateUser(model);

            if (this.Data.Users.Any(u => u.Username == model.Username))
            {
                modelErrors.Add($"User with '{model.Username}' username already exists.");
            }

            if (this.Data.Users.Any(u => u.Email == model.Email))
            {
                modelErrors.Add($"User with '{model.Email}' e-mail already exists.");
            }

            if (modelErrors.Any())
            {
                return(modelErrors.ToList());
            }

            var user = new User
            {
                Username = model.Username,
                Password = this.PasswordHasher.GeneratePassword(model.Password),
                Email    = model.Email,
            };

            var cart = new Cart {
                Products = new List <Product>(), User = user
            };

            user.Cart   = cart;
            user.CartId = cart.Id;

            Data.Users.Add(user);
            Data.Carts.Add(cart);

            Data.SaveChanges();

            return(modelErrors.ToList());
        }
        public HttpResponse Register(RegisterUserFormModel model)
        {
            var modelErrors = this.validator.ValidateUser(model);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            var user = new User
            {
                Username = model.Username,
                Email    = model.Email,
                Password = passwordHasher.HashPassword(model.Password),
            };

            this.data.Users.Add(user);

            this.data.SaveChanges();

            return(Redirect("/Users/Login"));
        }
Beispiel #25
0
        public HttpResponse Register(RegisterUserFormModel model)
        {
            if (this.User.IsAuthenticated)
            {
                return(this.Redirect("/Users/Index"));
            }

            if (this.userService.IsUsernameAvailable(model.Username))
            {
                return(this.Error("User with this username is an already exist!"));
            }

            var errors = this.validator.ValidateUser(model);

            if (errors.Any())
            {
                return(this.Error(errors));
            }

            this.userService.Create(model.Username, model.Email, model.Password);

            return(this.Redirect("/Users/Login"));
        }
Beispiel #26
0
        public ICollection <string> ValidateUser(RegisterUserFormModel model)
        {
            var errors = new List <string>();

            if (model.Username.Length < UserMinUsername || model.Username.Length > DefaultMaxLength)
            {
                errors.Add($"Username '{model.Username}' is not valid. It must be between {UserMinUsername} and {DefaultMaxLength} characters long.");
            }

            if (!Regex.IsMatch(model.Email, UserEmailRegularExpression))
            {
                errors.Add($"Email {model.Email} is not a valid e-mail address.");
            }

            if (model.Password.Length < UserMinPassword || model.Password.Length > DefaultMaxLength)
            {
                errors.Add($"The provided password is not valid. It must be between {UserMinPassword} and {DefaultMaxLength} characters long.");
            }

            if (model.Password.Any(x => x == ' '))
            {
                errors.Add($"The provided password cannot contain whitespaces.");
            }

            if (model.Password != model.ConfirmPassword)
            {
                errors.Add($"Password and its confirmation are different.");
            }

            if (model.UserType != UserTypeMechanic && model.UserType != UserTypeClient)
            {
                errors.Add($"User should be either a '{UserTypeMechanic}' or '{UserTypeClient}'.");
            }

            return(errors);
        }