Hook perm适用于多种内容类型

And*_*rew 1 drupal drupal-hooks drupal-permissions drupal-content-types

Drupal 6.x

我有这个模块管理四种不同的内容类型.就此而言,如何为同一模块中的每个内容定义权限?这甚至可能吗?我无法弄清楚如何为每个内容类型定义权限cuz hook_perm必须使用模块名称命名,并且它没有任何参数(如hook_access $ node)来返回内容类型的权限.这是我想做的 -

function mymodule_perm() 
{
if(content1)    
return array(
    'create content1 node',
    'edit content1 nodes',
    'delete content1 nodes',
);
if(content2)    
return array(
    'create content2 node',
    'edit content2 nodes',
    'delete content2 nodes',
);
if(content3)    
return array(
    'create content3 node',
    'edit content3 nodes',
    'delete content3 nodes',
);
.......
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.

Hen*_*pel 6

通常,您不需要自己为内容类型创建权限,因为节点模块会为您执行此操作node_perm().对于您声明的每种内容类型hook_node_info(),节点模块将自动创建一组固定的权限,如下所示:

  $perms[] = 'create '. $name .' content';
  $perms[] = 'delete own '. $name .' content';
  $perms[] = 'delete any '. $name .' content';
  $perms[] = 'edit own '. $name .' content';
  $perms[] = 'edit any '. $name .' content';
Run Code Online (Sandbox Code Playgroud)

除此之外,您可以在模块hook_perm()实现中声明任意数量的其他权限(只要它们是唯一的),并根据需要在代码中使用这些权限.

这里重要的是,权限本身并不多 - 它只是一个将显示在权限页面上的名称,允许将其归因于角色.它们只有通过user_access()调用使用它们的代码才会变得"有意义" .

因此,例如,如果您想自己为每个内容类型创建一个特殊的新权限,那么您只需hook_perm()一次性声明它们(因此您不需要任何参数 - 只需返回每个权限一个字符串即可喜欢创造).