15
15
package xormadapter
16
16
17
17
import (
18
+ "context"
18
19
"errors"
19
20
"log"
20
21
"runtime"
@@ -271,9 +272,14 @@ func loadPolicyLine(line *CasbinRule, model model.Model) {
271
272
272
273
// LoadPolicy loads policy from database.
273
274
func (a * Adapter ) LoadPolicy (model model.Model ) error {
275
+ return a .LoadPolicyCtx (context .Background (), model )
276
+ }
277
+
278
+ // LoadPolicyCtx loads policy from database.
279
+ func (a * Adapter ) LoadPolicyCtx (ctx context.Context , model model.Model ) error {
274
280
lines := make ([]* CasbinRule , 0 , 64 )
275
281
276
- if err := a .engine .Table (& CasbinRule {tableName : a .getFullTableName ()}).Find (& lines ); err != nil {
282
+ if err := a .engine .Context ( ctx ). Table (& CasbinRule {tableName : a .getFullTableName ()}).Find (& lines ); err != nil {
277
283
return err
278
284
}
279
285
@@ -312,6 +318,11 @@ func (a *Adapter) genPolicyLine(ptype string, rule []string) *CasbinRule {
312
318
313
319
// SavePolicy saves policy to database.
314
320
func (a * Adapter ) SavePolicy (model model.Model ) error {
321
+ return a .SavePolicyCtx (context .Background (), model )
322
+ }
323
+
324
+ // SavePolicyCtx saves policy to database.
325
+ func (a * Adapter ) SavePolicyCtx (ctx context.Context , model model.Model ) error {
315
326
err := a .dropTable ()
316
327
if err != nil {
317
328
return err
@@ -342,15 +353,20 @@ func (a *Adapter) SavePolicy(model model.Model) error {
342
353
return nil
343
354
}
344
355
345
- _ , err = a .engine .Insert (& lines )
356
+ _ , err = a .engine .Context ( ctx ). Insert (& lines )
346
357
347
358
return err
348
359
}
349
360
350
361
// AddPolicy adds a policy rule to the storage.
351
362
func (a * Adapter ) AddPolicy (sec string , ptype string , rule []string ) error {
363
+ return a .AddPolicyCtx (context .Background (), sec , ptype , rule )
364
+ }
365
+
366
+ // AddPolicyCtx adds a policy rule to the storage.
367
+ func (a * Adapter ) AddPolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) error {
352
368
line := a .genPolicyLine (ptype , rule )
353
- _ , err := a .engine .InsertOne (line )
369
+ _ , err := a .engine .Context ( ctx ). InsertOne (line )
354
370
return err
355
371
}
356
372
@@ -371,8 +387,13 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
371
387
372
388
// RemovePolicy removes a policy rule from the storage.
373
389
func (a * Adapter ) RemovePolicy (sec string , ptype string , rule []string ) error {
390
+ return a .RemovePolicyCtx (context .Background (), sec , ptype , rule )
391
+ }
392
+
393
+ // RemovePolicyCtx removes a policy rule from the storage.
394
+ func (a * Adapter ) RemovePolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) error {
374
395
line := a .genPolicyLine (ptype , rule )
375
- _ , err := a .engine .Delete (line )
396
+ _ , err := a .engine .Context ( ctx ). Delete (line )
376
397
return err
377
398
}
378
399
@@ -393,6 +414,11 @@ func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) err
393
414
394
415
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
395
416
func (a * Adapter ) RemoveFilteredPolicy (sec string , ptype string , fieldIndex int , fieldValues ... string ) error {
417
+ return a .RemoveFilteredPolicyCtx (context .Background (), sec , ptype , fieldIndex , fieldValues ... )
418
+ }
419
+
420
+ // RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage.
421
+ func (a * Adapter ) RemoveFilteredPolicyCtx (ctx context.Context , sec string , ptype string , fieldIndex int , fieldValues ... string ) error {
396
422
line := CasbinRule {Ptype : ptype , tableName : a .getFullTableName ()}
397
423
398
424
idx := fieldIndex + len (fieldValues )
@@ -415,7 +441,7 @@ func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
415
441
line .V5 = fieldValues [5 - fieldIndex ]
416
442
}
417
443
418
- _ , err := a .engine .Delete (& line )
444
+ _ , err := a .engine .Context ( ctx ). Delete (& line )
419
445
return err
420
446
}
421
447
0 commit comments