Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Customizing casbin_rule table name #74

Closed
arafat-java opened this issue Feb 3, 2021 · 3 comments
Closed

[Question] Customizing casbin_rule table name #74

arafat-java opened this issue Feb 3, 2021 · 3 comments
Assignees
Labels

Comments

@arafat-java
Copy link
Contributor

arafat-java commented Feb 3, 2021

What's your scenario? What do you want to achieve?
We are working on a cloud based offering and planning to use casbin.
In that we would like to segregate tenant's data and for that we would like to create separate casbin_rules tables for each tenant.
I know that we have the provision for seperate databases and that we can pass in the connection string
But we don't want to have that many databases.

So our requirement is that instead of the default table name casbin_rule how can we have that table name customized to something like tenant1_casbin_rule, tenant1_casbin_rule... tenantn_casbin_rule

@hsluoyz
Copy link
Member

hsluoyz commented Feb 3, 2021

@arafat-java what adapter do you use? Customizing casbin_rule table name is usually provided by each adapter instead of the main lib.

@arafat-java
Copy link
Contributor Author

We are using https://github.com/casbin/gorm-adapter

@hsluoyz hsluoyz transferred this issue from casbin/casbin Feb 3, 2021
@hsluoyz
Copy link
Member

hsluoyz commented Feb 3, 2021

@arafat-java see:

gorm-adapter/adapter.go

Lines 158 to 177 in d4ad5d9

// NewAdapterByDBUseTableName creates gorm-adapter by an existing Gorm instance and the specified table prefix and table name
// Example: gormadapter.NewAdapterByDBUseTableName(&db, "cms", "casbin") Automatically generate table name like this "cms_casbin"
func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tableName string) (*Adapter, error) {
if len(tableName) == 0 {
tableName = defaultTableName
}
a := &Adapter{
tablePrefix: prefix,
tableName: tableName,
}
a.db = db.Scopes(a.casbinRuleTable()).Session(&gorm.Session{Context: db.Statement.Context})
err := a.createTable()
if err != nil {
return nil, err
}
return a, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants