Exemple #1
0
 public UserModel Insert(NewUserRequestModel newUser)
 {
     string message;
     if (NewUserRequestModel.Validate(newUser, out message))
     {
         User user = new User();
         user.Username = newUser.Username;
         user.Salt = Hasher.GetSalt();
         user.Password = Hasher.Hash(newUser.Password, user.Salt);
         _userRepo.Insert(user);
         return Mapper.Map<User, UserModel>(user);
     }
     else
     {
         return null;
     }
 }
        public static bool Validate(NewUserRequestModel user, out string message)
        {
            bool fail = false; ;
            if (user.Username.Length < UID_MIN_LENGTH)
            {
                fail = true;
                message = string.Format("Username must be at least {0} characters", UID_MIN_LENGTH);
            }
            else if(user.Username.Length > UID_MAX_LENGTH)
            {
                fail = true;
                message = string.Format("Username can be no more than {0} characters", UID_MAX_LENGTH);
            }
            else if(!uidRegex.IsMatch(user.Username))
            {
                fail = true;
                message = "Username is not correct format";
            }
            else if(user.Password.Length < PWD_MIN_LENGTH)
            {
                fail = true;
                message = string.Format("Password must be at least {0} characters", PWD_MIN_LENGTH);
            }
            else if(user.Password != user.Password2)
            {
                fail = true;
                message = "Retyped password is not matched";
            }
            else if(user.Password == user.Username)
            {
                fail = true;
                message = "Password should not be same as username";
            }
            else
            {
                message = "Success";
            }

            return !fail;
        }