Source for file auth.php

Documentation is available at auth.php

  1. <?php
  2. /**
  3. * User Authentication and Authorisation (Login and Permissions object)
  4. *
  5. @author     Stuart Prescott
  6. @copyright  Copyright Stuart Prescott
  7. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. @version    $Id$
  9. @package    Bumblebee
  10. @subpackage DBObjects
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. /** inherit from basic auth module */
  17. require_once 'inc/bb/basicauth.php';
  18. /** sql manipulation routines */
  19. require_once 'inc/formslib/sql.php';
  20. /** type checking and data manipulation */
  21. require_once 'inc/typeinfo.php';
  22. /** permissions codes */
  23. require_once 'inc/permissions.php';
  24. /** logging functions */
  25. require_once 'inc/logging.php';
  26. /** system configuration */
  27. require_once 'inc/bb/configreader.php';
  28.  
  29. /**
  30. * User *authorisation* and *authentication* object
  31. *
  32. @package    Bumblebee
  33. @subpackage DBObjects
  34. @todo //TODO: update permissions system
  35. @todo //TODO: documentation
  36. */
  37. class BumblebeeAuth extends BasicAuth {
  38.   var $anonymous = false;
  39.   var $euid;            //permit user masquerading like su. Effective UID
  40.     var $ename;           //effective name
  41.     var $eusername;       //effective username
  42.     var $permissions = array();
  43.   var $system_permissions = 0;
  44.  
  45.   /**
  46.   *  Create the auth object
  47.   *
  48.   * @param array   $data    array containing keys 'username' and 'pass'
  49.   * @param boolean $recheck (optional) ignore session data and check anyway
  50.   * @param string  $table  (optional) db table from which login data should be taken
  51.   */
  52.   function BumblebeeAuth($data$recheck false$table='users'{
  53.     $this->_checkAnonymous($data);
  54.  
  55.     parent::BasicAuth($data$recheck || $this->_changeUser()$table);
  56.  
  57.     if ($this->_loggedin{
  58.       // set up Authorisation parts
  59.       $this->_loadPermissions();
  60.       $this->_checkMasq();
  61.     }
  62.   }
  63.  
  64.   function _changeUser({
  65.     if (isset($_POST['changeuser']|| isset($_GET['changeuser'])) {
  66.       return true;
  67.     }
  68.   }
  69.  
  70.   function _checkAnonymous(&$data{
  71.     $conf ConfigReader::getInstance();
  72.     if ((isset($_POST['anonymous']|| isset($_GET['anonymous']))
  73.         && $conf->value('display''AnonymousAllowed'false)) {
  74.       $data['username'$conf->value('display''AnonymousUsername');
  75.       $data['pass']     $conf->value('display''AnonymousPassword');
  76.     }
  77.   }
  78.  
  79.   /**
  80.    * Permit user masquerading -- the admin user can become another user for a period
  81.    * of time to make a bookings etc
  82.   **/
  83.  
  84.   function _checkMasq({
  85.     if ($this->masqPermitted(&& $this->_var_get('euid'!= NULL{
  86.       $this->euid      = $this->_var_get('euid');
  87.       $this->ename     = $this->_var_get('ename');
  88.       $this->eusername = $this->_var_get('eusername');
  89.     }
  90.   }
  91.  
  92.   function isSystemAdmin({
  93.     if($this->system_permissions == -|| $this->system_permissions >= BBROLE_ADMIN_BASE{
  94.       return true;
  95.     else {
  96.       return false;
  97.     }
  98.   }
  99.  
  100.   function isInstrumentAdmin($instr{
  101.     trigger_error("using deprecated isInstrumentAdmin"E_USER_NOTICE);
  102.     $l debug_backtrace();
  103.     print "file= "$l[0]['file'.", line=".$l[0]['line']."\n<br />";
  104.     return $this->isSystemAdmin(||
  105.           ($this->instrument_permissions($instrBBPERM_INSTR_BOOK_FUTURE);
  106.   }
  107.  
  108.  
  109.   function instrument_permissions($instrument{
  110.     $conf ConfigReader::getInstance();
  111.  
  112.     if (isset($this->permissions[$instrument])) {
  113.       return $this->permissions[$instrument];
  114.     }
  115.     $permission 0;
  116.     if ($instrument==0{
  117.       // look for permissions across all instruments
  118.       $total 0;
  119.       global $TABLEPREFIX;
  120.       $q 'SELECT * '
  121.           .' FROM '.$TABLEPREFIX.'permissions'
  122.           .' WHERE userid=' qw($this->uid);
  123.       $sql db_get($qfalse);
  124.       while ($row db_fetch_array($sql)) {
  125.         if (isset($row['permissions']&& $conf->value('auth''permissionsModel'false)) {
  126.           $permission $row['permissions'];
  127.         else {
  128.           $permission $this->_constructInstrumentPermission($row);
  129.         }
  130.         $this->permissions[$instrument$permission;
  131.         $total ((int)$total((int)$permission);
  132.       }
  133.       $permission $total;
  134.     else {
  135.       $row quickSQLSelect('permissions',
  136.                               array('userid',   'instrid'),
  137.                               array($this->uid$instrument)
  138.                            );
  139.       if (is_array($row)) {
  140.         if (isset($row['permissions']&& $conf->value('auth''permissionsModel'false)) {
  141.           $permission $row['permissions'];
  142.         else {
  143.           $permission $this->_constructInstrumentPermission($row);
  144.         }
  145.       }
  146.     }
  147.     //save the permissions to speed this up later
  148.     $this->permissions[$instrument= (int)$permission;
  149.     return $this->permissions[$instrument];
  150.   }
  151.  
  152.   /**
  153.   * make up the permissions for the instrument
  154.   *
  155.   * @param    array   $row   from the database
  156.   * @returns  integer        permissions
  157.   */
  158.   function _constructInstrumentPermission($row{
  159.     logmsg(2"Making up some permissions for instrument. Upgrade database format to get rid of this message.");
  160.     $permission 0;
  161.     if (isset($row['isadmin']&& $row['isadmin']{
  162.       $permission BBPERM_INSTR_ALL;
  163.     else {
  164.       $permission BBPERM_INSTR_BASIC;
  165.     }
  166.     return $permission;
  167.   }
  168.  
  169.   function getEUID({
  170.     return (isset($this->euid$this->euid : $this->uid);
  171.   }
  172.  
  173.   function masqPermitted($instr=0{
  174.     return $this->permitted(BBROLE_ADMIN_MASQ$instr);
  175.   }
  176.  
  177.   function amMasqed({
  178.     return (isset($this->euid&& $this->euid != $this->uid);
  179.   }
  180.  
  181.   /**
  182.   * start masquerading as another user
  183.   */
  184.   function assumeMasq($id{
  185.     if ($this->masqPermitted()) {
  186.       //masquerade permitted
  187.       $row $this->_retrieveUserInfo($id0);
  188.       $this->_var_put('euid',       $this->euid      = $row['id']);
  189.       $this->_var_put('eusername',  $this->eusername = $row['username']);
  190.       $this->_var_put('ename',      $this->ename     = $row['name']);
  191.       return $row;
  192.     else {
  193.       // masquerade not permitted
  194.       echo "Couldn't assume masq";
  195.       return 0;
  196.     }
  197.   }
  198.  
  199.   /**
  200.   * stop masquerading as another user
  201.   */
  202.   function removeMasq({
  203.     $this->_var_put('euid',        $this->euid      = null);
  204.     $this->_var_put('eusername',   $this->eusername = null);
  205.     $this->_var_put('ename',       $this->ename     = null);
  206.   }
  207.  
  208.   function permitted($operation$instrument=NULL{
  209.     //print "Requested: $operation and have permissions $this->system_permissions<br/>";
  210.     // NOTE: Must cast to int before using PHP's bitwise operators else you will get stupid results
  211.     // due to the loose typing mechanism switching the variables to float or string on you.
  212.     if ($operation == BBROLE_NONEreturn true;
  213.     if ($instrument === NULL{
  214.       // looking for system permissions
  215.       return ((int) $operation (int) $this->system_permissions== $operation;
  216.     else {
  217.       if (is_array($instrument)) {
  218.         foreach ($instrument as $i{
  219.           if ($this->permitted($operation$i)) return false;
  220.         }
  221.         return true;
  222.       else {
  223.         #echo "op = ". $operation;
  224.         #echo "instr = " .(int) $this->instrument_permissions($instrument);
  225.         #echo "ok=". ((int) $this->instrument_permissions($instrument) & (int) $operation);
  226.         #echo "<br />";
  227.         return (((int) $operation)
  228.               (int) $this->system_permissions | (int) $this->instrument_permissions($instrument) ))
  229.              == $operation;
  230.       }
  231.     }
  232.   }
  233.  
  234.   function _loadPermissions({
  235.     $conf ConfigReader::getInstance();
  236.  
  237.     if ($this->isLoggedIn()) return;
  238.  
  239.     /// FIXME
  240.     if (isset($this->user_row['permissions']&& $conf->value('auth''permissionsModel'false)) {
  241.       $this->system_permissions = (int) $this->user_row['permissions'];
  242.     else {
  243.       logmsg(2"Making up some permissions for user. Upgrade database format to get rid of this message.");
  244.       $this->system_permissions = 0;
  245.       if (isset($this->user_row['isadmin']&& $this->user_row['isadmin']{
  246.         $this->system_permissions |= BBPERM_ADMIN_ALL;
  247.       else {
  248.         if ($this->localLogin{
  249.           $this->system_permissions |= BBPERM_USER_BASIC BBPERM_USER_PASSWD;
  250.         else {
  251.           $this->system_permissions |= BBPERM_USER_BASIC;
  252.         }
  253.       }
  254.       if ($this->masqPermitted()) {
  255.         $this->system_permissions |= BBPERM_ADMIN_MASQ;
  256.       }
  257.     }
  258.     if ($this->localLogin{
  259.       $this->system_permissions = $this->system_permissions (BBPERM_USER_PASSWD);
  260.     }
  261.   }
  262.  
  263. //BumblebeeAuth
  264.  
  265. ?>

Documentation generated on Tue, 06 Mar 2007 10:00:36 +0000 by phpDocumentor 1.3.0