Comment restreindre les utilisateurs à afficher leur propre type de contenu personnalisé?
Question
Je développe un module personnalisé appelé « serviceentry » qui expose également un type de contenu personnalisé appelé « serviceentry ».
J'expose également les autorisations personnalisées comme:
function serviceentry_permission() {
return array(
'view own serviceentry content' => array(
'title' => t('view own serviceentry content'),
'description' => t('view own serviceentry content')
),
'view any serviceentry content' => array(
'title' => t('view any serviceentry content'),
'description' => t('view any serviceentry content')
),
'create serviceentry content' => array(
'title' => t('create serviceentry content'),
'description' => t('create serviceentry content')
),
'edit own serviceentry content' => array(
'title' => t('edit own serviceentry content'),
'description' => t('edit own serviceentry content')
),
'edit any serviceentry content' => array(
'title' => t('edit any serviceentry content'),
'description' => t('edit any serviceentry content')
),
'delete own serviceentry content' => array(
'title' => t('delete own serviceentry content'),
'description' => t('delete own serviceentry content')
),
'delete any serviceentry content' => array(
'title' => t('delete any serviceentry content'),
'description' => t('delete any serviceentry content')
),
'administer serviceentry' => array(
'title' => t('administer serviceentry'),
'description' => t('administer serviceentry')
),
);
}
Alors, je vérifie les autorisations en utilisant la fonction suivante.
function serviceentry_node_access($node, $op, $account) {
$type = is_string($node) ? $node : $node->type;
if (in_array($type, node_permissions_get_configured_types())) {
if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
return NODE_ACCESS_ALLOW;
}
if ($op == 'view') {
if (user_access('view any ' . $type . ' content', $account) || (user_access('view own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
}
}
// Returning nothing from this function would have the same effect.
return NODE_ACCESS_IGNORE;
}
Maintenant, je veux restreindre les utilisateurs à voir nœuds de serviceentry base qu'ils ont « voir propre contenu serviceentry » ou « voir tout contenu serviceentry » autorisations. Le code ci-dessus ne fonctionne pas comme Drupal ne passe pas $ op = « view » afin de limiter l'utilisateur. Si l'utilisateur a la permission « contenu d'accès », tous les nœuds sont par ailleurs pas montré. Je veux avoir un contrôle plus précis sur mon propre type de noeud. Comment faire?
La solution
Lorsque la mise en œuvre des déclarations hook_node_access()
NODE_ACCESS_IGNORE
, il ne nie pas l'accès à un nœud, mais il est de laisser d'autres modules décider si l'utilisateur a accès au nœud. Si aucun des modules d'application retourne hook_node_access()
NODE_ACCESS_DENY
, et aucun des modules d'application retourne hook_node_grants()
0 pour le noeud, l'utilisateur a accès au nœud.
Autres conseils
Vous pouvez faire la même chose par le menu administrateur (People -> Autorisations). Vous pouvez attribuer des autorisations par rôle. Peut-être que je ne comprends pas le doute.