I'm using BjyAuthorize with Zend Framework2 to implement authorization and was able to successfully integrate roles from database. Now I want to get my Rules and Guards also from data base tables. How can I do this?
相关问题
- Why does recursive submodule update from github fa
- How to pass options/params to formCollection field
- Functional Test - Mock service does not persist in
- 'Zend_Db_Statement_Exception' with message
- Compress html output from zend framework 2
相关文章
- Symfony ONE-TO-ONE relation
- Doctrine 2 PlainValue expected
- Zend Framework 2 Forms Array notation for drop dow
- ManyToOne association mapping to a Class Table Inh
- Use spiffy navigation with zfcrbac module
- Migrating Existing Users and Passwords to new Symf
- Doctrine Dbal: How to set collation? (in Symfony2)
- Copy a Doctrine object with all relations
The easiest method and "the trick" here is really to:
Get your rules and guards into the same array format as it is shown in example configuration. So after reading records from the database, in whatever format your raw database data is, process it to match the same guard format as in the configuration. (My answer goes into detail on how to do that with Doctrine ORM, but also should give you an idea with other DB engines. Just substitute "DB read" operation with your fave database engine)
Inject the rules that are already in the proper format BjyAuthorize expects (because you made them so), into
BjyAuthorize\Guard\Controller
, from withinYOUR_MODULE_NAME\Factory\DoctrineControllerGuardAdapterFactory
, which you will write. Bjy's Controller will treat the rules as if those rules came from configuration*, and not suspect any difference.Step back and enjoy!
This is the construct that you need to write in your own module:
Now watch and observe how mind-numbingly complicated this can be made! (modeled after Bjy's own mechanisms)
This is mostly just ZF2, OO & Bjy "Configuration Hell", folks, nothing special otherwise. Welcome to ZF2 and Bjy and ORM Configuration Hell. You are welcome.
Detailed Answer - How to Implement?
Write an adapter factory, which reads rules from database, and then injects them into BjyAuthorize's Controller Guard. The effect will be the same as if the rules were being read from
['guards'][\BjyAuthorize\Guard\Controller::class]
What?
The way BjyAuthorize's Controller Guard works is it takes rules in a certain format (format specified for
['guards']['BjyAuthorize\Guard\Controller']
), and then it uses the rules to populate the ACL. It also computes Resources from rules for you and loads those into ACL as well. If it didn't, you would have to write your own Resource Provider to do so.So the task becomes:
['guards']['YOUR_MODULE_NAME_controller_guard_adapter']
.Example Implementation Details (from Q/A in comments)
More on the last point of "Injecting rules into Controller". Basically two steps: 1) make sure you already have (or will) generate your rules somehow (that's the hard step ). 2) inject those rules into controller (that's the easier step). The actual injection is done like this
See code block below for my own implementation, where the last line in the block is the line I gave just above here.
DoctrineRuleProvider
Q: How and where exactly to register the factory
DoctrineControllerGuardAdapterFactory
A: Try this path:
module\YOUR_MODULE_NAME\config\module.config.php
and haveYOUR_MODULE_NAME
. The thing on the left of=>
sign is "the key", and can be anything you want it to be. Convention in Bjy is that it is similar to the actual class names and paths. And the thing on the right of the=>
is the actual fully qualified namespace to the class that you want to call with with this key.Basically you have to write your own
Provider
.Check out the different
RoleProvider
. EveryRoleProvider
implements theProvider\Role\ProviderInterface
. The same thing has to be done when you want to implement Guards and Rules. You go into the specific directoriesProvider\Rule
andProvider\Resource
and check for the specificProviderInterface
.That way you can write your own class implementing the Interface and then via configuration you tell BjyAuthorize to use your provider-classes.
As far as Guards are concerned, i do believe it is not yet possible to create those from Database. You would have to modify / PR the Module itself to make that happen.