@@ -486,3 +486,119 @@ func (a *Adapter) UpdatePolicies(sec string, ptype string, oldRules, newRules []
486486
487487 return session .Commit ()
488488}
489+
490+ func (a * Adapter ) UpdateFilteredPolicies (sec string , ptype string , newPolicies [][]string , fieldIndex int , fieldValues ... string ) ([][]string , error ) {
491+ // UpdateFilteredPolicies deletes old rules and adds new rules.
492+ line := & CasbinRule {}
493+
494+ line .PType = ptype
495+ if fieldIndex <= 0 && 0 < fieldIndex + len (fieldValues ) {
496+ line .V0 = fieldValues [0 - fieldIndex ]
497+ }
498+ if fieldIndex <= 1 && 1 < fieldIndex + len (fieldValues ) {
499+ line .V1 = fieldValues [1 - fieldIndex ]
500+ }
501+ if fieldIndex <= 2 && 2 < fieldIndex + len (fieldValues ) {
502+ line .V2 = fieldValues [2 - fieldIndex ]
503+ }
504+ if fieldIndex <= 3 && 3 < fieldIndex + len (fieldValues ) {
505+ line .V3 = fieldValues [3 - fieldIndex ]
506+ }
507+ if fieldIndex <= 4 && 4 < fieldIndex + len (fieldValues ) {
508+ line .V4 = fieldValues [4 - fieldIndex ]
509+ }
510+ if fieldIndex <= 5 && 5 < fieldIndex + len (fieldValues ) {
511+ line .V5 = fieldValues [5 - fieldIndex ]
512+ }
513+
514+ newP := make ([]CasbinRule , 0 , len (newPolicies ))
515+ oldP := make ([]CasbinRule , 0 )
516+ for _ , newRule := range newPolicies {
517+ newP = append (newP , * a .genPolicyLine (ptype , newRule ))
518+ }
519+ tx := a .engine .NewSession ()
520+ defer tx .Close ()
521+
522+ if err := tx .Begin (); err != nil {
523+ return nil , err
524+ }
525+
526+ for i := range newP {
527+ str , args := line .queryString ()
528+ if err := tx .Where (str , args ... ).Find (& oldP ); err != nil {
529+ return nil , tx .Rollback ()
530+ }
531+ if _ , err := tx .Where (str .(string ), args ... ).Delete (CasbinRule {}); err != nil {
532+ return nil , tx .Rollback ()
533+ }
534+ if _ , err := tx .Insert (& newP [i ]); err != nil {
535+ return nil , tx .Rollback ()
536+ }
537+ }
538+
539+ // return deleted rulues
540+ oldPolicies := make ([][]string , 0 )
541+ for _ , v := range oldP {
542+ oldPolicy := v .toStringPolicy ()
543+ oldPolicies = append (oldPolicies , oldPolicy )
544+ }
545+ return oldPolicies , tx .Commit ()
546+ }
547+
548+ func (c * CasbinRule ) toStringPolicy () []string {
549+ policy := make ([]string , 0 )
550+ if c .PType != "" {
551+ policy = append (policy , c .PType )
552+ }
553+ if c .V0 != "" {
554+ policy = append (policy , c .V0 )
555+ }
556+ if c .V1 != "" {
557+ policy = append (policy , c .V1 )
558+ }
559+ if c .V2 != "" {
560+ policy = append (policy , c .V2 )
561+ }
562+ if c .V3 != "" {
563+ policy = append (policy , c .V3 )
564+ }
565+ if c .V4 != "" {
566+ policy = append (policy , c .V4 )
567+ }
568+ if c .V5 != "" {
569+ policy = append (policy , c .V5 )
570+ }
571+ return policy
572+ }
573+
574+ func (c * CasbinRule ) queryString () (interface {}, []interface {}) {
575+ queryArgs := []interface {}{c .PType }
576+
577+ queryStr := "p_type = ?"
578+ if c .V0 != "" {
579+ queryStr += " and v0 = ?"
580+ queryArgs = append (queryArgs , c .V0 )
581+ }
582+ if c .V1 != "" {
583+ queryStr += " and v1 = ?"
584+ queryArgs = append (queryArgs , c .V1 )
585+ }
586+ if c .V2 != "" {
587+ queryStr += " and v2 = ?"
588+ queryArgs = append (queryArgs , c .V2 )
589+ }
590+ if c .V3 != "" {
591+ queryStr += " and v3 = ?"
592+ queryArgs = append (queryArgs , c .V3 )
593+ }
594+ if c .V4 != "" {
595+ queryStr += " and v4 = ?"
596+ queryArgs = append (queryArgs , c .V4 )
597+ }
598+ if c .V5 != "" {
599+ queryStr += " and v5 = ?"
600+ queryArgs = append (queryArgs , c .V5 )
601+ }
602+
603+ return queryStr , queryArgs
604+ }
0 commit comments