contracts/base/user.go

89 lines
1.6 KiB
Go
Raw Normal View History

2023-04-12 16:56:55 +08:00
package base
import (
"github.com/samber/lo"
2024-05-09 13:15:24 +08:00
"git.fsdpf.net/go/req"
2023-04-12 16:56:55 +08:00
)
2023-04-13 14:47:34 +08:00
type user struct {
2023-12-05 16:01:42 +08:00
id int64
2023-04-12 16:56:55 +08:00
uuid string
username string
nickname string
roles []string
2024-05-09 13:15:24 +08:00
runtime req.UserRuntime
2023-04-12 16:56:55 +08:00
}
2023-12-05 16:01:42 +08:00
func (this user) ID() int64 {
2023-04-12 16:56:55 +08:00
return this.id
}
2023-04-13 14:47:34 +08:00
func (this user) Uuid() string {
2023-04-12 16:56:55 +08:00
return this.uuid
}
2023-04-13 14:47:34 +08:00
func (this user) Username() string {
2023-04-12 16:56:55 +08:00
return this.username
}
2023-04-13 14:47:34 +08:00
func (this user) Nickname() string {
2023-04-12 16:56:55 +08:00
return this.nickname
}
func (this user) GetUserInfo() map[string]any {
2023-04-12 16:56:55 +08:00
return nil
}
2023-04-13 14:47:34 +08:00
func (this user) Roles() (roles []string) {
2023-04-12 16:56:55 +08:00
return this.roles
}
2023-04-13 14:47:34 +08:00
func (this user) HasUserRoles(roles ...string) bool {
2023-04-12 16:56:55 +08:00
return lo.Contains(this.Roles(), "ffffffff-ffff-ffff-ffff-ffffffffffff") || len(lo.Intersect(this.Roles(), roles)) > 0
}
2023-04-13 14:47:34 +08:00
func (this user) IsAnonymous() bool {
return this.id == 0
2023-04-12 16:56:55 +08:00
}
2024-05-09 13:15:24 +08:00
func (this user) Runtime() req.UserRuntime {
return this.runtime
2023-04-12 16:56:55 +08:00
}
// 获取匿名用户
2024-05-09 13:15:24 +08:00
func GetAnonymous(opts ...req.UserRuntimeOption) req.User {
u := user{
2023-04-12 16:56:55 +08:00
id: 0,
uuid: "00000000-0000-0000-0000-000000000000",
username: "anonymous",
nickname: "匿名者",
roles: []string{"00000000-0000-0000-0000-000000000000"},
2024-05-09 13:15:24 +08:00
runtime: req.UserRuntime{},
}
for _, opt := range opts {
opt(&u.runtime)
2023-04-12 16:56:55 +08:00
}
return u
2023-04-12 16:56:55 +08:00
}
// 系统用户
2024-05-09 13:15:24 +08:00
func GetSystemUser(opts ...req.UserRuntimeOption) req.User {
u := user{
2023-04-12 16:56:55 +08:00
id: -1,
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff",
username: "system",
nickname: "系统",
roles: []string{"ffffffff-ffff-ffff-ffff-ffffffffffff"},
2024-05-09 13:15:24 +08:00
runtime: req.UserRuntime{},
}
for _, opt := range opts {
opt(&u.runtime)
2023-04-12 16:56:55 +08:00
}
return u
2023-04-12 16:56:55 +08:00
}