安装脚本中的自定义用户角色和 - 权限

时间:2011-11-03 13:53:40

标签: php drupal install drupal-7

是否可以在D7网站的.install文件中编写一些代码,以便您自动生成用户角色和权限?我总是如此,但是现在,我想不出办法来做到这一点 有什么建议?

1 个答案:

答案 0 :(得分:1)

绝对:

function mymodule_install() {
  // Make the new role
  $role = new stdClass;
  $role->name = 'new role name';
  $role->weight = 3;
  user_role_save($role);

  // Permissions to assign to the role.
  // Note these are defined in hook_permission()
  $perms = array(
    'access administration pages',
    'view content',
    'any other permission you want'
  );

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