-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenant.go
175 lines (144 loc) · 5.2 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
package actions
import (
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/validate"
)
//CreateTenant is the input model used to create a tenant
type CreateTenant struct {
Model *models.CreateTenant
}
// Initialize the model
func (input *CreateTenant) Initialize() interface{} {
input.Model = new(models.CreateTenant)
input.Model.VerificationKey = models.GenerateSecretKey()
return input.Model
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *CreateTenant) IsAuthorized(user *models.User, services *app.Services) bool {
return true
}
// Validate is current model is valid
func (input *CreateTenant) Validate(user *models.User, services *app.Services) *validate.Result {
result := validate.Success()
var err error
if input.Model.Name == "" && input.Model.Email == "" {
if input.Model.Token == "" {
result.AddFieldFailure("token", "Please identify yourself before proceeding.")
} else {
if input.Model.UserClaims, err = jwt.DecodeOAuthClaims(input.Model.Token); err != nil {
return validate.Error(err)
}
}
} else {
if input.Model.Email == "" {
result.AddFieldFailure("email", "Email is required.")
} else {
messages := validate.Email(input.Model.Email)
result.AddFieldFailure("email", messages...)
}
if input.Model.Name == "" {
result.AddFieldFailure("name", "Name is required.")
}
if len(input.Model.Name) > 60 {
result.AddFieldFailure("name", "Name must have less than 60 characters.")
}
}
if env.IsSingleHostMode() {
input.Model.Subdomain = "default"
}
if input.Model.TenantName == "" {
result.AddFieldFailure("tenantName", "Name is required.")
}
messages, err := validate.Subdomain(services.Tenants, input.Model.Subdomain)
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("subdomain", messages...)
if env.HasLegal() && !input.Model.LegalAgreement {
result.AddFieldFailure("legalAgreement", "You must agree before proceeding.")
}
return result
}
//UpdateTenantSettings is the input model used to update tenant settings
type UpdateTenantSettings struct {
Model *models.UpdateTenantSettings
}
// Initialize the model
func (input *UpdateTenantSettings) Initialize() interface{} {
input.Model = new(models.UpdateTenantSettings)
input.Model.Logo = &models.ImageUpload{}
return input.Model
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *UpdateTenantSettings) IsAuthorized(user *models.User, services *app.Services) bool {
return user != nil && user.Role == models.RoleAdministrator
}
// Validate is current model is valid
func (input *UpdateTenantSettings) Validate(user *models.User, services *app.Services) *validate.Result {
result := validate.Success()
if services.Tenants.Current() != nil {
input.Model.Logo.BlobKey = services.Tenants.Current().LogoBlobKey
}
messages, err := validate.ImageUpload(input.Model.Logo, validate.ImageUploadOpts{
IsRequired: false,
MinHeight: 200,
MinWidth: 200,
MaxKilobytes: 100,
ExactRatio: true,
})
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("logo", messages...)
if input.Model.Title == "" {
result.AddFieldFailure("title", "Title is required.")
}
if len(input.Model.Title) > 60 {
result.AddFieldFailure("title", "Title must have less than 60 characters.")
}
if len(input.Model.Invitation) > 60 {
result.AddFieldFailure("invitation", "Invitation must have less than 60 characters.")
}
if input.Model.CNAME != "" {
messages := validate.CNAME(services.Tenants, input.Model.CNAME)
result.AddFieldFailure("cname", messages...)
}
return result
}
//UpdateTenantAdvancedSettings is the input model used to update tenant advanced settings
type UpdateTenantAdvancedSettings struct {
Model *models.UpdateTenantAdvancedSettings
}
// Initialize the model
func (input *UpdateTenantAdvancedSettings) Initialize() interface{} {
input.Model = new(models.UpdateTenantAdvancedSettings)
return input.Model
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *UpdateTenantAdvancedSettings) IsAuthorized(user *models.User, services *app.Services) bool {
return user != nil && user.Role == models.RoleAdministrator
}
// Validate is current model is valid
func (input *UpdateTenantAdvancedSettings) Validate(user *models.User, services *app.Services) *validate.Result {
return validate.Success()
}
//UpdateTenantPrivacy is the input model used to update tenant privacy settings
type UpdateTenantPrivacy struct {
Model *models.UpdateTenantPrivacy
}
// Initialize the model
func (input *UpdateTenantPrivacy) Initialize() interface{} {
input.Model = new(models.UpdateTenantPrivacy)
return input.Model
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *UpdateTenantPrivacy) IsAuthorized(user *models.User, services *app.Services) bool {
return user != nil && user.Role == models.RoleAdministrator
}
// Validate is current model is valid
func (input *UpdateTenantPrivacy) Validate(user *models.User, services *app.Services) *validate.Result {
return validate.Success()
}