contracts/base/user.go

82 lines
1.5 KiB
Go
Raw Normal View History

2023-04-12 16:56:55 +08:00
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",
}
}