-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_plan.go
More file actions
95 lines (85 loc) · 2.12 KB
/
Copy pathapp_plan.go
File metadata and controls
95 lines (85 loc) · 2.12 KB
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
package main
import (
"errors"
"time"
"futrixdata/platform/internal/auth"
"futrixdata/platform/internal/planlimits"
)
func (a *App) currentPlan() (string, bool) {
if a == nil || a.authStore == nil {
return "", false
}
state := a.authStore.Current()
if state.Session == nil {
return planlimits.EffectivePlanWithTrial("", "", 0, trialExpiresAt(state), time.Now()), true
}
return effectivePlanForState(state, time.Now()), true
}
func effectivePlanForState(state auth.State, now time.Time) string {
if state.Session == nil {
return planlimits.EffectivePlanWithTrial("", "", 0, trialExpiresAt(state), now)
}
license := state.Session.License
return planlimits.EffectivePlanWithTrial(license.Plan, license.Status, license.ExpiresAt, trialExpiresAt(state), now)
}
func trialExpiresAt(state auth.State) int64 {
if state.Trial == nil {
return 0
}
return state.Trial.ExpiresAt
}
func (a *App) ensureDatasourceCreateAllowed() error {
check := a.datasourceCreateCheck()
if check == nil || a == nil || a.store == nil {
return nil
}
return check(len(a.store.List()))
}
func (a *App) datasourceCreateCheck() func(count int) error {
plan, ok := a.currentPlan()
if !ok || a == nil || a.store == nil {
return nil
}
limit := planlimits.DatasourceLimit(plan)
if limit <= 0 {
return nil
}
return func(count int) error {
if count >= limit {
return errors.New(planlimits.DatasourceLimitError(plan))
}
return nil
}
}
func (a *App) ensureCustomRiskRulesAllowed() error {
if a.riskRulesAuthenticated() {
return nil
}
plan, ok := a.currentPlan()
if ok && planlimits.PolicyManagementAllowed(plan) {
return nil
}
return auth.ErrLoginRequired
}
func (a *App) ensureBuiltinRiskRulesAllowed() error {
plan, ok := a.currentPlan()
if ok && planlimits.PolicyManagementAllowed(plan) {
return nil
}
if !a.riskRulesAuthenticated() {
return auth.ErrLoginRequired
}
if ok {
return errors.New(planlimits.CustomRiskRulesError(plan))
}
return nil
}
func (a *App) riskRulesAuthenticated() bool {
if a == nil || a.authStore == nil {
return false
}
if a.authStore.Current().Session == nil {
return false
}
return true
}