問題描述

我很確定我瞭解 WordPress 中的角色和功能設置:細粒度的功能,可以分配給可以分配給用户的角色。代碼應該檢查粒度能力,而不是角色 (因為特定角色的功能可以改變) 。角色不一定是分層的 (儘管默認角色是) 。

有沒有辦法為用户分配多個角色?或者,具有多個能力組,並且將一個或多個組與用户相關聯?我的網站的工作方式,有很多明顯的職責:更新網頁,主持論壇,更新活動日曆等。每個職責都有一組能力來執行與之相關的任務。我想讓用户執行一個或多個職責。因此,用户 A 可以更新網頁和事件日曆,但不能適應論壇 (不夠巧妙),但用户 B 可以適度的論壇,更新活動日曆,但不允許在網頁附近。

沒有為每個可能的責任組合確定一個角色,有沒有辦法這樣做?

最佳解決方案

由於底層的 WP_User 類支持多個角色,缺少多個角色已經使我很長時間了。我甚至考慮尋找替代軟件解決方案。 @lpryor – 閲讀你的帖子後,我是 re-motivated 自己實現的。

儘管我不得不劫持 users.php 文件,但由於我太懶了,無法為我創建一個單獨的插件,所以我花費了驚人的短線。顯然,這是錯誤的做法,所以如果我今後有足夠的動力,我可能會嘗試做到這一點。

如果您不關心能夠升級到最新版本的 Wordpress(您應該) – 您可以使用下面的代碼段來實現多個角色。請記住,我不是一個 wordpress 專家。我剛剛打開相關文件並進行了更改,而不試圖瞭解我正在做的全部影響。這段代碼對我來説似乎是合理的,但我不會相信我的生活。

(我使用 3.2,所以你的行號可能會有所不同) 在 class-wp-users-list-table.php 之前的行 150 添加一些如下:

<div class="alignleft actions">
    <label class="screen-reader-text" for="remove_role"><?php _e( 'Remove role &hellip;' ) ?></label>
    <select name="remove_role" id="remove_role">
        <option value=''><?php _e( 'Remove role &hellip;' ) ?></option>
        <?php wp_dropdown_roles(); ?>
    </select>
    <?php submit_button( __( 'Remove' ), 'secondary', 'changeit', false ); ?>
</div>

然後更改 current_account 功能看起來像這樣

function current_action() {
    if ( isset($_REQUEST['changeit']) ) {
        if ( !empty($_REQUEST['new_role']) )
            return 'promote';
        elseif ( !empty($_REQUEST['remove_role']) )
            return 'remove_role';
    }

    return parent::current_action();

}

現在在 users.php 註釋線 71-76

/*
if ( $id == $current_user->ID && !$wp_roles->role_objects[$_REQUEST['new_role']]->has_cap('promote_users') ) {
    $update = 'err_admin_role';
    continue;
}
*/

用 add_role 替換第 83 行中的 set_role

$user->add_role($_REQUEST['new_role']);

在第 92 行添加以下內容 (這只是從促銷操作中輕輕編輯的複製和粘貼 – 我還沒有檢查確保 promote_user 功能適合刪除角色)

case 'remove_role':
    check_admin_referer('bulk-users');

    if ( ! current_user_can( 'promote_users' ) )
            wp_die( __( 'You can’t edit that user.' ) );

    if ( empty($_REQUEST['users']) ) {
            wp_redirect($redirect);
            exit();
    }

    $editable_roles = get_editable_roles();
    if ( empty( $editable_roles[$_REQUEST['remove_role']] ) )
            wp_die(__('You can’t remove that role'));

    $userids = $_REQUEST['users'];
    $update = 'remove_role';
    foreach ( $userids as $id ) {
            $id = (int) $id;

            if ( ! current_user_can('promote_user', $id) )
                    wp_die(__('You can’t edit that user.'));
            // The new role of the current user must also have promote_users caps
            // Need to think this through
            /*
            if ( $id == $current_user->ID && !$wp_roles->role_objects[$_REQUEST['new_role']]->has_cap('promote_users') ) {
                    $update = 'err_admin_role';
                    continue;
            }
            */

            // If the user doesn't already belong to the blog, bail.
            if ( is_multisite() && !is_user_member_of_blog( $id ) )
                    wp_die(__('Cheatin’ uh?'));

            $user = new WP_User($id);
            $user->remove_role($_REQUEST['remove_role']);
    }

    wp_redirect(add_query_arg('update', $update, $redirect));
    exit();

在第 370 行添加以下內容

case 'remove_role':
    $messages[] = '<div id="message" class="updated"><p>' . __('Removed role.') . '</p></div>';
    break;

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。