自定义插件-黑名单/白名单域电子邮件和更改用户角色-建议

时间:2021-08-24 作者:DIegoP

我有一个SSO Saml网站。我需要有一个基于域电子邮件的黑名单/白名单,检查完后,我需要通过在登录返回的专业/专业参数之间进行双重检查来更改角色。

我已经开发了一个插件来实现这一点。一切都很好,但我需要知道这是正确的方式还是更干净的方式。我是基于登录检查的,但我怀疑它的性能可能会很昂贵。(我认为每次加载页面时都会检查用户角色)。除了在wordpress注册过程中,是否有一些钩子可以做同样的事情?所以我只能做一次。

这是代码。我使用bboress论坛,所以每个用户有两个角色-感谢您的建议。

// 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= [\'test1@test.com\']; 
    $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 个回复
SO网友:vlood

我建议您处理所有用户一次(如果您的数据库很大,并且响应超时可能存在风险,则使用WP-CLI命令),然后在用户每次尝试更改其电子邮件时检查用户(使用WP\\u update\\u user filter-https://developer.wordpress.org/reference/functions/wp_update_user/).