开发者 | tbenyon |
---|---|
更新时间 | 2021年2月7日 20:01 |
捐献地址: | 去捐款 |
PHP版本: | 5.6.34 及以上 |
WordPress版本: | 5.6.1 |
版权: | MIT |
/wp-content/plugins/external-login
directory.To give an idea of whether this plugin does the job you need it to, here is the basic logic flow:
For ports that differ the standard 3306, add them to the end of the host separated by a colon eg: 'localhost:3306'.
Below is a list of the available hashing options. Within each there are examples of what the hashed string might look like.
Yes. It will require you to use a built in hook (exlog_hook_filter_authenticate_hash) that you can add to your functions.php file. Documentation on how to use this and other hooks can be found in the FAQ section.
Here is a full listing of possible fields and values.
$password
- the password that was typed in at the login screen$hashFromDatabase
- the hash stored in the database$username
- the username that was typed in in the login screen$externalUserData
- the rest of the data retrieved from the external database for the user that was found
Returning true
will authenticate the user and returning false
will treat them as unauthorised.function myExlogHashAuthenticator($password, $hashFromDatabase, $username, $externalUserData) { return password_verify($password, $hashFromDatabase); } add_filter('exlog_hook_filter_authenticate_hash', 'myExlogHashAuthenticator', 10, 4);
functions.php
file to add additional fields from the users
table on the external database as user meta data in WordPress.
Please note the use of the third parameter $rawResponse that returns all fields from the users table in the external
database.
`
/**
true
(or a string - see below regarding custom error messages) from this function it will prevent the
user logging in, and returning false
will bypass this exclusion.
For example, let's say your external users table had a field called expiry
which stored a date. In this example we
want to block users if they login after their expiry date.
Adding the following to your functions.php
would achieve this:
function myExlogCustomExcluder($userData) { return strtotime($userData['expiry']) < strtotime('now'); } add_filter('exlog_hook_filter_custom_should_exclude', 'myExlogCustomExcluder', 10, 1);
Alternatively if you provide a string the user will be blocked and the string will be used as the error for the user.
function myExlogCustomExcluder($userData) { return strtotime($userData['expiry']) < strtotime('now') ? 'Your account has expired' : false; } add_filter('exlog_hook_filter_custom_should_exclude', 'myExlogCustomExcluder', 10, 1);
The exlog_hook_action_authenticated
hook can be used to achieve this. It is only run if the user is authenticated
with the external database and not the local WordPress database.
If you are unfamiliar with where to put such code, at the top of your functions.php
file in your them folder is a
good place to start. My personal preference is to store this type of code in "must use plugins" but that is your own
decision. Alternatively you could use a plugin like Code Snippets
to add the code.
If the additional data you require is in the users table achiving your goal is slightly easier. Below is an example
where by there is a field in the external database called someExternalField
and we want to store that as user meta
data in WordPress under the name someKeyForUserMetaData
.
`
/**
update_user_meta
.
If the additional data you require is not in the users table of your external database you will have to write your own
query to access the data you require.
If you wish to fetch the connection details from the plugin, the below code snippet shows how you can use an External
Login function to get the connection details you require and also shows an example query:
`
// Uses the data provided to the plugin to create the database object and data required for a query
$db_data = exlog_get_external_db_instance_and_fields('mysql');
// A query of your choice
$query_string = "SELECT * FROM someTable";
$result = $db_data["db_instance"]->get_results($query_string, ARRAY_A);
`
You may need to pass the string "mssql" or "postgresql" instead of "mysql" depending on your database.
$roles
- the array of roles already mapped using the built in logic based on data set it the admin panel$username
- the username that was typed in the login screen$userData
- the data that was originally queried for the user
It is expected that you will return an array of roles.function myExlogRolesMapper($roles, $username, $userData) { array_push($roles, "everyoneRole"); return $roles; } add_filter('exlog_hook_filter_assign_roles', 'myExlogRolesMapper', 10, 3);
The below is a relatively complex example that shows how you could write your own query to fetch data and use that to add WordPress roles.
`
function myExlogRolesMapper($roles, $username, $userData) {
// Uses the data provided to the plugin to create the database object and data required for a query
$db_data = exlog_get_external_db_instance_and_fields();
// Start building a query to fetch the user
// This is the start of the query that you may want to use if you require additional data from your database
// It may well be that all the data you need is in the passed $userData
$query_string =
'SELECT *' .
// This is specifying the table specified in the settings panel, you can hard code these if you rather
' FROM ' . esc_sql($db_data["dbstructure_table"]) .
// This finds the correct user based on the username field set in the settings and the username that they typed in
' WHERE (' . esc_sql($db_data["dbstructure_username"]) . '="' . esc_sql($username) . '"';
if ($db_data["dbstructure_email"]) {
// Because the username they type in can be an e-mail, if you have set an e-mail field in the settings panel we will also try and find the user by e-mail
$query_string .= ' OR ' . esc_sql($db_data["dbstructure_email"]) . '="' . esc_sql($username) . '")';
} else {
$query_string .= ')';
}
// Use the above computed query actually fetch the data
$rows = $db_data["db_instance"]->get_results($query_string, ARRAY_A);
// Checking if a user was found
if ($rows && count($rows) > 0) {
$foundData = $rows[0];
// If the custom field in your database called 'myCustomRoleField' has 'editingKing' stored in it
if ($foundData['myCustomRoleField'] == 'editingKing') {
// Add the wordpress role 'editor' to the user
array_push($roles, "editor");
}
}
// return the array of roles as WordPress supports multiple roles in the backend even though their settings pane only shows one
return $roles;
}
add_filter('exlog_hook_filter_assign_roles', 'myExlogRolesMapper', 10, 3);
`
Get in contact on the support forum and we can discuss it :)