Modify external DB authentication login script

Modify external DB authentication login script

by Albert R. -
Number of replies: 10

I'm currently using the external DB authentication to let users login into my Moodle 3.9 site. The thing is that I need to do an additional check in the external database in order to allow users to log in, so apart from checking username and password, I need to check the date they registered in that external DB (it's one of the fields in that table).

In theory, it would be as easy as modifying the user_login() function in the auth.php file, it would be a small change, but of course I'm aware that modifying the source code isn't a good idea.

So I would like to know what would be the best approach here. 

1. Create a new plugin from scratch that connects to my external database and handles the login flow?

2. Duplicate the external DB authentication plugin and just change the code that I need? If I go that route, what things should I take in consideration to not break thinks up?

3. Any other option that would make more sense in this particular situation?

Thanks so much in advance!

Average of ratings: -
In reply to Albert R.

Re: Modify external DB authentication login script

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

Hi,

Option 2 is best though alternatively you can build a dependant authentication plugin that extends the auth_plugin_db class and just overrides the user_login() function.

In reply to Benjamin Ellis

Re: Modify external DB authentication login script

by Albert R. -
Thank you for your reply Benjamin, what you propose seems to be definitely the best way to go. Any chance that you would point me in the right direction about building a dependant plugin? I can't seem to find information about that.

Thanks!
In reply to Albert R.

Re: Modify external DB authentication login script

by Vitaly Potenko -
Picture of Core developers Picture of Plugin developers
It seems there is a duplication of your forum question. See also my reply here - https://moodle.org/mod/forum/discuss.php?d=409544
In reply to Vitaly Potenko

Re: Modify external DB authentication login script

by Albert R. -
Hi Vitaly, thank you for your reply here as well, I created that other separate question because I thought it was a different one, more specifically about how to extend an existing plugin (if that is possible at all).
In reply to Albert R.

Re: Modify external DB authentication login script

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

I haven't looked deeply into it. But it feels to me you will still need a custom authentication plugin. Just do not copy what the DB authentication does. Instead, you should be able to call its API to perform its own checks, and then your auth plugin would perform the additional checks.

In reply to David Mudrák

Re: Modify external DB authentication login script

by Albert R. -
Thank you David for your reply. The thing is that the change that I need in the script is so small that building a whole new plugin seems a bit of an overkill, if it's the only way of course I will look into that, but I was hoping to find a more efficient way. Benjamin's idea of extending the DB plugin sounds ideal, but I'm not sure that's possible for what other comments have mentioned, unfortunately.
In reply to Albert R.

Re: Modify external DB authentication login script

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

Hi,

My suggestion is similar to copying the db authentication and renaming the plugin.  However going forward, you would have to maintain the code  to keep up with Moodle changes.  To create a new auth plugin, you will need at least 3 files - version.php, lang/en/<plugin>.php and auth.php.  In your auth.php file, require auth/db/auth.php and declare your auth_plugin_<plugin> class extending the auth_plugin_db class.  Then you can override the user_login function and if need be call the parent function if required.  You then only need to worry about Moodle API changes on upgrade if the specific needs for user_login() are changed.  A good idea to ensure you define the db auth plugin as a dependancy for your plugin in version.php.  NB:  This is only a suggestion to get a quick resolution.  



In reply to Benjamin Ellis

Re: Modify external DB authentication login script

by Albert R. -
Hi Benjamin, thank you very much for this explanation, I will definitely give it a go. Just one question: by using this method, I understand that both the External DB Auth plugin and my newly created plugin will need to be enabled, since the new will get the external DB configuration from the one it's extending, correct? Will this mean that during a login, Moodle will check both plugins for a correct login (in whatever order I set them up, I presume the new plugin would go first)? And wouldn't this cause the login system to basically ignore my new plugin? Since it would still check the user_login() function in the core External DB Auth plugin after checking the one in the new plugin.
In reply to Albert R.

Re: Modify external DB authentication login script

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

Hi,

The relevant users' profiles will have to be changed to the new authentication method.


Average of ratings: Useful (1)
In reply to Albert R.

Re: Modify external DB authentication login script

by Vitaly Potenko -
Picture of Core developers Picture of Plugin developers

I would proceed with the duplication of the core db auth plugin's settings. This will enable you to not mess up the core auth workflow and just disable the core db auth plugin and use only your own instead. But you'll have to duplicate some more code of the core plugin which handles the settings.

Average of ratings: Useful (1)