配置具有相同权限的多个角色

时间:2011-11-07 08:21:26

标签: arrays drupal permissions

我有一个超过20个角色和权限的大型网站。但是,它始终具有相同的权限,但根据创建内容的人员,权限会有所不同......

所以我现在做的是:

    // Make the new role
    $role = new stdClass;
    $role->name = 'Redacteur 1';
    $role->weight = 3;
    user_role_save($role);

    // Permissions to assign to the role.
    // Note these are defined in hook_permission()
    $perms = array(
    'access content','access content overview'
    );

    // Grant the permissions. This function takes care of all necessary cache resets
    user_role_grant_permissions($role->rid, $perms);

    // Make the new role
    $role = new stdClass;
    $role->name = 'Redacteur 2';
    $role->weight = 3;
    user_role_save($role);

    // Permissions to assign to the role.
    // Note these are defined in hook_permission()
    $perms = array(
    'access content','access content overview'
    );

    // Grant the permissions. This function takes care of all necessary cache resets
    user_role_grant_permissions($role->rid, $perms);

是不是有办法用某种数组做这个,所以我最终没有1000行代码。当您想要更改权限中的某些内容时,您必须修改所有角色......这必须更容易。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以更改数据库中的字段。我希望我能帮助你更多,但我只做了一些小的调整。

答案 1 :(得分:0)

我写了一些默认函数来实现这个目标:

function _load_permission_settings($role_index = null) {
    // Blocks
    $perms['administer blocks'] =           array(0, 0, 0);
    // Comments
    $perms['administer comments'] =         array(0, 1, 0);
    $perms['access comments'] =             array(1, 1, 1);
    $perms['post comments'] =               array(1, 1, 1);
    $perms['skip comment approval'] =       array(1, 1, 1);
    $perms['edit own comments'] =           array(1, 1, 1);
        ...


function _create_users() {
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

    $roles = user_roles(true);

    $users[] = array('an', array(3));
    $users[] = array('ben', array(4, 6, 8));
    ...

function _set_roles_and_permissions() {
    // Enable default permissions for system roles.
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
    user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, _load_permission_settings(0));

    // Permissions to assign to the roles. (all Thema-related roles share the same permissions)
    $perms_eind = _load_permission_settings(1);
    $perms_red = _load_permission_settings(2);

    $user_roles = _load_thema_user_role_names('');
    foreach ($user_roles as $name) {
        // Role1
            $role = new stdClass;
        $role->name = 'Role 1 - ' . $name;
        user_role_save($role);
        user_role_grant_permissions($role->rid, $perms_eind);
        // Role2
        $role = new stdClass;
        $role->name = 'Role 2 - ' . $name;
        user_role_save($role);
        user_role_grant_permissions($role->rid, $perms_red);
    }
}