contracts/base/user.go

89 lines
1.6 KiB
Go

package base
import (
"github.com/samber/lo"
"git.fsdpf.net/go/contracts"
)
type user struct {
id int64
uuid string
username string
nickname string
roles []string
runtime contracts.UserRuntime
}
func (this user) ID() int64 {
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() map[string]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 this.id == 0
}
func (this user) Runtime() contracts.UserRuntime {
return this.runtime
}
// 获取匿名用户
func GetAnonymous(opts ...contracts.UserRuntimeOption) contracts.User {
u := user{
id: 0,
uuid: "00000000-0000-0000-0000-000000000000",
username: "anonymous",
nickname: "匿名者",
roles: []string{"00000000-0000-0000-0000-000000000000"},
runtime: contracts.UserRuntime{},
}
for _, opt := range opts {
opt(&u.runtime)
}
return u
}
// 系统用户
func GetSystemUser(opts ...contracts.UserRuntimeOption) contracts.User {
u := user{
id: -1,
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff",
username: "system",
nickname: "系统",
roles: []string{"ffffffff-ffff-ffff-ffff-ffffffffffff"},
runtime: contracts.UserRuntime{},
}
for _, opt := range opts {
opt(&u.runtime)
}
return u
}