[ Index ]

PHP Cross Reference of ACL Module

title

Body

[close]

/models/ -> Action.php (source)

   1  <?php
   2  
   3  /**

   4   * This is the model class for table "{{action}}".

   5   *

   6   * Actions represent the things you can do with an Acess control object: for 

   7   * example you can most often create, read, update and delete objects (also called CRUD)

   8   * The actions which can be performed on a given object can be explicitely

   9   * denoted by RestrictedActiveRecord::$possibleActions. Actions which are not

  10   * in this list, if it is defined, will never be granted.

  11   * @see RestrictedActivgeRecord::possibleActions

  12   * 

  13   * @author dispy <dispyfree@googlemail.com>

  14   * @package acl.base

  15   * @license LGPLv2

  16   * 

  17   * The followings are the available columns in table '{{action}}':

  18   * @property integer $id

  19   * @property string $name

  20   * @property integer $created

  21   */
  22  class Action extends CActiveRecord
  23  {
  24      
  25      /**

  26       * Translates the gibven actions into valid actions

  27       * @param AclObject $obj the object to perform the permissions on

  28       * @param mixed $actions may be a string or an array 

  29       */
  30      public static function translateActions($obj, $actions){
  31          if(is_string($actions))
  32              $actions = static::translateStringActions($actions);
  33          
  34          //Now, check if the object restricts the actions

  35          $class = NULL;
  36          
  37          if($obj instanceof CActiveRecord)
  38              $class = get_class($class);
  39          elseif($obj->model !== NULL){
  40              $class = $obj->model;
  41          }
  42          
  43          if($class === NULL)
  44              return array();
  45              
  46          
  47          if(isset($class::$possibleActions)){
  48              $newActions = array();
  49              foreach($actions as $action){
  50                  if(in_array($action, $class::$possibleActions))
  51                          $newActions[] = $action;
  52              }
  53              $actions = $newActions;
  54          }
  55              
  56          
  57          
  58          return $actions;
  59      }
  60      
  61      /**

  62       * Processes the given actions

  63       * @param mixed $actions string or array of actions

  64       */
  65      protected static function translateStringActions($actions){
  66          //Nothing more to do

  67          if(is_array($actions))
  68              return $actions;
  69          
  70          //Now, it is a string

  71          //Search the first occurence of a modificator (+ or -)

  72          $posMinus = strpos($actions, '-');
  73          $posPlus  = strpos($actions, '+');
  74          
  75          //If none is found, we can split it up into the actions

  76          if($posMinus === false && $posPlus === false){
  77              $actions = str_replace(",", " ",$actions);
  78              $actions = explode(" ", $actions);
  79              
  80              $completedActions = array();
  81              foreach($actions as $action){
  82                  $action = trim($action);
  83                  if(strlen($action) > 0){
  84                      
  85                      if($action == '*')
  86                          $completedActions = array_merge($completedActions, static::getAllStringActions());
  87                      else
  88                          $completedActions[] = $action;
  89                  }
  90              }
  91              
  92              return $completedActions;
  93          }
  94          
  95          else{
  96              return static::processActionOperation($posMinus, $posPlus, $actions);
  97          }
  98      }
  99      
 100      /**

 101       * Processes the next operation on the actions and returns them

 102       * @param int $posMinus pos of the next minus-symbol in the string

 103       * @param int $posPlus pos of the next plus-symbol in the string

 104       * @param string $actions the action-string

 105       * @return array[string] the actions 

 106       */
 107      protected static function processActionOperation($posMinus, $posPlus, $actions){
 108          $firstPos = NULL;
 109              if($posMinus !== false && $posPlus !== false)
 110                  $firstPos = min($posMinus, $posPlus);
 111              elseif($posMinus === false)
 112                  $firstPos = $posPlus;
 113              else
 114                  $firstPos = $posMinus;
 115  
 116              $operation = $firstPos == $posMinus ? '-' : '+';
 117  
 118              $startStr = substr($actions, 0, $firstPos);
 119              $endStr = substr($actions, $firstPos + 1);
 120  
 121              $startActions = static::translateStringActions($startStr);
 122              $endActions = static::translateStringActions($endStr);
 123              
 124              if($operation == '+'){
 125                  return array_merge($startActions, $endActions);
 126              }
 127              else{
 128                  return array_diff($startActions, $endActions);
 129              }
 130      }
 131      
 132      /**

 133       * Fetches all actions from the database and returns them in an indexed array

 134       * @return array[string] the actinos 

 135       */
 136      protected static function getAllStringActions(){
 137          $actions = Action::model()->findAll();
 138          $sActions = array();
 139          
 140          foreach($actions as $action){
 141              $sActions[] = $action->name;
 142          }
 143          return $sActions;
 144      }
 145      
 146      /**

 147       * Returns the static model of the specified AR class.

 148       * @param string $className active record class name.

 149       * @return Action the static model class

 150       */
 151      public static function model($className=__CLASS__)
 152      {
 153          return parent::model($className);
 154      }
 155  
 156      /**

 157       * @return string the associated database table name

 158       */
 159      public function tableName()
 160      {
 161          return '{{action}}';
 162      }
 163  
 164      /**

 165       * @return array validation rules for model attributes.

 166       */
 167      public function rules()
 168      {
 169          // NOTE: you should only define rules for those attributes that

 170          // will receive user inputs.

 171          return array(
 172              array('name, created', 'required'),
 173              array('created', 'numerical', 'integerOnly'=>true),
 174              array('name', 'length', 'max'=>15),
 175              // The following rule is used by search().

 176              // Please remove those attributes that should not be searched.

 177              array('id, name, created', 'safe', 'on'=>'search'),
 178          );
 179      }
 180  
 181      /**

 182       * @return array relational rules.

 183       */
 184      public function relations()
 185      {
 186          // NOTE: you may need to adjust the relation name and the related

 187          // class name for the relations automatically generated below.

 188          return array(
 189          );
 190      }
 191  
 192      /**

 193       * @return array customized attribute labels (name=>label)

 194       */
 195      public function attributeLabels()
 196      {
 197          return array(
 198              'id' => 'ID',
 199              'name' => 'Name',
 200              'created' => 'Created',
 201          );
 202      }
 203  
 204      /**

 205       * Retrieves a list of models based on the current search/filter conditions.

 206       * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

 207       */
 208      public function search()
 209      {
 210          // Warning: Please modify the following code to remove attributes that

 211          // should not be searched.

 212  
 213          $criteria=new CDbCriteria;
 214  
 215          $criteria->compare('id',$this->id);
 216          $criteria->compare('name',$this->name,true);
 217          $criteria->compare('created',$this->created);
 218  
 219          return new CActiveDataProvider($this, array(
 220              'criteria'=>$criteria,
 221          ));
 222      }
 223  } 
 224  ?>


Generated: Sun Jul 1 19:24:45 2012 Cross-referenced by PHPXref 0.7.1