-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpanelaccesscontrolrules.go
152 lines (117 loc) · 3.7 KB
/
panelaccesscontrolrules.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
package database
import (
"context"
_ "embed"
"errors"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type AccessControlAction string
const (
AccessControlActionAllow AccessControlAction = "allow"
AccessControlActionDeny AccessControlAction = "deny"
)
var ErrNoRuleMatched = errors.New("no rule matched")
type PanelAccessControlRule struct {
RoleId uint64 `json:"role_id,string"`
Action AccessControlAction `json:"action"`
}
type PanelAccessControlRules struct {
*pgxpool.Pool
}
func newPanelAccessControlRules(db *pgxpool.Pool) *PanelAccessControlRules {
return &PanelAccessControlRules{
db,
}
}
var (
//go:embed sql/panel_access_control_rules/schema.sql
panelAccessControlRulesSchema string
//go:embed sql/panel_access_control_rules/delete_rules.sql
panelAccessControlRulesDelete string
//go:embed sql/panel_access_control_rules/get_all.sql
panelAccessControlRulesGetAll string
//go:embed sql/panel_access_control_rules/get_all_for_guild.sql
panelAccessControlRulesGetAllForGuild string
//go:embed sql/panel_access_control_rules/get_first_matched.sql
panelAccessControlRulesGetFirstMatched string
//go:embed sql/panel_access_control_rules/insert.sql
panelAccessControlRulesInsert string
)
func (p PanelAccessControlRules) Schema() string {
return panelAccessControlRulesSchema
}
func (p *PanelAccessControlRules) GetAll(ctx context.Context, panelId int) ([]PanelAccessControlRule, error) {
rows, err := p.Query(ctx, panelAccessControlRulesGetAll, panelId)
if err != nil {
return nil, err
}
defer rows.Close()
rules := make([]PanelAccessControlRule, 0, 10)
for rows.Next() {
var rule PanelAccessControlRule
if err := rows.Scan(&rule.RoleId, &rule.Action); err != nil {
return nil, err
}
rules = append(rules, rule)
}
return rules[:], nil
}
// GetAllForGuild returns a map[panel_id][]rules
func (p *PanelAccessControlRules) GetAllForGuild(ctx context.Context, guildId uint64) (map[int][]PanelAccessControlRule, error) {
rows, err := p.Query(ctx, panelAccessControlRulesGetAllForGuild, guildId)
if err != nil {
return nil, err
}
defer rows.Close()
rules := make(map[int][]PanelAccessControlRule)
for rows.Next() {
var panelId int
var rule PanelAccessControlRule
if err := rows.Scan(&panelId, &rule.RoleId, &rule.Action); err != nil {
return nil, err
}
rules[panelId] = append(rules[panelId], rule)
}
return rules, nil
}
func (p *PanelAccessControlRules) GetFirstMatched(ctx context.Context, panelId int, userRoles []uint64) (uint64, AccessControlAction, error) {
idArray := &pgtype.Int8Array{}
if err := idArray.Set(userRoles); err != nil {
return 0, "", err
}
var roleId uint64
var action AccessControlAction
if err := p.QueryRow(ctx, panelAccessControlRulesGetFirstMatched, panelId, idArray).Scan(&roleId, &action); err != nil {
if err == pgx.ErrNoRows {
return 0, "", ErrNoRuleMatched
}
return 0, "", err
}
return roleId, action, nil
}
func (p *PanelAccessControlRules) Replace(ctx context.Context, panelId int, rules []PanelAccessControlRule) error {
tx, err := p.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
if err := p.ReplaceWithTx(ctx, tx, panelId, rules); err != nil {
return err
}
return tx.Commit(ctx)
}
func (p *PanelAccessControlRules) ReplaceWithTx(ctx context.Context, tx pgx.Tx, panelId int, rules []PanelAccessControlRule) error {
// Remove existing rules
if _, err := tx.Exec(ctx, panelAccessControlRulesDelete, panelId); err != nil {
return err
}
// Add each rule
for position, rule := range rules {
if _, err := tx.Exec(ctx, panelAccessControlRulesInsert, panelId, rule.RoleId, position, rule.Action); err != nil {
return err
}
}
return nil
}