[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
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
|
|
||||||
"git.fsdpf.net/go/contracts"
|
"git.fsdpf.net/go/contracts"
|
||||||
@ -13,8 +11,8 @@ type user struct {
|
|||||||
uuid string
|
uuid string
|
||||||
username string
|
username string
|
||||||
nickname string
|
nickname string
|
||||||
platform string
|
|
||||||
roles []string
|
roles []string
|
||||||
|
runtime contracts.UserRuntime
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this user) ID() int64 {
|
func (this user) ID() int64 {
|
||||||
@ -46,36 +44,45 @@ func (this user) HasUserRoles(roles ...string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this user) IsAnonymous() 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 {
|
func (this user) Runtime() contracts.UserRuntime {
|
||||||
if this.platform == "" {
|
return this.runtime
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
return this.platform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取匿名用户
|
// 获取匿名用户
|
||||||
func GetAnonymous() contracts.User {
|
func GetAnonymous(opts ...contracts.UserRuntimeOption) contracts.User {
|
||||||
return user{
|
u := user{
|
||||||
id: 0,
|
id: 0,
|
||||||
uuid: "00000000-0000-0000-0000-000000000000",
|
uuid: "00000000-0000-0000-0000-000000000000",
|
||||||
username: "anonymous",
|
username: "anonymous",
|
||||||
nickname: "匿名者",
|
nickname: "匿名者",
|
||||||
roles: []string{"00000000-0000-0000-0000-000000000000"},
|
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 {
|
func GetSystemUser(opts ...contracts.UserRuntimeOption) contracts.User {
|
||||||
return user{
|
u := user{
|
||||||
id: -1,
|
id: -1,
|
||||||
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff",
|
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff",
|
||||||
username: "system",
|
username: "system",
|
||||||
nickname: "系统",
|
nickname: "系统",
|
||||||
roles: []string{"ffffffff-ffff-ffff-ffff-ffffffffffff"},
|
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
|
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 {
|
type User interface {
|
||||||
|
Runtime() UserRuntime
|
||||||
// 用户ID
|
// 用户ID
|
||||||
ID() int64
|
ID() int64
|
||||||
// 用户UUID
|
// 用户UUID
|
||||||
@ -15,11 +31,23 @@ type User interface {
|
|||||||
Roles() []string
|
Roles() []string
|
||||||
// 用户权限检查
|
// 用户权限检查
|
||||||
HasUserRoles(roles ...string) bool
|
HasUserRoles(roles ...string) bool
|
||||||
// 平台
|
|
||||||
Platform() string
|
|
||||||
// 是否匿名用户
|
// 是否匿名用户
|
||||||
IsAnonymous() bool
|
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