1515package xormadapter
1616
1717import (
18+ "context"
1819 "errors"
1920 "log"
2021 "runtime"
@@ -271,9 +272,14 @@ func loadPolicyLine(line *CasbinRule, model model.Model) {
271272
272273// LoadPolicy loads policy from database.
273274func (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 {
274280 lines := make ([]* CasbinRule , 0 , 64 )
275281
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 {
277283 return err
278284 }
279285
@@ -312,6 +318,11 @@ func (a *Adapter) genPolicyLine(ptype string, rule []string) *CasbinRule {
312318
313319// SavePolicy saves policy to database.
314320func (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 {
315326 err := a .dropTable ()
316327 if err != nil {
317328 return err
@@ -342,15 +353,20 @@ func (a *Adapter) SavePolicy(model model.Model) error {
342353 return nil
343354 }
344355
345- _ , err = a .engine .Insert (& lines )
356+ _ , err = a .engine .Context ( ctx ). Insert (& lines )
346357
347358 return err
348359}
349360
350361// AddPolicy adds a policy rule to the storage.
351362func (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 {
352368 line := a .genPolicyLine (ptype , rule )
353- _ , err := a .engine .InsertOne (line )
369+ _ , err := a .engine .Context ( ctx ). InsertOne (line )
354370 return err
355371}
356372
@@ -371,8 +387,13 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
371387
372388// RemovePolicy removes a policy rule from the storage.
373389func (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 {
374395 line := a .genPolicyLine (ptype , rule )
375- _ , err := a .engine .Delete (line )
396+ _ , err := a .engine .Context ( ctx ). Delete (line )
376397 return err
377398}
378399
@@ -393,6 +414,11 @@ func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) err
393414
394415// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
395416func (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 {
396422 line := CasbinRule {Ptype : ptype , tableName : a .getFullTableName ()}
397423
398424 idx := fieldIndex + len (fieldValues )
@@ -415,7 +441,7 @@ func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
415441 line .V5 = fieldValues [5 - fieldIndex ]
416442 }
417443
418- _ , err := a .engine .Delete (& line )
444+ _ , err := a .engine .Context ( ctx ). Delete (& line )
419445 return err
420446}
421447
0 commit comments