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

[Feature] Changing targeted tablename after newAdaptorByDb() #110

Closed
normangoh opened this issue Jun 9, 2021 · 7 comments · Fixed by #111
Closed

[Feature] Changing targeted tablename after newAdaptorByDb() #110

normangoh opened this issue Jun 9, 2021 · 7 comments · Fixed by #111

Comments

@normangoh
Copy link

Want to prioritize this issue? Try:

issuehunt-to-marktext


Is your feature request related to a problem? Please describe.
I am trying to build a SAAS and I would need to check permission from different schema depending on the request. Currently, I think that Casbin does not support changing schema when reading from the DB.

Describe the solution you'd like
It will be great if we can first create an adaptor with something like newAdaptorByDb() and then switch the tablename afterwards. So we can use something like schema1.casbin_rule or schema2.casbin_rule if we need to read permissions from different schema and this feature will not require a new adaptor creation.

Describe alternatives you've considered
None.

Additional context
None.

@normangoh normangoh changed the title [Feature] Changing targetted tablename after newAdaptorByDb() [Feature] Changing targeted tablename after newAdaptorByDb() Jun 9, 2021
@hsluoyz
Copy link
Member

hsluoyz commented Jun 9, 2021

@normangoh use NewAdapterByDBUseTableName(), see: #74

@hsluoyz
Copy link
Member

hsluoyz commented Jun 9, 2021

@normangoh replied:

I just tried using NewAdapterByDBUserTableName() but it does not work with periods, since I am target another schema I need to use schema1.casbin_rule and schema2.casbin_rule for example.

@hsluoyz
Copy link
Member

hsluoyz commented Jun 9, 2021

@closetool

@kilosonc
Copy link
Contributor

kilosonc commented Jun 9, 2021

@normangoh It works. If you pass in different schema by db (func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tableName string) (*Adapter, error))
If this doesn't meet your requirement, plz try this one

// NewAdapter is the constructor for Adapter.
, just use different database name.

@normangoh
Copy link
Author

@closetool I already tried using the func you mentioned and it does not work with something like the following.

NewAdapterByDBUseTableName(db, '', 'schema1.casbin_rule') (*Adapter, error))

Regarding the alternative solution you mentioned, will that requires a new connection to be setup? Since I am planning to use it for a SAAS app I can foresee a multiple requests on going. If the app creates a connection everytime they need to read from another schema I think that it will not be very ideal.

I am looking for a solution that does not need to create a new connection and let me read from another table in another schema.

@kilosonc
Copy link
Contributor

kilosonc commented Jun 9, 2021

@normangoh I found something helpful in grom's doc, https://gorm.io/docs/dbresolver.html, it seems the resolver keeps multiple connections either. If you still need this feature, I could make a pr for you.

@github-actions
Copy link

🎉 This issue has been resolved in version 3.3.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

3 participants