[feat] 调整用户信息, 新增 contracts.UserRuntime 类型
This commit is contained in:
37
base/user.go
37
base/user.go
@@ -1,8 +1,6 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/samber/lo"
|
||||
|
||||
"git.fsdpf.net/go/contracts"
|
||||
@@ -13,8 +11,8 @@ type user struct {
|
||||
uuid string
|
||||
username string
|
||||
nickname string
|
||||
platform string
|
||||
roles []string
|
||||
runtime contracts.UserRuntime
|
||||
}
|
||||
|
||||
func (this user) ID() int64 {
|
||||
@@ -46,36 +44,45 @@ func (this user) HasUserRoles(roles ...string) bool {
|
||||
}
|
||||
|
||||
func (this user) IsAnonymous() bool {
|
||||
return reflect.DeepEqual(this.Roles(), []string{"00000000-0000-0000-0000-000000000000"})
|
||||
return this.id == 0
|
||||
}
|
||||
|
||||
func (this user) Platform() string {
|
||||
if this.platform == "" {
|
||||
return "unknown"
|
||||
}
|
||||
return this.platform
|
||||
func (this user) Runtime() contracts.UserRuntime {
|
||||
return this.runtime
|
||||
}
|
||||
|
||||
// 获取匿名用户
|
||||
func GetAnonymous() contracts.User {
|
||||
return user{
|
||||
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"},
|
||||
platform: "unknown",
|
||||
runtime: contracts.UserRuntime{},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&u.runtime)
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
// 系统用户
|
||||
func GetSystemUser() contracts.User {
|
||||
return user{
|
||||
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"},
|
||||
platform: "unknown",
|
||||
runtime: contracts.UserRuntime{},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&u.runtime)
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user