2018-01-26 08:43:53 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-01-29 09:18:19 +01:00
|
|
|
"time"
|
2018-01-26 08:43:53 +01:00
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2018-01-26 08:43:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// User represents a User of the system which is able to log in
|
|
|
|
type User struct {
|
2018-01-29 09:18:19 +01:00
|
|
|
Model
|
|
|
|
Email string
|
|
|
|
EmailValid bool
|
|
|
|
DisplayName string
|
2018-01-26 08:43:53 +01:00
|
|
|
HashedPassword []byte
|
|
|
|
IsAdmin bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword sets the password of an user struct, but does not save it yet
|
|
|
|
func (u *User) SetPassword(password string) error {
|
2018-01-29 09:18:19 +01:00
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.HashedPassword = bytes
|
|
|
|
return nil
|
2018-01-26 08:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPassword compares a supplied plain text password with the internally
|
|
|
|
// stored password hash, returns error=nil on success.
|
|
|
|
func (u *User) CheckPassword(password string) error {
|
2018-01-29 09:18:19 +01:00
|
|
|
return bcrypt.CompareHashAndPassword(u.HashedPassword, []byte(password))
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserProvider interface {
|
|
|
|
CountUsers() (uint, error)
|
2018-02-01 03:30:00 +01:00
|
|
|
CreateUser(*User) error
|
2018-01-29 09:18:19 +01:00
|
|
|
ListUsers(count, offset int) ([]*User, error)
|
|
|
|
GetUserByID(id uint) (*User, error)
|
|
|
|
GetUserByEmail(email string) (*User, error)
|
|
|
|
DeleteUser(id uint) error
|
2018-01-26 08:43:53 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:30:00 +01:00
|
|
|
type PasswordReset struct {
|
2018-01-29 09:18:19 +01:00
|
|
|
Model
|
2018-02-01 03:30:00 +01:00
|
|
|
User *User
|
2018-01-29 09:18:19 +01:00
|
|
|
UserID uint
|
2018-02-01 03:30:00 +01:00
|
|
|
Token string
|
|
|
|
ValidUntil time.Time
|
2018-01-26 08:43:53 +01:00
|
|
|
}
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-02-01 03:30:00 +01:00
|
|
|
type PasswordResetProvider interface {
|
|
|
|
CreatePasswordReset(*PasswordReset) error
|
|
|
|
GetPasswordResetByToken(token string) (*PasswordReset, error)
|
2018-01-29 09:18:19 +01:00
|
|
|
}
|