All pastes #229751 Raw Edit

Mine

public text v1 · immutable
#229751 ·published 2006-10-30 22:25 UTC
rendered paste body

<?php

  // $Id: ac1.module,v 0.5 08/16/2006 rjewell Exp $
/**
 * @file
 * Control access to site content based on userid.
 * 
 * 
 */


/**
 * Implementation of hook_help
 */
function ac1_help($section) {
  switch ($section) {
  case 'admin/modules#description':
	return t('Simple/experimental access control based on user id.');
	break;
  case 'admin/help#tac_lite':
	$output .= '<p>'.t('This experimental module allows you to create private nodes that can only be viewed by a particular user. On node setup page, select the user ID or -1 for public.This module allows you to restrict access to site content.  It uses a simple scheme based on Taxonomy, Users and Roles.')."</p>\n";
	return $output;
	break;

  }
}

function ac1_form_alter($form_id, &$form) {
  // if this is a form (?)
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {

     // get the node record
    $node = $form['#node'];
    
    drupal_set_message( "FORM ALTER, ac1 = " . $node->ac1 );

    if( ! isset( $node->ac1 ) ) {
        drupal_set_message( "AC1 not set" );
        // Load the grants from the database for this node
        $result = db_query('SELECT na.gid FROM {node_access} na WHERE na.nid = %d AND na.realm = \'ac1\' AND na.grant_view = 1', $node->nid );
        $grant = db_fetch_object($result);

        $str = "grant = " . $grant->gid;
        drupal_set_message( $str );
        
        // load ac1 with gid
        if( $grant ) {//&& $grant->gid == 0) {
          // The "public" grant was set.
          $node->ac1 = $grant->gid;
          drupal_set_message( "AC1 assigned from grants " . $node->ac1 );
        }
        // default to 0
        else {
          $node->ac1 = 0;
          drupal_set_message( "AC1 defaulted to 0" );
        }
      }
      
      //$str = "ac1 = " . $node->ac1;
      //drupal_set_message($str);
    
    // create a new section in the edit node form called access control
    $form['access'] = array('#type' => 'fieldset',
      '#title' => t('Access Control'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($item['title']),
      '#tree' => TRUE,
      '#weight' => 30,
    );
    
    // we need to build an array of user names and another with matching user id's
    
    // we need to figure out based on nodes owner, which name to select in menu
                        /*
     $form['access']['ac1_userid'] = array(
                              '#type' => 'select',
                              '#title' => t('Specify owner for private nodes'),
                              '#default_value' => variable_get('ac1_userid','user 1'),
                              '#options' => array(
                                         'title' => t('user 0'),
                                         'teaser' => t('user 1'),
                                         'fulltext' => t('user 2'),
                                         ),
                              '#description' => t('Owner of this node that has sole viewing privledges'),
                              );
                          */
  $form['access']['ac1'] = array(
  '#type' => 'textfield',
  '#title' => t('Access ID -1, 0, or UID'),
  '#default_value' => $node->ac1,
  '#size' => 5,
  '#maxlength' => 128,
  '#required' => TRUE,
);

    // the final return is not needed because $form is a reference.
  }
}

function ac1_nodeapi(&$node, $op, $arg = 0) {
  //  $str = "nodeapi op = " . $op . " ac1 = " . $node->ac1;
  drupal_set_message( "nodeapi op = " . $op . " ac1 = " . $node->ac1 );
  
  switch ($op) {
    case 'delete':
      // When a node is deleted, delete any relevant grants for this node
      db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'ac1\'', $node->nid );
      break;
    case 'load':
        $result = db_query('SELECT na.gid FROM {node_access} na WHERE na.nid = %d AND na.realm = \'ac1\' AND na.grant_view = 1', $node->nid );
        $grant = db_fetch_object($result);


        // load ac1 with gid
        if( $grant ) {//&& $grant->gid == 0) {
          // The "public" grant was set.
          $node->ac1 = $grant->gid;
          drupal_set_message( "AC1 set from grant in nodeapi load " . $node->ac1 );

        }
        // default to 0
        else {
          $node->ac1 = 0;
          drupal_set_message( "AC1 defaulted to 0 in nodeapi load" );

        }

      break;
    case 'insert':
    case 'update':
         drupal_set_message( "storing ac1 as gid nodeapi " . $node->ac1 );

      // Clear out any existing grants for the node, and set new ones.
      db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'ac1\'', $node->nid );

      // for testing node_grants, I am hardwiring in a uid (or -1 registered user only, 0 everyone)
      db_query('INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update,
        grant_delete) VALUES (%d, %d, \'ac1\', %d, %d, %d)', $node->nid, $node->ac1, 1, 0, 0);
      //}
      break;
  }
}

function ac1_node_grants( $account, $op ) {
  $grants = array();

  // this appears to override drupals default node access entry that allows everyone access to
  // everything (without destructively deleting it)
  $all = array(-1);

  //everyone gets access to nodes that have gid 0 (don't need an account)
  $grants[0] = 0;
  
  // if user has an account they get access to gid -1 nodes (for clients only)
  if( $account->uid > 0 )
      $grants[1] = -1;
      
  // users can also view private nodes (ones set with gid == users uid)
  if( $op == 'view' )
      $grants[2] = $account->uid;
  
  // only the admin can edit/delete nodes with this system (I think)
  
  return array( 'all' => $all, 'ac1' => $grants );
}