This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
user.go
216 lines (190 loc) · 5.06 KB
/
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package user manages user accounts and their team membership.
package user
import (
"context"
"fmt"
"time"
"log/slog"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/rbac"
"github.com/leg100/otf/internal/team"
)
const (
SiteAdminID = "user-site-admin"
SiteAdminUsername = "site-admin"
)
var (
SiteAdmin = User{ID: SiteAdminID, Username: SiteAdminUsername}
_ internal.Subject = (*User)(nil)
)
type (
// User represents an OTF user account.
User struct {
ID string `jsonapi:"primary,users"`
CreatedAt time.Time `jsonapi:"attribute" json:"created-at"`
UpdatedAt time.Time `jsonapi:"attribute" json:"updated-at"`
SiteAdmin bool `jsonapi:"attribute" json:"site-admin"`
// username is globally unique
Username string `jsonapi:"attribute" json:"username"`
// user belongs to many teams
Teams []*team.Team
}
// UserListOptions are options for the ListUsers endpoint.
UserListOptions struct {
Organization *string
TeamName *string
}
NewUserOption func(*User)
CreateUserOptions struct {
Username string `json:"username"`
}
UserSpec struct {
UserID *string
Username *string
AuthenticationTokenID *string
}
)
func NewUser(username string, opts ...NewUserOption) *User {
user := &User{
ID: internal.NewID("user"),
Username: username,
CreatedAt: internal.CurrentTimestamp(nil),
UpdatedAt: internal.CurrentTimestamp(nil),
}
for _, fn := range opts {
fn(user)
}
return user
}
func WithTeams(memberships ...*team.Team) NewUserOption {
return func(user *User) {
user.Teams = memberships
}
}
func (u *User) String() string { return u.Username }
// IsTeamMember determines whether user is a member of the given team.
func (u *User) IsTeamMember(teamID string) bool {
for _, t := range u.Teams {
if t.ID == teamID {
return true
}
}
return false
}
// Organizations returns the user's membership of organizations (indirectly via
// their membership of teams).
//
// NOTE: always returns a non-nil slice
func (u *User) Organizations() []string {
// De-dup organizations using map
seen := make(map[string]bool)
for _, t := range u.Teams {
if _, ok := seen[t.Organization]; ok {
continue
}
seen[t.Organization] = true
}
// Turn map into slice
organizations := make([]string, len(seen))
var i int
for org := range seen {
organizations[i] = org
i++
}
return organizations
}
// IsSiteAdmin determines whether user is a site admin. A user is a site admin
// in either of two cases:
// (1) their account has been promoted to site admin (think sudo)
// (2) the account is *the* site admin (think root)
func (u *User) IsSiteAdmin() bool {
return u.SiteAdmin || u.ID == SiteAdminID
}
func (u *User) CanAccessSite(action rbac.Action) bool {
switch action {
case rbac.GetGithubAppAction:
return true
case rbac.CreateUserAction, rbac.ListUsersAction:
// A user can perform these actions only if they are an owner of at
// least one organization. This permits an owner to search users or create
// a user before adding them to a team.
for _, team := range u.Teams {
if team.IsOwners() {
return true
}
}
}
// Otherwise only the site admin can perform site actions.
return u.IsSiteAdmin()
}
func (u *User) CanAccessTeam(action rbac.Action, teamID string) bool {
// coarser-grained site-level perms take precedence
if u.CanAccessSite(action) {
return true
}
for _, team := range u.Teams {
if team.ID == teamID {
return true
}
}
return false
}
func (u *User) CanAccessOrganization(action rbac.Action, org string) bool {
// coarser-grained site-level perms take precedence
if u.CanAccessSite(action) {
return true
}
// fallback to finer-grained organization-level perms
for _, team := range u.Teams {
if team.CanAccessOrganization(action, org) {
return true
}
}
return false
}
func (u *User) CanAccessWorkspace(action rbac.Action, policy internal.WorkspacePolicy) bool {
// coarser-grained organization perms take precedence.
if u.CanAccessOrganization(action, policy.Organization) {
return true
}
// fallback to checking finer-grained workspace perms
for _, team := range u.Teams {
if team.CanAccessWorkspace(action, policy) {
return true
}
}
return false
}
// IsOwner determines if user is an owner of an organization
func (u *User) IsOwner(organization string) bool {
for _, team := range u.Teams {
if team.IsOwner(organization) {
return true
}
}
return false
}
func (s UserSpec) LogValue() slog.Value {
if s.Username != nil {
return slog.String("username", *s.Username).Value
}
if s.UserID != nil {
return slog.String("id", *s.UserID).Value
}
if s.AuthenticationTokenID != nil {
return slog.String("token_id", "*****").Value
}
return slog.String("unknown key", "unknown value").Value
}
// UserFromContext retrieves a user from a context
func UserFromContext(ctx context.Context) (*User, error) {
subj, err := internal.SubjectFromContext(ctx)
if err != nil {
return nil, err
}
user, ok := subj.(*User)
if !ok {
return nil, fmt.Errorf("no user in context")
}
return user, nil
}