openid-connect-generic
openid-connect-generic copied to clipboard
Support ongoing update of user data from claims
The plugin supports mapping claim fields to certain WordPress core user fields, e.g. email and display name, when new users are created. However, as far as I can see this is the only circumstance where the claims data is mapped. When a user connects to an existing WordPress account, or subsequently logs in again however their WordPress account was first created, these fields are not updated.
It would be nice to have an optional checkbox in settings to enable continually mapping claim fields on login. This would enable going back through the mapping process for configured claim fields every time the user logs in via the plugin.
This could also be accomplished with hooks, but of course that's a semi- to fully custom solution that can only be accomplished by a developer. I'm working with a client who would like to see the configurability of the plugin extended here. I'd be interested in contributing a PR for the feature, if it's something the plugin team would be interested in?
We have this problem too - any news on that front? The plugin appears to have code to support this, but it seems not be called.
I found a solution for updating firstname, lastname and email after each login (note: this is run always, so without a configuration toggle, could be added optionally).
In the "openid-connect-generic-client-wrapper.php"->register->...is_admin-If, add the following line to hook into the update request:
add_action( 'openid-connect-generic-update-user-using-current-claim', array( $client_wrapper, 'update_existing_user_fields' ), 99, 2);
The linked function would look like this:
public function update_existing_user_fields($user, $user_claim) {
$user->data->first_name = isset( $user_claim['given_name'] ) ? $user_claim['given_name'] : '';;
$user->data->last_name = isset( $user_claim['family_name'] ) ? $user_claim['family_name'] : '';
$user->data->user_email = isset( $user_claim['email'] ) ? $user_claim['email'] : '';
$id = $user->data->ID;
$user_name = $user->data->user_login;
wp_update_user($user);
$this->logger->log("User updated: " . $user_name . " (" . $id . ")", "update_existing_user_fields");
}