contracts/base/user.go

82 lines
1.5 KiB
Go

package base
import (
"reflect"
"github.com/samber/lo"
"git.fsdpf.net/go/contracts"
)
type User struct {
id int
uuid string
username string
nickname string
platform string
roles []string
}
func (this User) ID() int {
return this.id
}
func (this User) Uuid() string {
return this.uuid
}
func (this User) Username() string {
return this.username
}
func (this User) Nickname() string {
return this.nickname
}
func (this User) GetUserInfo() any {
return nil
}
func (this User) Roles() (roles []string) {
return this.roles
}
func (this User) HasUserRoles(roles ...string) bool {
return lo.Contains(this.Roles(), "ffffffff-ffff-ffff-ffff-ffffffffffff") || len(lo.Intersect(this.Roles(), roles)) > 0
}
func (this User) IsAnonymous() bool {
return reflect.DeepEqual(this.Roles(), []string{"00000000-0000-0000-0000-000000000000"})
}
func (this User) Platform() string {
if this.platform == "" {
return "unknown"
}
return this.platform
}
// 获取匿名用户
func GetAnonymous() contracts.User {
return User{
id: 0,
uuid: "00000000-0000-0000-0000-000000000000",
username: "anonymous",
nickname: "匿名者",
roles: []string{"00000000-0000-0000-0000-000000000000"},
platform: "unknown",
}
}
// 系统用户
func GetSystemUser() contracts.User {
return User{
id: -1,
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff",
username: "system",
nickname: "系统",
roles: []string{"ffffffff-ffff-ffff-ffff-ffffffffffff"},
platform: "unknown",
}
}