Tfa handles plugin instantiation which allows for simple object setup but increases complexity when integrating with Drupal and dealing with context and plugin-specific dependencies. It would be simpler if plugins were instantiated during context setup and injected into the Tfa object.
TFA module will need to manage global configuration and handling fallbacks and can also instantiate default objects if not already done so by an alter hook.
An example of the current difficulties, TFA Basic's SMS plugin stores the mobile number as an account field. To use the number for sending the SMS theplugin can 1) directly query for the account field and its value in the object, 2) call a procedural function in tfa_basic that returns the number, or 3) have the number passed in as plugin context during instantiation. Option 3 is simpler, so TFA module needs to support a TFA Basic context alter doing SMS plugin instantiation.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 2327441-tfa-di-6.patch | 14.44 KB | coltrane |
| #2 | 2327441-tfa-di-2.patch | 10.16 KB | coltrane |
| #1 | 2327441-tfa-di.patch | 10.59 KB | coltrane |
Comments
Comment #1
coltraneHere's a in-progress patch for this. Tests are failing for some unclear reasons. Needs work.
Comment #2
coltraneGot it working.
Comment #3
coltraneNote for those implementing custom plugins with TFA: this change affects Tfa and TfaSetup instantiation. No longer do you pass in a class name, you pass in the object itself.
Before:
After:
Comment #4
coltrane#2 accomplishes the goal but doesn't completely handle the example use case. Perhaps plugin modules should instantiate objects themselves so that they have control over constructor arguments.
Comment #5
gregglesI didn't test this, but the changes look logical and seem like they'll make this easier to test.
Comment #6
coltraneThis patch changes tfa_get_process() from the approach of #2 to calling new tfa_get_plugin() function that will return a plugin object. tfa_get_plugin() can refer to TFA API implementations for a 'callback' function or the plugin name suffixed with '_create'. This allows modules providing TFA plugins to control the instantiation and thereby control arguments and dependencies.
For example of this see tfa_test_login_create() in tfa_test.module after applying this patch.
Comment #8
coltrane#6 committed.
### Usage and upgrade notes
Users of TFA Basic will see support for this via #2376633: Support new TFA context injection and in the next tagged release.
If you have custom TFA plugins you may not need to make any changes at all depending on your integration with the TFA process.
The only required changes are if you create the Tfa or TfaSetup objects yourself somehow (such as during plugin configuration or custom TFA flows). If so you'll need to change the arguments to these classes to be full plugin objects instead of plugin machine names.
Additionally, if you want to instantiate your plugins yourself (in order to manage dependencies) you can register a 'callback' function in your hook_tfa_api. This function will be called during TFA setup and expect to receive a TFA plugin object back.