You are here

simple_access.module in Simple Access 6.2

This module allows administrators to make nodes viewable by specific 'access groups'. Each access group can contain any number of roles. If a node is not assigned to any access groups, it will remain viewable by all users.

Database definition:

File

simple_access.module
View source
<?php

/**
 * @file
 * This module allows administrators to make nodes viewable by specific
 * 'access groups'. Each access group can contain any number of roles.
 * If a node is not assigned to any access groups, it will remain viewable 
 * by all users.
 *
 * Database definition:
 * @code
 * @endcode
 *
 */

/**
 * Implementation of hook_menu().
 */
function simple_access_menu() {
  $items['admin/user/simple_access'] = array(
    'title' => 'Access groups',
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_page_overview',
    ),
    'type' => MENU_NORMAL_ITEM,
    'description' => 'Manage groups of users for node-specific access control.',
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/simple_access/list'] = array(
    'title' => 'List',
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -8,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/simple_access/add'] = array(
    'title' => 'Add Group',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_group_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -6,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/simple_access/edit/%'] = array(
    'title' => 'Edit Group',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_group_form',
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/simple_access/delete/%'] = array(
    'title' => 'Delete Group',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_delete_group_confirm',
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/sa_profiles'] = array(
    'title' => 'Access profiles',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_profile_list',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_NORMAL_ITEM,
    'description' => 'Maintain access profiles',
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/sa_profiles/list'] = array(
    'title' => 'List',
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -9,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/sa_profiles/add'] = array(
    'title' => 'Add',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_profile_form',
      NULL,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/settings/simple_access'] = array(
    'title' => 'Simple Access',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_settings_page',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_NORMAL_ITEM,
    'description' => 'Configure which kinds of access (view, edit, delete) users with permission to use Simple Access can define for each node.',
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/sa_profiles/%/edit'] = array(
    'title' => 'Edit Profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_profile_form',
      3,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'simple_access.admin.inc',
  );
  $items['admin/user/sa_profiles/%/delete'] = array(
    'title' => 'Delete Profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simple_access_profile_delete_confirm',
      3,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'manage simple access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'simple_access.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function simple_access_perm() {
  return array(
    'manage simple access',
    'assign access to nodes',
    'assign profiles to profiles',
    'assign owner permissions',
  );
}

/**
 * Implementation of hook_nodeapi()
 */
function simple_access_nodeapi(&$node, $op) {
  switch ($op) {
    case 'prepare':

      // Only use the defaults if this is a new node.
      if (empty($node->nid)) {
        $defaults = variable_get('simple_access_' . $node->type, array(
          'simple_access' => array(),
          'simple_access_profiles' => array(),
          'simple_access_owner' => array(),
        ));
      }
      else {
        $defaults = array(
          'simple_access' => array(),
          'simple_access_profiles' => array(),
          'simple_access_owner' => array(),
        );
      }
      foreach ($defaults as $key => $value) {
        if (!isset($node->{$key})) {
          $node->{$key} = $value;
        }
      }
      break;
    case 'load':
      if ($row = db_fetch_array(db_query('SELECT no.sa_view, no.sa_update, no.sa_delete FROM {simple_access_owner} no WHERE no.nid = %d', $node->nid))) {
        $node->simple_access_owner = $row;
      }
      else {
        $node->simple_access_owner = array(
          'sa_view' => 0,
          'sa_update' => 0,
          'sa_delete' => 0,
        );
      }
      $extra = array();
      $result = db_query('SELECT na.gid, na.sa_view, na.sa_update, na.sa_delete FROM {simple_access_node} na WHERE na.nid = %d', $node->nid);
      while ($grant = db_fetch_array($result)) {
        $gid = $grant['gid'];
        unset($grant['gid']);
        $extra[$gid] = $grant;
      }
      $node->simple_access = $extra;
      $result = db_query('SELECT pid FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      while ($row = db_fetch_array($result)) {
        $node->simple_access_profiles[] = $row['pid'];
      }
      break;
    case 'update':
    case 'insert':
      if ($node->uid && ($node->simple_access_owner['sa_view'] || $node->simple_access_owner['sa_update'] || $node->simple_access_owner['sa_update'])) {
        $node->simple_access_owner['nid'] = $node->nid;
        drupal_write_record('simple_access_owner', $node->simple_access_owner, array(
          'nid',
        ));
        if (!db_affected_rows()) {
          drupal_write_record('simple_access_owner', $node->simple_access_owner);
        }
      }
      else {
        db_query('DELETE FROM {simple_access_owner} WHERE nid = %d', $node->nid);
      }
      db_query('DELETE FROM {simple_access_node} WHERE nid = %d', $node->nid);
      if (isset($node->simple_access)) {
        foreach ($node->simple_access as $gid => $access) {
          if ($access['sa_view'] || $access['sa_update'] || $access['sa_delete']) {
            $access['nid'] = $node->nid;
            $access['gid'] = $gid;
            drupal_write_record('simple_access_node', $access);
          }
        }
      }
      db_query('DELETE FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      if (isset($node->simple_access_profiles)) {
        foreach (array_filter($node->simple_access_profiles) as $pid) {
          $data = array(
            'nid' => $node->nid,
            'pid' => $pid,
          );
          drupal_write_record('simple_access_profiles_node', $data);
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {simple_access_node} WHERE nid = %d', $node->nid);
      db_query('DELETE FROM {simple_access_owner} WHERE nid = %d', $node->nid);
      db_query('DELETE FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      break;
  }
}

/**
 * Implementation of hook_node_access_records
 */
function simple_access_node_access_records($node) {
  $records = array();
  if (!empty($node->simple_access_profiles)) {
    foreach (array_filter($node->simple_access_profiles) as $pid) {
      $records[] = array(
        'realm' => 'simple_access_profile',
        'gid' => $pid,
        'grant_view' => 1,
        'grant_update' => 1,
        'grant_delete' => 1,
        'priority' => 0,
      );
    }
  }
  if (!empty($node->simple_access)) {

    // loop through simple_access arrays from page submission
    // $type is either 'view', 'update', or 'delete'
    foreach ($node->simple_access as $gid => $access) {
      if ($access['sa_view'] || $access['sa_update'] || $access['sa_delete']) {
        $records[] = array(
          'realm' => 'simple_access',
          'gid' => $gid,
          'grant_view' => $access['sa_view'],
          'grant_update' => $access['sa_update'],
          'grant_delete' => $access['sa_delete'],
          'priority' => 0,
        );
      }
    }
  }
  if ($node->uid && ($node->simple_access_owner['sa_view'] || $node->simple_access_owner['sa_update'] || $node->simple_access_owner['sa_delete'])) {
    $records[] = array(
      'realm' => 'simple_access_author',
      'gid' => $node->uid,
      'grant_view' => $node->simple_access_owner['sa_view'],
      'grant_update' => $node->simple_access_owner['sa_update'],
      'grant_delete' => $node->simple_access_owner['sa_delete'],
      'priority' => 0,
    );
  }
  return $records;
}

/**
 * Implementation of hook_node_grants().
 *
 *  @TODO implement to correcly return groups in all cases.
 */
function simple_access_node_grants($account, $op) {
  $gids = simple_access_groups_from_roles(array_keys($account->roles));
  $grants['simple_access'] = $gids;
  if (in_array($op, array(
    'view',
    'update',
    'delete',
  )) && !empty($gids)) {
    $result = db_query('SELECT DISTINCT pid FROM {simple_access_profiles_access} WHERE sa_' . $op . ' = 1 AND gid in (' . implode(',', array_fill(0, count($gids), '%d')) . ')', $gids);
    while ($row = db_fetch_array($result)) {
      $pids[] = $row['pid'];
    }
    if (!empty($pids)) {
      $grants['simple_access_profiles'] = $pids;
    }
  }
  $grants['simple_access_author'] = array(
    $account->uid,
  );
  return $grants;
}

/**
 * Implementation of hook_node_access_explain()
 */
function simple_access_node_access_explain($row) {
  switch ($row->realm) {
    case 'simple_access_author':
      return t('Access for the content owner');
    case 'simple_access':
      $groups = simple_access_get_groups();
      return t('Access restrictions for the "%group" group', array(
        '%group' => $groups[$row->gid]['name'],
      ));
    case 'simple_access_profile':
      $groups = simple_access_get_groups();
      $profiles = simple_access_get_profiles();
      $profile = $profiles[$row->gid];
      $message = t('Access restrictions for profile "%profile"<br /><dt>', array(
        '%profile' => $profile['name'],
      ));
      if (!empty($profile['access'])) {
        foreach ($profile['access'] as $gid => $access) {
          $message .= t(' "%group" group can @perm.', array(
            '%group' => $groups[$gid]['name'],
            '@perm' => implode(', ', array_keys(array_filter($access))),
          ));
        }
      }
      $message .= '</dt>';
      return $message;
  }
}
function simple_access_form_alter(&$form, &$form_state, $form_id) {

  // if this is a node form...
  if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if ($simple_access_form = simple_access_form($form['#node'])) {
      $form = array_merge($form, $simple_access_form);
      if (module_exists('vertical_tabs')) {
        $form['sa']['#group'] = 'additional_settings';
        $form['sa']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'simple_access') . '/simple_access.js';
      }
    }
  }
}
function simple_access_form_node_type_form_alter(&$form, &$form_state) {
  $type = $form['old_type']['#value'];
  $default = variable_get('simple_access_' . $type, array(
    'simple_access' => array(),
    'simple_access_profiles' => array(),
    'simple_access_owner' => array(),
  ));
  $tmp_form = simple_access_form((object) $default, TRUE);
  $form['simple_access'] = $tmp_form['sa'];
  $form['simple_access']['simple_access']['owner']['#parents'] = array(
    'simple_access',
    'simple_access_owner',
  );
  $form['simple_access']['#tree'] = TRUE;
  if (module_exists('vertical_tabs')) {
    $form['simple_access']['#group'] = 'additional_settings';
    $form['simple_access']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'simple_access') . '/simple_access.js';
  }
  $form['#submit'][] = 'simple_access_node_type_submit';
}
function simple_access_node_type_submit(&$form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  $type = $form_state['values']['type'];
  if ($op == t('Save content type')) {
    variable_set('simple_access_' . $type, $form_state['values']['simple_access']);
  }
}
function simple_access_theme() {
  return array(
    'simple_access_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'simple_access.theme.inc',
    ),
    'simple_access_page_overview' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'simple_access.theme.inc',
    ),
    'simple_access_profile_list' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'simple_access.theme.inc',
    ),
  );
}
function simple_access_form($node, $admin = FALSE) {

  // Get the array of checkbox options to use for each form element.
  // If the "Show groups even when user is not a member" setting is
  // enabled, or if the current user has 'administer nodes', let
  // them choose from any of the SA groups.
  $groups = simple_access_group_select();
  $profiles = simple_access_get_profiles_select();
  if (empty($groups) && empty($profiles) && !user_access('assign owner permissions')) {
    return;
  }
  $user_groups = is_array($node->simple_access) ? array_filter($node->simple_access, '_simple_access_filter_access') : array();
  $owner_priv = is_array($node->simple_access_owner) ? array_filter($node->simple_access_owner) : array();

  // set up the outer fieldset
  $form['sa'] = array(
    '#title' => t('Access'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => empty($user_groups) && empty($node->simple_access_profiles) && empty($owner_priv),
    '#access' => user_access('assign access to profiles') || user_access('assign access to nodes') || user_access('administer nodes'),
    '#attributes' => array(
      'class' => 'simple-access-settings',
    ),
    '#weight' => module_exists('content') && isset($form['type']) ? content_extra_field_weight($form['type']['#value'], 'sa') : 20,
  );
  if (!empty($profiles)) {
    $form['sa']['simple_access_profiles'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Access profile'),
      '#default_value' => $node->simple_access_profiles,
      '#options' => $profiles,
      '#access' => user_access('assign access to profiles') || user_access('administer nodes'),
    );
  }
  if (!empty($groups) || user_access('assign owner permissions')) {
    $form['sa']['simple_access'] = array(
      '#tree' => TRUE,
      '#weight' => 5,
      '#access' => user_access('assign access to nodes') || user_access('administer nodes') || user_access('assign owner permissions'),
      '#theme' => 'simple_access_form',
    );
    if ($admin) {
      $form['sa']['simple_access']['#admin'] = TRUE;
    }

    // Load the owner perminisions.
    $group = array(
      'name' => t('Owner permissions'),
      'access' => user_access('assign owner permissions') && isset($node->uid) && $node->uid,
    );
    $access = array(
      'owner' => $node->simple_access_owner,
    );
    $form['sa']['simple_access']['owner'] = simple_access_form_row('owner', $group, $access, $admin);
    $form['sa']['simple_access']['owner']['#parents'] = array(
      'simple_access_owner',
    );

    // See what form elements we should include. If not configured,
    // only enable the 'view' elements by default.
    $variable = variable_get('sa_display', array(
      'view' => 1,
    ));
    foreach ($groups as $gid => $group) {
      $form['sa']['simple_access'][$gid] = simple_access_form_row($gid, $group, $node->simple_access, $admin);
    }
  }
  return $form;
}
function simple_access_form_row($gid, $group, $access, $admin = FALSE) {
  if ($admin) {
    $variable = array(
      'view' => 1,
      'update' => 1,
      'delete' => 1,
    );
  }
  else {
    $variable = variable_get('sa_display', array(
      'view' => 1,
      'update' => 0,
      'delete' => 0,
    ));
  }
  $defaults = array(
    'sa_view' => NULL,
    'sa_update' => NULL,
    'sa_delete' => NULL,
  );
  if (empty($access[$gid])) {
    $access[$gid] = $defaults;
  }
  else {
    $access[$gid] += $defaults;
  }
  $priv = $group['access'] || user_access('administer nodes');
  $form = array(
    '#access' => $priv,
  );
  if ($admin) {
    $form['#admin'] = $admin;
  }
  $form['name'] = array(
    '#value' => $group['name'],
    '#access' => $priv,
  );
  $form['sa_view'] = array(
    '#type' => 'checkbox',
    '#default_value' => $access[$gid]['sa_view'],
    '#access' => $priv && $variable['view'],
  );
  $form['sa_update'] = array(
    '#type' => 'checkbox',
    '#default_value' => $access[$gid]['sa_update'],
    '#access' => $priv && $variable['update'],
  );
  $form['sa_delete'] = array(
    '#type' => 'checkbox',
    '#default_value' => $access[$gid]['sa_delete'],
    '#access' => $priv && $variable['delete'],
  );
  return $form;
}
function simple_access_get_roles($gid) {
  $roles = array();
  $sql = db_query('SELECT rid FROM {simple_access_roles} WHERE gid = %d', $gid);
  while ($row = db_fetch_object($sql)) {
    $roles[] = $row->rid;
  }
  return $roles;
}
function simple_access_get_profiles_select() {
  $profiles = simple_access_get_profiles();
  return array_map('_simple_access_filter_profiles', $profiles);
}
function _simple_access_filter_profiles($a) {
  return $a['name'];
}
function simple_access_get_profiles() {
  $profiles = array();
  $result = db_query('SELECT pid, name FROM {simple_access_profiles} ORDER BY weight, name');
  while ($p = db_fetch_array($result)) {
    $profiles[$p['pid']] = $p;
    $profiles[$p['pid']]['access'] = array();
  }
  $result = db_query('SELECT pid, gid, sa_view, sa_update, sa_delete FROM {simple_access_profiles_access} ORDER BY pid');
  while ($a = db_fetch_array($result)) {
    if (isset($profiles[$a['pid']])) {
      $profiles[$a['pid']]['access'][$a['gid']] = array(
        'sa_view' => $a['sa_view'],
        'sa_update' => $a['sa_update'],
        'sa_delete' => $a['sa_delete'],
      );
    }
  }
  return $profiles;
}
function simple_access_get_groups() {
  $groups = array();
  $result = db_query('SELECT gid, name, weight FROM {simple_access_groups} ORDER BY weight, name');
  while ($g = db_fetch_array($result)) {
    $groups[$g['gid']]['name'] = $g['name'];
    $groups[$g['gid']]['gid'] = $g['gid'];
    $groups[$g['gid']]['weight'] = $g['weight'];
    $groups[$g['gid']]['roles'] = simple_access_get_roles($g['gid']);
  }
  return $groups;
}
function simple_access_group_select() {
  static $groups;
  if (empty($groups)) {
    global $user;
    $default_access = user_access('administer nodes');
    $groups = array();
    $result = db_query('SELECT gid, name FROM {simple_access_groups} ORDER BY weight, name');
    while ($group = db_fetch_array($result)) {
      $groups[$group['gid']] = $group;
      $groups[$group['gid']]['access'] = $default_access;
    }
    if (!$default_access) {

      // return just groups for which user is a member
      $roles = array_keys($user->roles);
      $result = db_query('SELECT g.gid FROM {simple_access_groups} g INNER JOIN {simple_access_roles} r ON g.gid = r.gid WHERE r.rid IN (' . implode(',', array_fill(0, count($roles), '%d')) . ') GROUP BY g.gid', $roles);
      while ($group = db_fetch_array($result)) {
        $groups[$group['gid']]['access'] = TRUE;
      }
    }
  }
  return $groups;
}

/**
 * Get a list of group/grant ids based on a list of user roles
 * $roles should be a linear list a role ids
 */
function simple_access_groups_from_roles($roles) {

  // there probably should be some 'static' stuff going on here
  // always return gid 0 just to be safe.
  $gids = array();
  $result = db_query('SELECT DISTINCT(gid) FROM {simple_access_roles} WHERE rid IN (' . implode(',', array_fill(0, count($roles), '%d')) . ')', $roles);
  while ($g = db_fetch_object($result)) {
    $gids[] = $g->gid;
  }
  return $gids;
}

/**
 *
 */
function simple_access_groups_check_user($groups) {
  global $user;
  $roles = array_keys($user->roles);
  $roles[] = $user->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
  $user_groups = simple_access_groups_from_roles($roles);
  return array_intersect(array_filter($groups, $user_groups));
}

/**
 * Save group of roles into the database
 * $roles is an associative array of roles where the keys are role ids
 * $name is the name of the group
 * $gid is the group id
 *
 */
function simple_access_save_group($edit) {
  if (empty($edit['gid'])) {
    drupal_write_record('simple_access_groups', $edit);
  }
  else {
    drupal_write_record('simple_access_groups', $edit, array(
      'gid',
    ));
  }
  db_query('DELETE FROM {simple_access_roles} WHERE gid = %d', $edit['gid']);
  if (is_array($edit['roles'])) {
    foreach ($edit['roles'] as $key => $value) {
      if ($value) {
        $record = array(
          'rid' => $key,
          'gid' => $edit['gid'],
        );
        $s = drupal_write_record('simple_access_roles', $record);
        $success = $success && $s;
      }
    }
  }
  return $success;
}
function simple_access_delete_profile($pid) {
  db_query('DELETE FROM {simple_access_profiles} WHERE pid = %d', $pid);
  db_query('DELETE FROM {simple_access_profiles_access} WHERE pid = %d', $pid);
  db_query('DELETE FROM {simple_access_profiles_node} WHERE pid = %d', $pid);
}
function simple_access_delete_group($gid) {
  db_query('DELETE FROM {simple_access_roles} WHERE gid = %d', $gid);
  db_query('DELETE FROM {simple_access_groups} WHERE gid = %d', $gid);
  db_query('DELETE FROM {simple_access_node} WHERE gid = %d', $gid);
}

/**
 * Filter the access records for the current user
 */
function _simple_access_filter_access($a) {
  $groups = simple_access_group_select();
  return isset($a['gid']) && isset($groups[$a['gid']]['access']) && $groups[$a['gid']]['access'];
}

/**
 * Implementation of hook_action_info()
 */
function simple_access_action_info() {
  return array(
    'simple_access_owner_grant' => array(
      'type' => 'node',
      'description' => t('Grant permissions to content owner'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
    'simple_access_owner_revoke' => array(
      'type' => 'node',
      'description' => t('Revoke permissions from content owner'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
    'simple_access_group_grant' => array(
      'type' => 'node',
      'description' => t('Grant permissions to groups'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
    'simple_access_group_revoke' => array(
      'type' => 'node',
      'description' => t('Revoke permissions from groups'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
    'simple_access_profile_enable' => array(
      'type' => 'node',
      'description' => t('Enable access profile'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
    'simple_access_profile_disable' => array(
      'type' => 'node',
      'description' => t('Disable access profile'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'insert',
          'update',
        ),
      ),
    ),
  );
}

/**
 * Configure grant content owner permissions
 */
function simple_access_owner_grant_form($settings = array()) {
  $form = array();
  $form['sa_owner_permissions'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Grant owner permissions'),
    '#default_value' => empty($settings['sa_owner_permissions']) ? array() : $settings['sa_owner_permissions'],
    '#options' => array(
      'sa_view' => t('View'),
      'sa_update' => t('Update'),
      'sa_delete' => t('Delete'),
    ),
    '#description' => t('Select permissions to grant for the content owner'),
  );
  return $form;
}
function simple_access_owner_grant_submit($form, &$form_state) {
  $settings = array(
    'sa_owner_permissions' => $form_state['values']['sa_owner_permissions'],
  );
  return $settings;
}

/**
 * Action to grant permissions to the owner
 */
function simple_access_owner_grant($node, $conf) {
  foreach (array_filter($conf['sa_owner_permissions']) as $option) {
    $node->simple_access_owner[$option] = 1;
  }
  return array(
    'node' => $node,
  );
}

/**
 * Configure revoke content owner permissions
 */
function simple_access_owner_revoke_form($settings = array()) {
  $form = array();
  $form['sa_owner_permissions'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Revoke owner permissions'),
    '#default_value' => empty($settings['sa_owner_permissions']) ? array() : $settings['sa_owner_permissions'],
    '#options' => array(
      'sa_view' => t('View'),
      'sa_update' => t('Update'),
      'sa_delete' => t('Delete'),
    ),
    '#description' => t('Select permissions to revoke for the content owner'),
  );
  return $form;
}
function simple_access_owner_revoke_submit($form, &$form_state) {
  $settings = array(
    'sa_owner_permissions' => $form_state['values']['sa_owner_permissions'],
  );
  return $settings;
}

/**
 * Action to grant permissions to the owner
 */
function simple_access_owner_revoke($node, $conf) {
  foreach (array_filter($conf['sa_owner_permissions']) as $option) {
    $node->simple_access_owner[$option] = 0;
  }
  return array(
    'node' => $node,
  );
}

/**
 * Configure grant group permissions
 */
function simple_access_group_grant_form($settings = array()) {
  $form = array();
  $form['sa_group_permissions'] = array(
    '#tree' => TRUE,
    '#theme' => 'simple_access_form',
    '#admin' => TRUE,
  );
  $groups = simple_access_get_groups();
  foreach ($groups as $gid => $group) {
    $form['sa_group_permissions'][$gid]['name'] = array(
      '#value' => $group['name'],
    );
    $form['sa_group_permissions'][$gid]['sa_view'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_view'],
    );
    $form['sa_group_permissions'][$gid]['sa_update'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_update'],
    );
    $form['sa_group_permissions'][$gid]['sa_delete'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_delete'],
    );
  }
  return $form;
}
function simple_access_group_grant_submit($form, &$form_state) {
  $settings = array(
    'sa_group_permissions' => $form_state['values']['sa_group_permissions'],
  );
  return $settings;
}

/**
 * Action to grant permissions to the owner
 */
function simple_access_group_grant($node, $conf) {
  foreach ($conf['sa_group_permissions'] as $gid => $group) {
    foreach (array_keys(array_filter($group)) as $option) {
      $node->simple_access[$gid][$option] = 1;
    }
  }
  return array(
    'node' => $node,
  );
}

/**
 * Configure revoke group permissions
 */
function simple_access_group_revoke_form($settings = array()) {
  $form = array();
  $form['sa_group_permissions'] = array(
    '#tree' => TRUE,
    '#theme' => 'simple_access_form',
    '#admin' => TRUE,
  );
  $groups = simple_access_get_groups();
  foreach ($groups as $gid => $group) {
    $form['sa_group_permissions'][$gid]['name'] = array(
      '#value' => $group['name'],
    );
    $form['sa_group_permissions'][$gid]['sa_view'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_view'],
    );
    $form['sa_group_permissions'][$gid]['sa_update'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_update'],
    );
    $form['sa_group_permissions'][$gid]['sa_delete'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['sa_group_permissions'][$gid]['sa_delete'],
    );
  }
  return $form;
}
function simple_access_group_revoke_submit($form, &$form_state) {
  $settings = array(
    'sa_group_permissions' => $form_state['values']['sa_group_permissions'],
  );
  return $settings;
}

/**
 * Action to revoke permissions to the owner
 */
function simple_access_group_revoke($node, $conf) {
  foreach ($conf['sa_group_permissions'] as $gid => $group) {
    foreach (array_keys(array_filter($group)) as $option) {
      $node->simple_access[$gid][$option] = 0;
    }
  }
  return array(
    'node' => $node,
  );
}

/**
 * Configure enable security profile
 */
function simple_access_profile_enable_form($settings = array()) {
  $form = array();
  $form['sa_profiles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Access profiles'),
    '#default_value' => empty($settings['sa_profiles']) ? array() : $settings['sa_profiles'],
    '#options' => simple_access_get_profiles_select(),
    '#description' => t('Select permissions to grant for the content owner'),
  );
  return $form;
}
function simple_access_profile_enable_submit($form, &$form_state) {
  $settings = array(
    'sa_profiles' => $form_state['values']['sa_profiles'],
  );
  return $settings;
}

/**
 * Action enable access profile
 */
function simple_access_profile_enable($node, $conf) {
  foreach (array_filter($conf['sa_profiles']) as $pid) {
    if (!in_array($pid, $node->simple_access_profiles)) {
      $node->simple_access_profiles[] = $pid;
    }
  }
  return array(
    'node' => $node,
  );
}

/**
 * Configure disable security profile
 */
function simple_access_profile_disable_form($settings = array()) {
  $form = array();
  $form['sa_profiles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Access profiles'),
    '#default_value' => empty($settings['sa_profiles']) ? array() : $settings['sa_profiles'],
    '#options' => simple_access_get_profiles_select(),
    '#description' => t('Select permissions to grant for the content owner'),
  );
  return $form;
}
function simple_access_profile_disable_submit($form, &$form_state) {
  $settings = array(
    'sa_profiles' => $form_state['values']['sa_profiles'],
  );
  return $settings;
}

/**
 * Action to disable access profile
 */
function simple_access_profile_disable($node, $conf) {
  foreach (array_filter($conf['sa_profiles']) as $pid) {
    if (in_array($pid, $node->simple_access_profiles)) {
      unset($node->simple_access_profiles[array_search($pid, $node->simple_access_profiles)]);
    }
  }
  return array(
    'node' => $node,
  );
}

/**
 * Implementation of hook_views_api().
 */
function simple_access_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Implementation of hook_content_extra_fields().
 */
function simple_access_content_extra_fields($type_name) {
  $fields['simple_access'] = array(
    'label' => t('Simple Access'),
    'description' => t('Simple Access module form.'),
    'weight' => 20,
  );
  return $fields;
}

Functions

Namesort descending Description
simple_access_action_info Implementation of hook_action_info()
simple_access_content_extra_fields Implementation of hook_content_extra_fields().
simple_access_delete_group
simple_access_delete_profile
simple_access_form
simple_access_form_alter
simple_access_form_node_type_form_alter
simple_access_form_row
simple_access_get_groups
simple_access_get_profiles
simple_access_get_profiles_select
simple_access_get_roles
simple_access_groups_check_user
simple_access_groups_from_roles Get a list of group/grant ids based on a list of user roles $roles should be a linear list a role ids
simple_access_group_grant Action to grant permissions to the owner
simple_access_group_grant_form Configure grant group permissions
simple_access_group_grant_submit
simple_access_group_revoke Action to revoke permissions to the owner
simple_access_group_revoke_form Configure revoke group permissions
simple_access_group_revoke_submit
simple_access_group_select
simple_access_menu Implementation of hook_menu().
simple_access_nodeapi Implementation of hook_nodeapi()
simple_access_node_access_explain Implementation of hook_node_access_explain()
simple_access_node_access_records Implementation of hook_node_access_records
simple_access_node_grants Implementation of hook_node_grants().
simple_access_node_type_submit
simple_access_owner_grant Action to grant permissions to the owner
simple_access_owner_grant_form Configure grant content owner permissions
simple_access_owner_grant_submit
simple_access_owner_revoke Action to grant permissions to the owner
simple_access_owner_revoke_form Configure revoke content owner permissions
simple_access_owner_revoke_submit
simple_access_perm Implementation of hook_perm().
simple_access_profile_disable Action to disable access profile
simple_access_profile_disable_form Configure disable security profile
simple_access_profile_disable_submit
simple_access_profile_enable Action enable access profile
simple_access_profile_enable_form Configure enable security profile
simple_access_profile_enable_submit
simple_access_save_group Save group of roles into the database $roles is an associative array of roles where the keys are role ids $name is the name of the group $gid is the group id
simple_access_theme
simple_access_views_api Implementation of hook_views_api().
_simple_access_filter_access Filter the access records for the current user
_simple_access_filter_profiles