[feat] 调整用户信息, 新增 contracts.UserRuntime 类型
This commit is contained in:
parent
95b5b15b16
commit
4fc4a1b053
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
|
||||
}
|
||||
|
34
user.go
34
user.go
@ -1,6 +1,22 @@
|
||||
package contracts
|
||||
|
||||
type UserRuntime struct {
|
||||
platform string
|
||||
saas string
|
||||
}
|
||||
|
||||
// 账号运行平台
|
||||
func (this UserRuntime) Platform() string {
|
||||
return this.platform
|
||||
}
|
||||
|
||||
// 账号运行租户
|
||||
func (this UserRuntime) Saas() string {
|
||||
return this.saas
|
||||
}
|
||||
|
||||
type User interface {
|
||||
Runtime() UserRuntime
|
||||
// 用户ID
|
||||
ID() int64
|
||||
// 用户UUID
|
||||
@ -15,11 +31,23 @@ type User interface {
|
||||
Roles() []string
|
||||
// 用户权限检查
|
||||
HasUserRoles(roles ...string) bool
|
||||
// 平台
|
||||
Platform() string
|
||||
// 是否匿名用户
|
||||
IsAnonymous() bool
|
||||
}
|
||||
|
||||
// 用户
|
||||
type GetUser func(uuid, platform string) (User, error)
|
||||
type GetUser func(uuid string, options ...UserRuntimeOption) (User, error)
|
||||
|
||||
type UserRuntimeOption func(option *UserRuntime)
|
||||
|
||||
func UserRuntimePlatform(value string) UserRuntimeOption {
|
||||
return func(option *UserRuntime) {
|
||||
option.platform = value
|
||||
}
|
||||
}
|
||||
|
||||
func UserRuntimeSaaS(value string) UserRuntimeOption {
|
||||
return func(option *UserRuntime) {
|
||||
option.saas = value
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user