-
Notifications
You must be signed in to change notification settings - Fork 2
/
2_user.go
95 lines (80 loc) · 2.31 KB
/
2_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package claim
import (
"github.com/temphia/temphia/code/backend/xtypes"
)
type User struct {
TenantId string `json:"tenent_id,omitempty"`
UserID string `json:"user_id,omitempty"`
UserGroup string `json:"user_group,omitempty"`
Type string `json:"type,omitempty"`
DeviceId int64 `json:"device_id,omitempty"`
Scopes []string `json:"scopes,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
}
func NewUserDevice(tenantId, userId, groupId string, scopes []string) *User {
return &User{
TenantId: tenantId,
Type: "device",
UserID: userId,
UserGroup: groupId,
DeviceId: 0,
Scopes: scopes,
Attributes: make(map[string]string),
}
}
func NewUserLogged(tenantId, userId, groupId string, device int64, scopes []string) *User {
return &User{
TenantId: tenantId,
Type: "logged",
UserID: userId,
UserGroup: groupId,
DeviceId: device,
Scopes: scopes,
Attributes: make(map[string]string),
}
}
func (u *User) IsSuperAdmin() bool {
return u.UserGroup == xtypes.UserGroupSuperAdmin
}
func (u *User) IsGuest() bool {
return u.UserGroup == xtypes.UserGroupGuest
}
func (u *User) DeriveSession(sid int64) *Session {
return &Session{
TenantId: u.TenantId,
UserID: u.UserID,
UserGroup: u.UserGroup,
Type: "session",
SessionID: sid,
DeviceId: u.DeviceId,
Attributes: make(map[string]string),
}
}
// session
type Session struct {
TenantId string `json:"-"`
UserID string `json:"user,omitempty"`
UserGroup string `json:"group,omitempty"`
Type string `json:"type,omitempty"`
SessionID int64 `json:"session_id,omitempty"`
DeviceId int64 `json:"device_id,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
}
func (p *Session) SetAttr(key, value string) {
p.Attributes[key] = value
}
func (u *Session) IsSuperAdmin() bool {
return u.UserGroup == xtypes.UserGroupSuperAdmin
}
func (u *Session) IsGuest() bool {
return u.UserGroup == xtypes.UserGroupGuest
}
func (u *Session) AsUserCtx() *UserContext {
return &UserContext{
TenantId: u.TenantId,
UserID: u.UserID,
UserGroup: u.UserGroup,
SessionID: u.SessionID,
DeviceId: u.DeviceId,
}
}