允许新的用户角色对其他用户角色,而不是其自身类型的‘EDIT_OTHERS_POSTS

时间:2015-05-10 作者:Mayeenul Islam

在我的系统中,我将扮演两个新角色:

  • Third Party (TP) (较低权限)Data Entry Operator (DEO) (强大于第三方,但不属于自己的集团)我的CPT“能力”参数通过使用\'map_meta_cap\' => true 例如:

    ...
    \'capabilities\' => array ( \'read\' => \'read_cpt\', \'edit_posts\' => \'edit_cpt\' ),
    \'map_meta_cap\' => true
    ...
    
    Scenario is: TP 可以自由添加内容,但不能发布。DEOs也可以自由添加其内容DEO可以编辑/修改TP\',最终可以发布。但是(DEOs) 无法触及彼此的帖子中各自的角色。假设“X作为a”DEO\' 添加了一篇文章“Y作为”DEO\' 不能碰它。但是X和Y可以单独接触Z的柱子TP\'.

    在添加新角色的同时,我正在做:

    $tp = add_role(
           \'third_party\',
           __(\'Third Party\'),
           array(
               \'read_cpt\' => true,
               \'edit_cpt\' => true,
               //\'edit_others_cpt => false //by default not assigned
           )
    );
    
    $deo = add_role(
           \'data_entry_operator\',
           __(\'Data Entry Operator\'),
           array(
               \'read_cpt\' => true,
               \'edit_cpt\' => true,
               \'edit_others_cpt => true
           )
    );
    
    你知道,edit_others_cpt 将访问它们进行编辑TP\',但不会限制他们编辑自己用户角色的帖子(DEO).

    How can I let DEO \'edit_others_posts\' of TP role only, not of DEO?

1 个回复
SO网友:Riffaz Starr

首先向以下角色添加功能

add_action( \'after_setup_theme\', \'add_caps_to_custom_roles\' );
function add_caps_to_custom_roles() {
  $caps = array(
    \'read_cpt\',
    \'edit_cpt\',
    \'edit_others_cpt\',
  );
  $roles = array(
    get_role( \'third_party\' ),
    get_role( \'data_entry_operator\' ),
  );
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );
    }
  }
}
那么

/**
 * Helper function getting roles that the user is allowed to create/edit/delete \'TP\' post.
 *
 * @param   WP_User $user
 * @return  array
 */
function allowed_roles_to_edit_TP_post( $user ) {
    $allowed = array();

    if ( in_array( \'administrator\', $user->roles ) ) { // Admin can edit all roles post
        $allowed = array_keys( $GLOBALS[\'wp_roles\']->roles );
    } else ( in_array( \'data_entry_operator\', $user->roles ) ) {                
        $allowed[] = \'third_party\';
    }

    return $allowed;
}

/**
 * Remove roles that are not allowed for the current user role.
 */
function editable_roles( $roles ) {
    if ( $user = wp_get_current_user() ) {
        $allowed = allowed_roles_to_edit_TP_post( $user );

        foreach ( $roles as $role => $caps ) {
            if ( ! in_array( $role, $allowed ) )
                unset( $roles[ $role ] );
        }
    }

    return $roles;
}
add_filter( \'editable_roles\', \'editable_roles\' );

/**
 * Prevent users deleting/editing users with a role outside their allowance.
 */
function controll_map_meta_cap( $caps, $cap, $user_ID, $args ) {
    if ( ( $cap === \'read_cpt\' || $cap === \'edit_cpt\' || $cap === \'edit_others_cpt\' ) && $args ) {
        $the_user = get_userdata( $user_ID ); // The user performing the task
        $user     = get_userdata( $args[0] ); // The user being edited/deleted

        if ( $the_user && $user ) {
            $allowed = allowed_roles_to_edit_TP_post( $the_user );

            if ( array_diff( $user->roles, $allowed ) ) {
                // Target user has roles outside of our limits
                $caps[] = \'not_allowed\';
            }
        }
    }

    return $caps;
}
add_filter( \'map_meta_cap\', \'controll_map_meta_cap\', 10, 4 );

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?