-
Notifications
You must be signed in to change notification settings - Fork 7
/
stores.go
241 lines (212 loc) · 7.32 KB
/
stores.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package server
import (
"context"
cmp "github.com/snetsystems/cmp/backend"
"github.com/snetsystems/cmp/backend/noop"
"github.com/snetsystems/cmp/backend/organizations"
"github.com/snetsystems/cmp/backend/roles"
platform "github.com/snetsystems/cmp/backend/v2"
)
// hasOrganizationContext retrieves organization specified on context
// under the organizations.ContextKey
func hasOrganizationContext(ctx context.Context) (string, bool) {
// prevents panic in case of nil context
if ctx == nil {
return "", false
}
orgID, ok := ctx.Value(organizations.ContextKey).(string)
// should never happen
if !ok {
return "", false
}
if orgID == "" {
return "", false
}
return orgID, true
}
// hasRoleContext retrieves organization specified on context
// under the organizations.ContextKey
func hasRoleContext(ctx context.Context) (string, bool) {
// prevents panic in case of nil context
if ctx == nil {
return "", false
}
role, ok := ctx.Value(roles.ContextKey).(string)
// should never happen
if !ok {
return "", false
}
switch role {
case roles.MemberRoleName, roles.ViewerRoleName, roles.EditorRoleName, roles.AdminRoleName:
return role, true
default:
return "", false
}
}
type userContextKey string
// UserContextKey is the context key for retrieving the user off of context
const UserContextKey = userContextKey("user")
// hasUserContext speficies if the context contains
// the UserContextKey and that the value stored there is cmp.User
func hasUserContext(ctx context.Context) (*cmp.User, bool) {
// prevents panic in case of nil context
if ctx == nil {
return nil, false
}
u, ok := ctx.Value(UserContextKey).(*cmp.User)
// should never happen
if !ok {
return nil, false
}
if u == nil {
return nil, false
}
return u, true
}
// hasSuperAdminContext speficies if the context contains
// the UserContextKey user is a super admin
func hasSuperAdminContext(ctx context.Context) bool {
u, ok := hasUserContext(ctx)
if !ok {
return false
}
return u.SuperAdmin
}
// DataStore is collection of resources that are used by the Service
// Abstracting this into an interface was useful for isolated testing
type DataStore interface {
Sources(ctx context.Context) cmp.SourcesStore
Servers(ctx context.Context) cmp.ServersStore
Layouts(ctx context.Context) cmp.LayoutsStore
Protoboards(ctx context.Context) cmp.ProtoboardsStore
Users(ctx context.Context) cmp.UsersStore
Organizations(ctx context.Context) cmp.OrganizationsStore
Mappings(ctx context.Context) cmp.MappingsStore
Dashboards(ctx context.Context) cmp.DashboardsStore
Config(ctx context.Context) cmp.ConfigStore
OrganizationConfig(ctx context.Context) cmp.OrganizationConfigStore
Cells(ctx context.Context) platform.CellService
DashboardsV2(ctx context.Context) platform.DashboardService
}
// ensure that Store implements a DataStore
var _ DataStore = &Store{}
// Store implements the DataStore interface
type Store struct {
SourcesStore cmp.SourcesStore
ServersStore cmp.ServersStore
LayoutsStore cmp.LayoutsStore
ProtoboardsStore cmp.ProtoboardsStore
UsersStore cmp.UsersStore
DashboardsStore cmp.DashboardsStore
MappingsStore cmp.MappingsStore
OrganizationsStore cmp.OrganizationsStore
ConfigStore cmp.ConfigStore
OrganizationConfigStore cmp.OrganizationConfigStore
CellService platform.CellService
DashboardService platform.DashboardService
}
// Sources returns a noop.SourcesStore if the context has no organization specified
// and an organization.SourcesStore otherwise.
func (s *Store) Sources(ctx context.Context) cmp.SourcesStore {
if isServer := hasServerContext(ctx); isServer {
return s.SourcesStore
}
if org, ok := hasOrganizationContext(ctx); ok {
return organizations.NewSourcesStore(s.SourcesStore, org)
}
return &noop.SourcesStore{}
}
// Servers returns a noop.ServersStore if the context has no organization specified
// and an organization.ServersStore otherwise.
func (s *Store) Servers(ctx context.Context) cmp.ServersStore {
if isServer := hasServerContext(ctx); isServer {
return s.ServersStore
}
if org, ok := hasOrganizationContext(ctx); ok {
return organizations.NewServersStore(s.ServersStore, org)
}
return &noop.ServersStore{}
}
// Layouts returns all layouts in the underlying layouts store.
func (s *Store) Layouts(ctx context.Context) cmp.LayoutsStore {
return s.LayoutsStore
}
// Protoboards returns all protoboards in the underlying protoboards store.
func (s *Store) Protoboards(ctx context.Context) cmp.ProtoboardsStore {
return s.ProtoboardsStore
}
// Users returns a cmp.UsersStore.
// If the context is a server context, then the underlying cmp.UsersStore
// is returned.
// If there is an organization specified on context, then an organizations.UsersStore
// is returned.
// If niether are specified, a noop.UsersStore is returned.
func (s *Store) Users(ctx context.Context) cmp.UsersStore {
if isServer := hasServerContext(ctx); isServer {
return s.UsersStore
}
if org, ok := hasOrganizationContext(ctx); ok {
return organizations.NewUsersStore(s.UsersStore, org)
}
return &noop.UsersStore{}
}
// Dashboards returns a noop.DashboardsStore if the context has no organization specified
// and an organization.DashboardsStore otherwise.
func (s *Store) Dashboards(ctx context.Context) cmp.DashboardsStore {
if isServer := hasServerContext(ctx); isServer {
return s.DashboardsStore
}
if org, ok := hasOrganizationContext(ctx); ok {
return organizations.NewDashboardsStore(s.DashboardsStore, org)
}
return &noop.DashboardsStore{}
}
// OrganizationConfig returns a noop.OrganizationConfigStore if the context has no organization specified
// and an organization.OrganizationConfigStore otherwise.
func (s *Store) OrganizationConfig(ctx context.Context) cmp.OrganizationConfigStore {
if orgID, ok := hasOrganizationContext(ctx); ok {
return organizations.NewOrganizationConfigStore(s.OrganizationConfigStore, orgID)
}
return &noop.OrganizationConfigStore{}
}
// Organizations returns the underlying OrganizationsStore.
func (s *Store) Organizations(ctx context.Context) cmp.OrganizationsStore {
if isServer := hasServerContext(ctx); isServer {
return s.OrganizationsStore
}
if isSuperAdmin := hasSuperAdminContext(ctx); isSuperAdmin {
return s.OrganizationsStore
}
if org, ok := hasOrganizationContext(ctx); ok {
return organizations.NewOrganizationsStore(s.OrganizationsStore, org)
}
return &noop.OrganizationsStore{}
}
// Config returns the underlying ConfigStore.
func (s *Store) Config(ctx context.Context) cmp.ConfigStore {
if isServer := hasServerContext(ctx); isServer {
return s.ConfigStore
}
if isSuperAdmin := hasSuperAdminContext(ctx); isSuperAdmin {
return s.ConfigStore
}
return &noop.ConfigStore{}
}
// Mappings returns the underlying MappingsStore.
func (s *Store) Mappings(ctx context.Context) cmp.MappingsStore {
if isServer := hasServerContext(ctx); isServer {
return s.MappingsStore
}
if isSuperAdmin := hasSuperAdminContext(ctx); isSuperAdmin {
return s.MappingsStore
}
return &noop.MappingsStore{}
}
// Cells returns the underlying CellService.
func (s *Store) Cells(ctx context.Context) platform.CellService {
return s.CellService
}
// DashboardsV2 returns the underlying DashboardsService.
func (s *Store) DashboardsV2(ctx context.Context) platform.DashboardService {
return s.DashboardService
}