contracts/base/user.go

82 lines
1.5 KiB
Go
Raw Normal View History

2023-04-12 16:56:55 +08:00
package base
import (
"reflect"
"github.com/samber/lo"
"git.fsdpf.net/go/contracts"
)
2023-04-12 22:50:37 +08:00
type User struct {
2023-04-12 16:56:55 +08:00
id int
uuid string
username string
nickname string
platform string
roles []string
}
2023-04-12 22:50:37 +08:00
func (this User) ID() int {
2023-04-12 16:56:55 +08:00
return this.id
}
2023-04-12 22:50:37 +08:00
func (this User) Uuid() string {
2023-04-12 16:56:55 +08:00
return this.uuid
}
2023-04-12 22:50:37 +08:00
func (this User) Username() string {
2023-04-12 16:56:55 +08:00
return this.username
}
2023-04-12 22:50:37 +08:00
func (this User) Nickname() string {
2023-04-12 16:56:55 +08:00
return this.nickname
}
2023-04-12 22:50:37 +08:00
func (this User) GetUserInfo() any {
2023-04-12 16:56:55 +08:00
return nil
}
2023-04-12 22:50:37 +08:00
func (this User) Roles() (roles []string) {
2023-04-12 16:56:55 +08:00
return this.roles
}
2023-04-12 22:50:37 +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-12 22:50:37 +08:00
func (this User) IsAnonymous() bool {
2023-04-12 16:56:55 +08:00
return reflect.DeepEqual(this.Roles(), []string{"00000000-0000-0000-0000-000000000000"})
}
2023-04-12 22:50:37 +08:00
func (this User) Platform() string {
2023-04-12 16:56:55 +08:00
if this.platform == "" {
return "unknown"
}
return this.platform
}
// 获取匿名用户
func GetAnonymous() contracts.User {
2023-04-12 22:50:37 +08:00
return 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"},
platform: "unknown",
}
}
// 系统用户
func GetSystemUser() contracts.User {
2023-04-12 22:50:37 +08:00
return 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"},
platform: "unknown",
}
}