0

i've a website with SSO Saml. I need to have a blacklist/whitelist based on domain email and after this is checked i need to have a change role by double check between profession/specialty parameters that return from login.

I've developped a plugin to do so. Everything works fine but i need to know if it'sthe right way or it could be a cleaner way. I've based it on login check, but i have the doubt that it can be expensive in performance. (i think every time a page load it check user role). Are there some hooks to do the same thing but in wordpress registration process? So i can do it only once.

Here's the code. I use bboress forum so i have two roles per user -Thanks for any suggestions.

// USER ROLE RETURN 
function get_user_role($user_id) {
    global $wp_roles;

    $roles = array();
    $user = new WP_User( $user_id );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )
        $roles[] .= translate_user_role( $role );
    }
    return implode(', ',$roles);   
} 
// END USER ROLE

/*
Case 1 - user has blacklist email domain - wp role 'restircted', bbpress role 'blocked'
Case 2 - user has whitelist email - wp role 'editor', bbpress role 'bbpress_participant'
Case 3 - user has one profession and one of three specialty - wp role 'role3' , bbpress role 'bbpress_participant'
Case 4 - user has no blacklist, whitelist or prefession/specialty check - WP role and bbpress role assigned automatically by Wordpress. 
*/

// LOGIN FUNCTIONS
//Get user role and Filter Blacklist Whitelist with array
function loginUserRole() {
    $current_user = wp_get_current_user();
    $user_email = $current_user ->user_email;
    $user_id = get_current_user_id();
    $user_role = get_user_role( $user_id );
    $user_profession= get_field('profession' , 'user_'. $user_id );
    $user_specialty= get_field('specialty' , 'user_'. $user_id );

    if (!is_admin() AND is_user_logged_in() AND ($user_role != 'administrator, bbp_keymaster')) {

    $blacklist = ['@test.com'];
    $whitelist= ['[email protected]']; 
    $trovato_blacklist=false;
    $trovato_whitelist=false;
    
    foreach ($blacklist as $black) {     
        $find_email_black = strpos($user_email, $black);
        
        if ($find_email_black !== false){
            $trovato_blacklist=true; 
        } 
    } 

    if ($trovato_blacklist) {
        // cambia il ruolo nella blacklist
        $u = new WP_User( $user_id );
        $u->set_role('restricted');
        $u->add_role('bbp_blocked');
    
        foreach ($whitelist as $white) {     
            $find_email_white = strpos($user_email, $white);
            
            if ($find_email_white !== false){
                $trovato_whitelist=true; 
            } else { echo $find_email_white; }
            } 
                if ($trovato_whitelist) {
                // change user role for Whitelist
                $u = new WP_User( $user_id );
                
                $u->set_role('editor');
                $u->add_role('bbp_participant');
                }
    }
    // after Blacklist, Whitelist check user profession and change role 
    else {
        // check user profession
        if ($user_profession == 'profession' )
        {
            //check user specialty 
            $specialty = array(
                'specialty1','specialty2','specialty3'
            );

            if (in_array($user_specialty , $specialty ) ) 
            {
                //change user role for profession/specialty check
                $u = new WP_User( $user_id );
                $u->set_role('role3');
                $u->add_role('bbp_participant');
            }
            
          }
    
        }
     }
   }

   add_action('init', 'loginUserRole');

1 Answer 1

0

I'd suggest you process all users once (using a WP-CLI command if your db is big and response timeout is a possible risk) and then check a user each time they try to change their email (using wp_update_user filter - https://developer.wordpress.org/reference/functions/wp_update_user/).

2
  • @diegop, I know you didn't ask for this, but take it as a friendly advice - if you avoid using non-English language in variable names and code comments, and you apply the WP/PHP coding standards, you'll make your code a lot easier for others to read, understand and improve.
    – vlood
    Aug 24, 2021 at 15:48
  • Thanks for precious suggestion!!!!!
    – DIegoP
    Aug 26, 2021 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.