You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
734 B
39 lines
734 B
package models
|
|
|
|
import (
|
|
"errors"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type User struct {
|
|
ID string `db:"user_id"`
|
|
Name string `db:"name"`
|
|
Active bool `db:"active"`
|
|
Admin bool `db:"admin"`
|
|
Hash []byte `db:"hash"`
|
|
}
|
|
|
|
func (user *User) CheckPassword(password string) bool {
|
|
return bcrypt.CompareHashAndPassword(user.Hash, []byte(password)) == nil
|
|
}
|
|
|
|
func (user *User) SetPassword(password string) error {
|
|
if len(password) < 6 {
|
|
return errors.New("password too short (min: 6)")
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user.Hash = hash
|
|
return nil
|
|
}
|
|
|
|
type UserFilter struct {
|
|
UserIDs []string
|
|
Active *bool
|
|
Admin *bool
|
|
Limit *int
|
|
}
|