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.
OneAuth/models/user.go

83 lines
1.9 KiB
Go

3 years ago
package models
import (
"github.com/veypi/OneAuth/oalib"
3 years ago
"github.com/veypi/utils"
"gorm.io/gorm"
3 years ago
)
// User db user model
type User struct {
BaseModel
Username string `gorm:"type:varchar(100);unique;not null"`
Nickname string `gorm:"type:varchar(100)"`
Phone string `gorm:"type:varchar(20);unique;default:null"`
Email string `gorm:"type:varchar(50);unique;default:null"`
3 years ago
CheckCode string `gorm:"type:varchar(64);not null" json:"-"`
RealCode string `gorm:"type:varchar(32);not null" json:"-"`
Position string
3 years ago
// disabled 禁用
Status string
3 years ago
Icon string
Roles []*Role `gorm:"many2many:UserRoles;"`
Apps []*App `gorm:"many2many:AppUsers;"`
Auths []*Auth `gorm:"foreignkey:UserID;references:ID"`
}
func (u *User) String() string {
return u.Username + ":" + u.Nickname
3 years ago
}
func (u *User) LoadAuths(tx *gorm.DB) error {
return tx.Where("ID = ?", u.ID).Preload("Auths").Preload("Roles.Auths").First(u).Error
}
func (u *User) GetAuths() []*Auth {
list := make([]*Auth, 0, 10)
for _, r := range u.Roles {
for _, a := range r.Auths {
list = append(list, a)
}
}
for _, a := range u.Auths {
list = append(list, a)
}
return list
3 years ago
}
func (u *User) GetAuth(uuid, ResourceID string, ResourceUUID ...string) oalib.AuthLevel {
var res = oalib.AuthNone
ruid := ""
if len(ResourceUUID) > 0 {
ruid = ResourceUUID[0]
}
for _, a := range u.GetAuths() {
if a.RID == ResourceID && a.AppUUID == uuid {
if a.RUID != "" {
if a.RUID == ruid {
if a.Level.Upper(res) {
res = a.Level
}
} else {
continue
}
} else if a.Level.Upper(res) {
res = a.Level
}
3 years ago
}
}
return res
}
func (u *User) UpdatePass(ps string) (err error) {
3 years ago
u.RealCode = utils.RandSeq(32)
u.CheckCode, err = utils.AesEncrypt(u.RealCode, []byte(ps))
return err
}
func (u *User) CheckLogin(ps string) (bool, error) {
temp, err := utils.AesDecrypt(u.CheckCode, []byte(ps))
return temp == u.RealCode, err
}