4 "golang.org/x/crypto/bcrypt"
10 Confirmed UserStatus = iota + 1
16 const RoleUser UserRole = "user"
17 const RoleAdmin UserRole = "admin"
29 func HashPassword(password string) (string, error) {
30 b, err := bcrypt.GenerateFromPassword([]byte(password), 10)
35 func ValidatePassword(password string, hash string) error {
36 return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
39 func InsertUser(user *User) error {
40 return DB.Insert(user)
43 func ConfirmUserByEmail(email string) error {
44 _, err := DB.Model(&User{}).Set("status=?", Confirmed).Where("email=?", email).Returning("*").Update()
49 func GetUserById(id int64) (*User, error) {
52 err := DB.Select(&user)
57 func GetUserByEmail(email string) (*User, error) {
60 err := DB.Model(&users).Where("email = ?", email).Select()
73 func SetOtpSecret(user *User, secret string, temporary bool) error {
74 user.OtpSecret = secret
75 user.IsOtpSetup = !temporary
77 return DB.Update(user)
80 func SetPassword(user *User, password string) error {
82 user.PasswordHash, err = HashPassword(password)
87 return DB.Update(user)
90 func SetUserStatus(user *User, status UserStatus) error {
93 return DB.Update(user)