ms-auth/internal/models/user.go
Vyacheslav1557 3b03447d2f feat(user):
2024-12-30 20:04:26 +05:00

53 lines
1 KiB
Go

package models
import (
"errors"
"golang.org/x/crypto/bcrypt"
"time"
)
type Role int32
const (
RoleParticipant Role = 0
RoleModerator Role = 1
RoleAdmin Role = 2
)
func (role Role) IsAdmin() bool {
return role == RoleAdmin
}
func (role Role) IsModerator() bool {
return role == RoleModerator
}
func (role Role) IsParticipant() bool {
return role == RoleParticipant
}
func (role Role) AtLeast(other Role) bool {
return role >= other
}
func (role Role) AtMost(other Role) bool {
return role <= other
}
type User struct {
Id int32 `db:"id"`
Username string `db:"username"`
HashedPassword string `db:"hashed_pwd"`
CreatedAt time.Time `db:"created_at"`
ModifiedAt time.Time `db:"modified_at"`
Role Role `db:"role"`
}
func (user *User) ComparePassword(password string) error {
err := bcrypt.CompareHashAndPassword([]byte(user.HashedPassword), []byte(password))
if err != nil {
return errors.New("bad username or password")
}
return nil
}