casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
678 stars 206 forks source link

[suggest]No physical deletion #193

Closed 2013rain closed 1 year ago

2013rain commented 1 year ago

I think it would be better to do this.

the table "casbin_rule" add column status

ALTER TABLE  `casbin_rule` 
ADD COLUMN `st` TINYINT(2) NULL COMMENT '1.valid;-1.invalid' AFTER `v7`;

the file "adapter.go" add one function like this:

// modifyPolicies  only modify the st (st=-1 invalid; st=1 valid).
func (a *Adapter) modifyPolicies(modifyType , sec , ptype string, rules [][]string) error {
        var modifyStr string
        fieldKey := []string{"ptyoe","v0","v1","v2","v3","v4","v5","v6","v7","st"}
        modifyStr +="INSERT INTO casbin_rule("+string.Join(fieldKey ,",")+") VALUES"
    placeholderVals :="("+strings.RightTrim(strings.Repeat(" ? ,", len(fieldKey )) , "," ) +") "
        stVal :="1"
       if modifyType=="delete" {
            stVal  = "-1"  
       }

    use_flag = 0,update_time = ?"
    var lines []interface{}
        var placeholderList []string
    for _, rule := range rules {
        line := a.savePolicyLine(ptype, rule)
                line.St = stVal
        lines = append(lines, line)
                placeholderList  = appent(placeholderList , placeholderVals)
    }
        modifyStr += strings.Join(placeholderList , ",")
        modifyStr += "ON DUPLICATE KEY UPDATE st=VALUES(st)"
        return   a.db.Exec(modifyStr , lines ...).Error
}

And modify the method "LoadPolicy":

// LoadPolicy loads policy from database.
func (a *Adapter) LoadPolicy(model model.Model) error {
    var lines []CasbinRule
    if err := a.db.Where("st=?", 1).Order("ID").Find(&lines).Error; err != nil {
        return err
    }
    for _, line := range lines {
        loadPolicyLine(line, model)
    }
    return nil
}

Because We can't find the previously deleted record.

casbin-bot commented 1 year ago

@tangyang9464 @JalinWang @imp2002

hsluoyz commented 1 year ago

@2013rain don't have plan to add the soft-delete feature in near future. You can fork it and add by your own