I currently have a front end part of a WP site that allows admins to create new users and assign then user roles from a drop down – that works perfectly.
However, I am using a plugin to also authenticate users against an AD/LDAP so I can use it on a corporate level too.
The problem is when the user authenticates against the LDAP for the first time, there is no user assignment and no where in the plugin that I can see on how to do that (using the NextAD plugin).
I however saw that I can do custom checks on the authorisation when logging in, and thought I could add the role there:
function mb_authenticate_user( $user ) {
// get the user id from log in name
$mb_user_id = get_user_by('login', $_REQUEST['log'] );
$mb_user_id = $mb_user_id->ID;
// empty role on login
if( empty( mb_current_user_role() ) ) {
wp_update_user( array( 'ID' => $mb_user_id, 'role' => 'mbToBeAssigned' ) );
}
// other custom code checks here too
}
add_filter( 'wp_authenticate_user', 'mb_authenticate_user', 1 );
add_filter( 'authorize', 'mb_authenticate_user' ); // plugin authentication NextAD
However, in my mb_current_user_role()
function,
function mb_current_user_role( $echo = false ) {
$user="";
$user = ( is_user_logged_in() ? array_values( wp_get_current_user()->roles ) : null );
$user = $user[0];
if( $echo ) {
echo $user;
} else {
return $user;
}
}
it is not correctly checking if the user has exisiting roles and is constantly overriding them with the authentication one: mbToBeAssigned
Is there something I’m missing? Or is there a better way to do this? Running on a multisite too.