You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/internal/model/user.go

40 lines
1.1 KiB
Go

package model
import (
"github.com/zijiren233/stream"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
2 years ago
type Role uint8
const (
RoleBanned Role = iota
RoleUser
RoleAdmin
RoleRoot
2 years ago
)
type User struct {
gorm.Model
Username string `gorm:"not null;uniqueIndex"`
2 years ago
Role Role `gorm:"not null"`
HashedPassword []byte
GroupUserRelations []RoomUserRelation `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Rooms []Room `gorm:"foreignKey:CreatorID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Movies []Movie `gorm:"foreignKey:CreatorID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}
func (u *User) CheckPassword(password string) bool {
return bcrypt.CompareHashAndPassword(u.HashedPassword, stream.StringToBytes(password)) == nil
}
func (u *User) SetPassword(password string) error {
hashedPassword, err := bcrypt.GenerateFromPassword(stream.StringToBytes(password), bcrypt.DefaultCost)
if err != nil {
return err
}
u.HashedPassword = hashedPassword
return nil
}