official-burak commented on issue #1702:
URL: https://github.com/apache/casbin/issues/1702#issuecomment-5440737150
Casbin does not wrap your business tables. gorm-adapter can share the same
GORM transaction as the role update.
Call `TurnOffAutoMigrate` once on the process db, then hand that `tx` to the
adapter:
```go
gormadapter.TurnOffAutoMigrate(db)
err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&User{}).Where("id = ?", id).Update("role",
role).Error; err != nil {
return err
}
a, err := gormadapter.NewAdapterByDB(tx)
if err != nil {
return err
}
orig := e.GetAdapter()
e.SetAdapter(a)
defer e.SetAdapter(orig)
_, err = e.AddGroupingPolicy(user, role)
return err
})
```
Keep AutoSave on and use `AddGroupingPolicy` / `RemoveGroupingPolicy`.
`SavePolicy` rewrites the whole `casbin_rule` table.
`Adapter.Transaction` and `NewTransactionalEnforcer` + `WithTransaction`
only cover policy rows. They do not include the user row unless you pass the
same `tx` as above.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]