예제 #1
0
파일: user.go 프로젝트: arbrix/go-test
func (u *User) CheckPass(db interfaces.Orm) (int, error) {
	//From github.com/asaskevich/govalidator
	emailPattern := "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
	paswd := u.Password
	if regexp.MustCompile(emailPattern).MatchString(u.Email) {
		if db.First(u, u) != nil {
			return http.StatusNotFound, errors.New("User is not found by email.")
		}
	} else {
		u.Name, u.Email = u.Email, ""
		if db.First(u, u) != nil {
			return http.StatusNotFound, errors.New("User is not found by name.")
		}
	}
	err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(paswd))
	if err != nil {
		return http.StatusUnauthorized, errors.New("Password incorrect.")
	}
	return http.StatusOK, nil
}
예제 #2
0
파일: user.go 프로젝트: arbrix/go-test
func (u *User) CreateNew(db interfaces.Orm) error {
	checkUser := &User{}
	if db.First(checkUser, u) == nil {
		return errors.New("User already exists")
	}
	if len(u.Password) == 0 {
		strHelper := &helper.Str{}
		u.Password = strHelper.GenRand(12)
		log.Println(u.Password)
	}

	hashPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), 10)
	u.Password = string(hashPassword)
	if err != nil {
		return err
	}
	now := time.Now()
	u.CreatedAt = &now
	err = db.Create(u)
	if err != nil {
		return err
	}
	return nil
}