Skip to content

Commit c2d044b

Browse files
committed
fixup! refactor: deduplicate / type license feature code
1 parent 355f3c0 commit c2d044b

File tree

2 files changed

+36
-70
lines changed

2 files changed

+36
-70
lines changed

codersdk/features.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ func (n FeatureName) Humanize() string {
3636
return strings.Title(strings.ReplaceAll(string(n), "_", " "))
3737
}
3838

39+
// AlwaysEnable returns if the feature is always enabled if entitled.
40+
// Warning: We don't know if we need this functionality.
41+
// This method may disappear at any time.
42+
func (n FeatureName) AlwaysEnable() bool {
43+
m := map[FeatureName]bool{
44+
FeatureMultipleGitAuth: true,
45+
FeatureExternalProvisionerDaemons: true,
46+
FeatureAppearance: true,
47+
}
48+
return m[n]
49+
}
50+
3951
var FeatureNames = []FeatureName{
4052
FeatureUserLimit,
4153
FeatureAuditLog,

enterprise/coderd/license/license.go

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -67,67 +67,31 @@ func Entitlements(
6767
// LicenseExpires we must be in grace period.
6868
entitlement = codersdk.EntitlementGracePeriod
6969
}
70-
if claims.Features.UserLimit > 0 {
71-
limit := claims.Features.UserLimit
72-
priorLimit := entitlements.Features[codersdk.FeatureUserLimit]
73-
if priorLimit.Limit != nil && *priorLimit.Limit > limit {
74-
limit = *priorLimit.Limit
75-
}
76-
entitlements.Features[codersdk.FeatureUserLimit] = codersdk.Feature{
77-
Enabled: true,
78-
Entitlement: entitlement,
79-
Limit: &limit,
80-
Actual: &activeUserCount,
81-
}
82-
}
83-
if claims.Features.AuditLog > 0 {
84-
entitlements.Features[codersdk.FeatureAuditLog] = codersdk.Feature{
85-
Entitlement: entitlement,
86-
Enabled: enablements[codersdk.FeatureAuditLog],
87-
}
88-
}
89-
if claims.Features.BrowserOnly > 0 {
90-
entitlements.Features[codersdk.FeatureBrowserOnly] = codersdk.Feature{
91-
Entitlement: entitlement,
92-
Enabled: enablements[codersdk.FeatureBrowserOnly],
93-
}
94-
}
95-
if claims.Features.SCIM > 0 {
96-
entitlements.Features[codersdk.FeatureSCIM] = codersdk.Feature{
97-
Entitlement: entitlement,
98-
Enabled: enablements[codersdk.FeatureSCIM],
99-
}
100-
}
101-
if claims.Features.HighAvailability > 0 {
102-
entitlements.Features[codersdk.FeatureHighAvailability] = codersdk.Feature{
103-
Entitlement: entitlement,
104-
Enabled: enablements[codersdk.FeatureHighAvailability],
105-
}
106-
}
107-
if claims.Features.TemplateRBAC > 0 {
108-
entitlements.Features[codersdk.FeatureTemplateRBAC] = codersdk.Feature{
109-
Entitlement: entitlement,
110-
Enabled: enablements[codersdk.FeatureTemplateRBAC],
111-
}
112-
}
113-
if claims.Features.MultipleGitAuth > 0 {
114-
entitlements.Features[codersdk.FeatureMultipleGitAuth] = codersdk.Feature{
115-
Entitlement: entitlement,
116-
Enabled: true,
117-
}
118-
}
119-
if claims.Features.ExternalProvisionerDaemons > 0 {
120-
entitlements.Features[codersdk.FeatureExternalProvisionerDaemons] = codersdk.Feature{
121-
Entitlement: entitlement,
122-
Enabled: true,
123-
}
124-
}
125-
if claims.Features.Appearance > 0 {
126-
entitlements.Features[codersdk.FeatureAppearance] = codersdk.Feature{
127-
Entitlement: entitlement,
128-
Enabled: true,
70+
for featureName, featureValue := range claims.Features {
71+
switch featureName {
72+
// User limit has special treatment as our only non-boolean feature.
73+
case codersdk.FeatureUserLimit:
74+
limit := featureValue
75+
priorLimit := entitlements.Features[codersdk.FeatureUserLimit]
76+
if priorLimit.Limit != nil && *priorLimit.Limit > limit {
77+
limit = *priorLimit.Limit
78+
}
79+
entitlements.Features[codersdk.FeatureUserLimit] = codersdk.Feature{
80+
Enabled: true,
81+
Entitlement: entitlement,
82+
Limit: &limit,
83+
Actual: &activeUserCount,
84+
}
85+
default:
86+
if featureValue > 0 {
87+
entitlements.Features[featureName] = codersdk.Feature{
88+
Entitlement: entitlement,
89+
Enabled: enablements[featureName] || featureName.AlwaysEnable(),
90+
}
91+
}
12992
}
13093
}
94+
13195
if claims.AllFeatures {
13296
allFeatures = true
13397
}
@@ -248,17 +212,7 @@ var (
248212
ErrMissingLicenseExpires = xerrors.New("license missing license_expires")
249213
)
250214

251-
type Features struct {
252-
UserLimit int64 `json:"user_limit"`
253-
AuditLog int64 `json:"audit_log"`
254-
BrowserOnly int64 `json:"browser_only"`
255-
SCIM int64 `json:"scim"`
256-
TemplateRBAC int64 `json:"template_rbac"`
257-
HighAvailability int64 `json:"high_availability"`
258-
MultipleGitAuth int64 `json:"multiple_git_auth"`
259-
ExternalProvisionerDaemons int64 `json:"external_provisioner_daemons"`
260-
Appearance int64 `json:"appearance"`
261-
}
215+
type Features map[codersdk.FeatureName]int64
262216

263217
type Claims struct {
264218
jwt.RegisteredClaims

0 commit comments

Comments
 (0)