From 4fc4a1b05356b642878e46dfe56cb25d847774f0 Mon Sep 17 00:00:00 2001 From: what Date: Wed, 27 Dec 2023 22:03:38 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E8=B0=83=E6=95=B4=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF,=20=E6=96=B0=E5=A2=9E=20contracts.UserRuntim?= =?UTF-8?q?e=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/user.go | 37 ++++++++++++++++++++++--------------- user.go | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/base/user.go b/base/user.go index 6409f77..7f2113e 100644 --- a/base/user.go +++ b/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 } diff --git a/user.go b/user.go index f2b8fae..9a6ca36 100644 --- a/user.go +++ b/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 + } +}