-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
282 lines (269 loc) · 5.84 KB
/
global.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package sqlite
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/imagvfx/forge"
)
func createGlobalsTable(tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE IF NOT EXISTS globals (
id INTEGER PRIMARY KEY,
entry_type_id INTEGER NOT NULL,
name STRING NOT NULL,
type STRING NOT NULL,
value STRING NOT NULL,
FOREIGN KEY (entry_type_id) REFERENCES entry_types (id),
UNIQUE (entry_type_id, name)
)
`)
if err != nil {
return err
}
_, err = tx.Exec(`CREATE INDEX IF NOT EXISTS index_globals_entry_type_id ON globals (entry_type_id)`)
return err
}
func FindGlobals(db *sql.DB, ctx context.Context, find forge.GlobalFinder) ([]*forge.Global, error) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
gls, err := findGlobals(tx, ctx, find)
if err != nil {
return nil, err
}
return gls, nil
}
func findGlobals(tx *sql.Tx, ctx context.Context, find forge.GlobalFinder) ([]*forge.Global, error) {
keys := make([]string, 0)
vals := make([]any, 0)
if find.EntryType != nil {
keys = append(keys, "entry_types.name=?")
vals = append(vals, *find.EntryType)
}
if find.Name != nil {
keys = append(keys, "globals.name=?")
vals = append(vals, *find.Name)
}
where := ""
if len(keys) != 0 {
where = "WHERE " + strings.Join(keys, " AND ")
}
rows, err := tx.QueryContext(ctx, `
SELECT
entry_types.name,
globals.name,
globals.type,
globals.value
FROM globals
LEFT JOIN entry_types ON globals.entry_type_id = entry_types.id
`+where,
vals...,
)
if err != nil {
return nil, err
}
defer rows.Close()
globals := make([]*forge.Global, 0)
for rows.Next() {
g := &forge.Global{}
err := rows.Scan(
&g.EntryType,
&g.Name,
&g.Type,
&g.Value,
)
if err != nil {
return nil, err
}
globals = append(globals, g)
}
return globals, nil
}
func GetGlobal(db *sql.DB, ctx context.Context, entryType, name string) (*forge.Global, error) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
g, err := getGlobal(tx, ctx, entryType, name)
if err != nil {
return nil, err
}
return g, nil
}
func getGlobal(tx *sql.Tx, ctx context.Context, entryType, name string) (*forge.Global, error) {
find := forge.GlobalFinder{
EntryType: &entryType,
Name: &name,
}
globals, err := findGlobals(tx, ctx, find)
if len(globals) == 0 {
return nil, forge.NotFound("global not found on %v: %v", entryType, name)
}
return globals[0], err
}
func AddGlobal(db *sql.DB, ctx context.Context, g *forge.Global) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
user := forge.UserNameFromContext(ctx)
if user == "" {
return forge.Unauthorized("context user unspecified")
}
yes, err := isAdmin(tx, ctx, user)
if err != nil {
return err
}
if !yes {
return forge.Unauthorized("user doesn't have permission to add global: %v", user)
}
err = addGlobal(tx, ctx, g)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
func addGlobal(tx *sql.Tx, ctx context.Context, g *forge.Global) error {
typeID, err := getEntryTypeID(tx, ctx, g.EntryType)
if err != nil {
return err
}
result, err := tx.ExecContext(ctx, `
INSERT INTO globals (
entry_type_id,
name,
type,
value
)
VALUES (?, ?, ?, ?)
`,
typeID,
g.Name,
g.Type,
g.Value,
)
if err != nil {
return err
}
id, err := result.LastInsertId()
if err != nil {
return err
}
g.ID = int(id)
return nil
}
func UpdateGlobal(db *sql.DB, ctx context.Context, upd forge.GlobalUpdater) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
user := forge.UserNameFromContext(ctx)
if user == "" {
return forge.Unauthorized("context user unspecified")
}
yes, err := isAdmin(tx, ctx, user)
if err != nil {
return err
}
if !yes {
return forge.Unauthorized("user doesn't have permission to update global: %v", user)
}
updateGlobal(tx, ctx, upd)
err = tx.Commit()
if err != nil {
return err
}
return nil
}
func updateGlobal(tx *sql.Tx, ctx context.Context, upd forge.GlobalUpdater) error {
keys := make([]string, 0)
vals := make([]any, 0)
if upd.Type != nil {
keys = append(keys, "type=?")
vals = append(vals, *upd.Type)
}
if upd.Value != nil {
keys = append(keys, "value=?")
vals = append(vals, *upd.Value)
}
if len(keys) == 0 {
return fmt.Errorf("need at least one field to update global: %v %v", upd.EntryType, upd.Name)
}
typeID, err := getEntryTypeID(tx, ctx, upd.EntryType)
if err != nil {
return err
}
vals = append(vals, typeID, upd.Name) // for where clause
_, err = tx.ExecContext(ctx, `
UPDATE globals
SET `+strings.Join(keys, ", ")+`
WHERE entry_type_id=? AND name=?
`,
vals...,
)
if err != nil {
return err
}
return nil
}
func DeleteGlobal(db *sql.DB, ctx context.Context, entryType, name string) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
user := forge.UserNameFromContext(ctx)
if user == "" {
return forge.Unauthorized("context user unspecified")
}
yes, err := isAdmin(tx, ctx, user)
if err != nil {
return err
}
if !yes {
return forge.Unauthorized("user doesn't have permission to delete default: %v", user)
}
err = deleteGlobal(tx, ctx, entryType, name)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
func deleteGlobal(tx *sql.Tx, ctx context.Context, entryType, name string) error {
typeID, err := getEntryTypeID(tx, ctx, entryType)
if err != nil {
return err
}
result, err := tx.ExecContext(ctx, `
DELETE FROM globals
WHERE entry_type_id=? AND name=?
`,
typeID,
name,
)
if err != nil {
return err
}
n, err := result.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return forge.NotFound("no such global for entry type %v: %v", entryType, name)
}
return nil
}