Drupal custom node Permissions

Source: Internet
Author: User
Tags drupal

Use hook_node_access_records and hook_node_grants (graphical combination (personal understanding is not authoritative, for reference only) to replace hook_node_access


1. First, we will introduce hook_node_access. It uses code writing to determine whether the user has the permission to access, edit, and delete a node, as shown below:

function modulename_node_access($node, $op, $account) {

// Here, the following results are returned based on your conditions.

    return NODE_ACCESS_IGNORE;

<pre name="code" class="php">    return NODE_ACCESS_DENY;
 
 

    return NODE_ACCESS_ALLOW;

}



2. Use the graphical combination of hook_node_access_records and hook_node_grants.

First, declare the hook_node_grants hook to be called in the following three cases of node, and add the corresponding records to the node_access Table, as follows:

The three functions are in node. module.

Node_save

Node_access_rebuild contains a batch (_ node_access_rebuild_batch_operation)

function modulename_node_access_records($node) {

    $grants[] = array(

      'realm' => 'node_access_custom_edit',

      'gid' => $gid,

      'grant_view' => 1,

      'grant_update' => 1,

      'grant_delete' => 1,

      'priority' => 0,

    );

</pre><p></p><p></p><pre name="code" class="php">      $grants[] = array(

        'realm' => 'node_access_custom_author',

        'gid' => $node->uid,

        'grant_view' => 1,

        'grant_update' => 1,

        'grant_delete' => 1,

        'priority' => 0,

      );

    return $grants;

}


Second, permission comparison:

Use this function in hook_node_access

Module_implements ('node _ grants') implements all node_grants as follows:

Function node_access_example_node_grants ($ account, $ OP ){

$ Grants = array ();

// First grant a grant to the author for own content.

// Do not grant to anonymous user else all anonymous users wocould be author.

If ($ account-> UID ){

$ Grants ['node _ access_example_author '] = array ($ account-> UID );

}

// Then, if "access any private content" is allowed to the account,

// Grant view, update, or Delete as necessary.

If ($ op = 'view' & user_access ('Access any private content', $ account )){

$ Grants ['node _ access_example_view '] = array (node_access_example_grant_all );

}

If ($ op = 'update' | $ op = 'delete') & user_access ('edit any private content', $ account )){

$ Grants ['node _ access_example_edit '] = array (node_access_example_grant_all );

}

Return $ grants;

}


Perform real GID as a group of multiple conditions for obtaining or. For details, see node_access function implementation,

For the rest, you just need to configure the graphic interface in the background.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.