Commit e78d6f8f authored by Bobík's avatar Bobík
Browse files

Giving only setted grants.

parent 064e34bd
...@@ -34,14 +34,14 @@ function node_access_link_form_node_type_form_alter(&$form, &$form_state) { ...@@ -34,14 +34,14 @@ function node_access_link_form_node_type_form_alter(&$form, &$form_state) {
$form['node_access_link']['node_access_link_grants'] = array( $form['node_access_link']['node_access_link_grants'] = array(
'#type' => 'checkboxes', '#type' => 'checkboxes',
'#title' => t('Grants'), '#title' => t('Grants to give'),
'#default_value' => $default_grants, '#default_value' => $default_grants,
'#options' => array( '#options' => array(
'view' => t('View'), 'view' => t('View'),
'update' => t('Update'), 'update' => t('Update'),
'delete' => t('Delete'), 'delete' => t('Delete'),
), ),
'#description' => t('What operations will be temporarily given to authorised user for the node.'), '#description' => t('What operations will be temporarily given to authorised user for the node. This not affect users who is authorised yet.'),
); );
$form['#submit'][] = 'node_access_link_form_node_type_form_alter_submit'; $form['#submit'][] = 'node_access_link_form_node_type_form_alter_submit';
...@@ -53,11 +53,19 @@ function node_access_link_form_node_type_form_alter(&$form, &$form_state) { ...@@ -53,11 +53,19 @@ function node_access_link_form_node_type_form_alter(&$form, &$form_state) {
function node_access_link_form_node_type_form_alter_submit(&$form, &$form_state) { function node_access_link_form_node_type_form_alter_submit(&$form, &$form_state) {
$instance = field_info_instance('node', 'field_node_access_link_authkey', $form_state['values']['type']); $instance = field_info_instance('node', 'field_node_access_link_authkey', $form_state['values']['type']);
if(!$instance && $form_state['values']['node_access_link_enable']) { // Instance does not exist and checkbox is enabled // Instance does not exist and checkbox is enabled - create filed and instance
node_access_link_create_field_instance($form_state['values']['type']); if(!$instance && $form_state['values']['node_access_link_enable']) {
node_access_link_create_field_instance($form_state['values']['type'], $form_state['values']['node_access_link_grants']);
} }
elseif($instance && !$form_state['values']['node_access_link_enable']) { // Instance exists and checkbox is disabled // Instance exists and checkbox is enabled - update instance
elseif($instance && $form_state['values']['node_access_link_enable']) {
$instance['node_access_link_grants'] = $form_state['values']['node_access_link_grants'];
field_update_instance($instance);
}
// Instance exists and checkbox is disabled - delete filed and instance
elseif($instance && !$form_state['values']['node_access_link_enable']) {
field_delete_instance($instance); field_delete_instance($instance);
} }
...@@ -67,7 +75,7 @@ function node_access_link_form_node_type_form_alter_submit(&$form, &$form_state) ...@@ -67,7 +75,7 @@ function node_access_link_form_node_type_form_alter_submit(&$form, &$form_state)
/** /**
* Create field instance in specified node type. * Create field instance in specified node type.
*/ */
function node_access_link_create_field_instance($node_type) { function node_access_link_create_field_instance($node_type, $grants) {
if(!field_info_field('field_node_access_link_authkey')) { if(!field_info_field('field_node_access_link_authkey')) {
$field = array( $field = array(
'translatable' => '1', 'translatable' => '1',
...@@ -124,6 +132,7 @@ function node_access_link_create_field_instance($node_type) { ...@@ -124,6 +132,7 @@ function node_access_link_create_field_instance($node_type) {
'field_name' => 'field_node_access_link_authkey', 'field_name' => 'field_node_access_link_authkey',
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => $node_type, 'bundle' => $node_type,
'node_access_link_grants' => $grants,
); );
field_create_instance($instance); field_create_instance($instance);
} }
...@@ -136,20 +145,28 @@ function node_access_link_node_access($node, $op, $account) { ...@@ -136,20 +145,28 @@ function node_access_link_node_access($node, $op, $account) {
if($op == 'create') if($op == 'create')
return NODE_ACCESS_IGNORE; return NODE_ACCESS_IGNORE;
// Check key // Check key if:
if(isset($_GET['authkey']) && isset($node->field_node_access_link_authkey)) { if(isset($_GET['authkey']) && // authkey param is set
isset($node->field_node_access_link_authkey) && // authkey in node is set
!isset($_SESSION['node_access_link_auth_nodes'][$node->nid])) { // grants has not loaded yet
$authkeys = field_get_items('node', $node, 'field_node_access_link_authkey'); $authkeys = field_get_items('node', $node, 'field_node_access_link_authkey');
if($authkeys[0]['value'] == $_GET['authkey']) { if($authkeys[0]['value'] == $_GET['authkey']) {
// Start session // Start session
if(!isset($_SESSION)) if(!isset($_SESSION))
drupal_session_initialize(); drupal_session_initialize();
// Load settings
$instance = field_info_instance('node', 'field_node_access_link_authkey', $node->type);
// Save authorization to session // Save authorization to session
$_SESSION['node_access_link_auth_nodes'][$node->nid] = TRUE; $_SESSION['node_access_link_auth_nodes'][$node->nid] = $instance['node_access_link_grants'];
} }
} }
// Permit if checked // Permit if checked
if(isset($_SESSION['node_access_link_auth_nodes'][$node->nid])) if(isset($_SESSION['node_access_link_auth_nodes'][$node->nid]) &&
$_SESSION['node_access_link_auth_nodes'][$node->nid][$op])
return NODE_ACCESS_ALLOW; return NODE_ACCESS_ALLOW;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment