b2evolution PHP Cross Reference Blogging Systems

Source: /inc/users/model/_groupsettings.class.php - 195 lines - 4325 bytes - Summary - Text - Print

   1  <?php
   2  
   3  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
   4  
   5  load_class( 'settings/model/_abstractsettings.class.php', 'AbstractSettings' );
   6  load_class( '_core/model/db/_sql.class.php', 'SQL' );
   7  
   8  
   9  /**
  10   * GroupSettings class
  11   *
  12   * This class represents group permissions
  13   */
  14  class GroupSettings extends AbstractSettings
  15  {
  16      /**
  17       * Current or default permission values
  18       * @var array
  19       */
  20      var $permission_values = array();
  21  
  22      /**
  23       * Permission modules
  24       * @var array
  25       */
  26      var $permission_modules = array();
  27  
  28      /**
  29       * New permissions values
  30       * @access private
  31       * @var array
  32       */
  33      var $_permissions = array();
  34  
  35  
  36      /**
  37       * Constructor
  38       */
  39  	function GroupSettings()
  40      {     // call parent constructor
  41          parent::AbstractSettings( 'T_groups__groupsettings', array( 'gset_grp_ID', 'gset_name' ), 'gset_value', 1 );
  42      }
  43  
  44  
  45      /**
  46       * Load permissions
  47       *
  48       * @param integer Group ID
  49       */
  50  	function load( $grp_ID )
  51      {
  52          global $DB, $modules;
  53  
  54          // Get default group permission from each module
  55          foreach( $modules as $module )
  56          {
  57              $Module = & $GLOBALS[$module.'_Module'];
  58              if( method_exists( $Module, 'get_default_group_permissions' ) )
  59              {    // Module has pluggable permissions and we can add them to the current setting
  60                  $this->add( $module, $Module->get_default_group_permissions( $grp_ID ), $grp_ID );
  61              }
  62          }
  63  
  64          if( $grp_ID != 0 )
  65          {
  66              // Select current group permission from database
  67              $SQL = new SQL();
  68              $SQL->SELECT( '*' );
  69              $SQL->FROM( 'T_groups__groupsettings' );
  70              $SQL->WHERE( 'gset_grp_ID = '.$grp_ID );
  71  
  72              $DB->begin();
  73  
  74              // Set current group permissions
  75              $existing_perm = array();
  76              foreach( $DB->get_results( $SQL->get() ) as $row )
  77              {
  78                  $existing_perm[] = $row->gset_name;
  79                  $this->permission_values[$row->gset_name] = $row->gset_value;
  80              }
  81  
  82              // Set default group permission if these permissions don't exist
  83              $update_permissions = false;
  84              foreach( $this->permission_values as $name => $value )
  85              {
  86                  if( ! in_array( $name, $existing_perm ) )
  87                  {
  88                      $this->set( $name, $value, $grp_ID );
  89                      $update_permissions = true;
  90                  }
  91              }
  92  
  93              if( $update_permissions )
  94              {    // We can update permission as there are some new permnissions
  95                  $this->dbupdate( $grp_ID );
  96              }
  97  
  98              $DB->commit();
  99          }
 100      }
 101  
 102  
 103      /**
 104       * Add default permission to the group.
 105       * Each module can define its own default permissions.
 106       *
 107       * @param string module name
 108       * @param array permissions
 109       * @param integer Group ID
 110       */
 111  	function add( $module, $permissions, $grp_ID )
 112      {
 113          if( ! empty( $permissions ) )
 114          {
 115              foreach( $permissions as $key => $value )
 116              {
 117                  $this->permission_values[$key] = $value;
 118                  $this->permission_modules[$key] = $module;
 119              }
 120          }
 121      }
 122  
 123  
 124      /**
 125       * Get a permission from the DB group settings table
 126       *
 127       * @param string name of permission
 128       * @param integer Group
 129       */
 130  	function get( $permission, $grp_ID )
 131      {
 132          if( $grp_ID != 0 )
 133          {    // We can get permission from database, because the current group setting are available in database
 134              $this->permission_values[$permission] = parent::get( $grp_ID, $permission );
 135          }
 136          return $this->permission_values[$permission];
 137      }
 138  
 139  
 140      /**
 141       * Temporarily sets a group permission ({@link dbupdate()} writes it to DB)
 142       *
 143       * @param string name of permission
 144       * @param mixed new value
 145       * @param integer Group ID
 146       */
 147  	function set( $permission, $value, $grp_ID )
 148      {
 149          if( $grp_ID != 0 )
 150          {    // We can set permission, because the current group is already in database
 151              $this->permission_values[$permission] = $value;
 152              return parent::set( $grp_ID, $permission, $value );
 153          }
 154  
 155          $this->_permissions[$permission] = $value;
 156          return true;
 157      }
 158  
 159      /**
 160       * Update the DB based on previously recorded changes
 161       *
 162       * @param integer Group ID
 163       */
 164  	function dbupdate( $grp_ID )
 165      {
 166          if( ! empty( $this->_permissions ) )
 167          {    // Set temporary permissions. It is only for the new creating group
 168              foreach( $this->_permissions as $name => $value )
 169              {
 170                  $this->set( $name, $value, $grp_ID );
 171              }
 172  
 173              $this->_permissions = array();
 174          }
 175  
 176          // Update permissions
 177          return parent::dbupdate();
 178      }
 179  
 180  
 181      /**
 182       * Delete all of the group permissions
 183       *
 184       * @param @param integer Group ID
 185       */
 186  	function delete( $grp_ID )
 187      {
 188          foreach( $this->permission_values as $name => $value )
 189          {
 190              parent::delete( $grp_ID, $name );
 191          }
 192      }
 193  }
 194  
 195  ?>

title

Description

title

Description

title

Description

title

title

Body