-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenant.go
245 lines (219 loc) · 6.68 KB
/
tenant.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
242
243
244
245
package inmemory
import (
"time"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/env"
)
// TenantStorage contains read and write operations for tenants
type TenantStorage struct {
lastID int
tenants []*models.Tenant
current *models.Tenant
user *models.User
verifications []*models.EmailVerification
oauthConfigs []*models.OAuthConfig
}
//NewTenantStorage creates a new TenantStorage
func NewTenantStorage() *TenantStorage {
return &TenantStorage{
tenants: make([]*models.Tenant, 0),
verifications: make([]*models.EmailVerification, 0),
oauthConfigs: make([]*models.OAuthConfig, 0),
}
}
// SetCurrentTenant tenant
func (s *TenantStorage) SetCurrentTenant(tenant *models.Tenant) {
s.current = tenant
}
// SetCurrentUser to current context
func (s *TenantStorage) SetCurrentUser(user *models.User) {
s.user = user
}
// Current returns current context tenant
func (s *TenantStorage) Current() *models.Tenant {
return s.current
}
// Add given tenant to tenant list
func (s *TenantStorage) Add(name string, subdomain string, status int) (*models.Tenant, error) {
s.lastID = s.lastID + 1
tenant := &models.Tenant{ID: s.lastID, Name: name, Subdomain: subdomain, Status: status}
s.tenants = append(s.tenants, tenant)
return tenant, nil
}
// First returns first tenant
func (s *TenantStorage) First() (*models.Tenant, error) {
for _, tenant := range s.tenants {
return tenant, nil
}
return nil, app.ErrNotFound
}
// GetByDomain returns a tenant based on its domain
func (s *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {
for _, tenant := range s.tenants {
if tenant.Subdomain == env.Subdomain(domain) || tenant.Subdomain == domain || tenant.CNAME == domain {
return tenant, nil
}
}
return nil, app.ErrNotFound
}
// IsSubdomainAvailable returns true if subdomain is available to use
func (s *TenantStorage) IsSubdomainAvailable(subdomain string) (bool, error) {
for _, tenant := range s.tenants {
if tenant.Subdomain == subdomain {
return false, nil
}
}
return true, nil
}
// IsCNAMEAvailable returns true if cname is available to use
func (s *TenantStorage) IsCNAMEAvailable(cname string) (bool, error) {
for _, tenant := range s.tenants {
if tenant.CNAME == cname {
return false, nil
}
}
return true, nil
}
// UpdateSettings of current tenant
func (s *TenantStorage) UpdateSettings(settings *models.UpdateTenantSettings) error {
if settings.Logo.Remove {
settings.Logo.BlobKey = ""
}
for _, tenant := range s.tenants {
if tenant.ID == s.current.ID {
tenant.Invitation = settings.Invitation
tenant.WelcomeMessage = settings.WelcomeMessage
tenant.Name = settings.Title
tenant.CNAME = settings.CNAME
tenant.LogoBlobKey = settings.Logo.BlobKey
return nil
}
}
return nil
}
// UpdateBillingSettings of current tenant
func (s *TenantStorage) UpdateBillingSettings(billing *models.TenantBilling) error {
s.current.Billing = billing
return nil
}
// UpdateAdvancedSettings of current tenant
func (s *TenantStorage) UpdateAdvancedSettings(settings *models.UpdateTenantAdvancedSettings) error {
for _, tenant := range s.tenants {
if tenant.ID == s.current.ID {
tenant.CustomCSS = settings.CustomCSS
return nil
}
}
return nil
}
// UpdatePrivacy settings of current tenant
func (s *TenantStorage) UpdatePrivacy(settings *models.UpdateTenantPrivacy) error {
for _, tenant := range s.tenants {
if tenant.ID == s.current.ID {
tenant.IsPrivate = settings.IsPrivate
return nil
}
}
return nil
}
// Activate given tenant
func (s *TenantStorage) Activate(id int) error {
for _, tenant := range s.tenants {
if tenant.ID == id {
tenant.Status = models.TenantActive
return nil
}
}
return app.ErrNotFound
}
// SaveVerificationKey used by email verification
func (s *TenantStorage) SaveVerificationKey(key string, duration time.Duration, request models.NewEmailVerification) error {
userID := 0
if request.GetUser() != nil {
userID = request.GetUser().ID
}
s.verifications = append(s.verifications, &models.EmailVerification{
Email: request.GetEmail(),
Name: request.GetName(),
Kind: request.GetKind(),
Key: key,
UserID: userID,
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(duration),
VerifiedAt: nil,
})
return nil
}
// FindVerificationByKey based on current tenant
func (s *TenantStorage) FindVerificationByKey(kind models.EmailVerificationKind, key string) (*models.EmailVerification, error) {
for _, verification := range s.verifications {
if verification.Key == key && verification.Kind == kind {
return verification, nil
}
}
return nil, app.ErrNotFound
}
// SetKeyAsVerified so that it cannot be used anymore
func (s *TenantStorage) SetKeyAsVerified(key string) error {
for _, verification := range s.verifications {
if verification.Key == key {
now := time.Now()
verification.VerifiedAt = &now
}
}
return nil
}
// SaveOAuthConfig saves given config into database
func (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) error {
bkey := ""
if config.Logo != nil {
bkey = config.Logo.BlobKey
}
for _, c := range s.oauthConfigs {
if c.ID == config.ID {
c.Provider = config.Provider
c.DisplayName = config.DisplayName
c.ClientID = config.ClientID
c.ClientSecret = config.ClientSecret
c.AuthorizeURL = config.AuthorizeURL
c.TokenURL = config.TokenURL
c.Scope = config.Scope
c.ProfileURL = config.ProfileURL
c.JSONUserIDPath = config.JSONUserIDPath
c.JSONUserNamePath = config.JSONUserNamePath
c.JSONUserEmailPath = config.JSONUserEmailPath
c.LogoBlobKey = bkey
return nil
}
}
s.oauthConfigs = append(s.oauthConfigs, &models.OAuthConfig{
ID: config.ID,
Provider: config.Provider,
DisplayName: config.DisplayName,
LogoBlobKey: bkey,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
AuthorizeURL: config.AuthorizeURL,
TokenURL: config.TokenURL,
Scope: config.Scope,
ProfileURL: config.ProfileURL,
JSONUserIDPath: config.JSONUserIDPath,
JSONUserNamePath: config.JSONUserNamePath,
JSONUserEmailPath: config.JSONUserEmailPath,
})
return nil
}
// GetOAuthConfigByProvider returns a custom OAuth configuration by provider name
func (s *TenantStorage) GetOAuthConfigByProvider(provider string) (*models.OAuthConfig, error) {
for _, c := range s.oauthConfigs {
if c.Provider == provider {
return c, nil
}
}
return nil, app.ErrNotFound
}
// ListOAuthConfig returns a list of all custom OAuth provider for current tenant
func (s *TenantStorage) ListOAuthConfig() ([]*models.OAuthConfig, error) {
return s.oauthConfigs, nil
}