Advertisement
fernandezekiel

Untitled

Oct 9th, 2013
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.18 KB | None | 0 0
  1. public static function restrictCriteria($model, $conditions = '', $params = array(), $options = array()) {
  2.         $user = self::getUser();
  3.  
  4.         if (is_string($model)) {
  5.             $model = OwnedActiveRecord::model($model);
  6.         }
  7.         if (is_object($conditions) && $conditions instanceof CDbCriteria) {
  8.             $criteria = $conditions;
  9.         } else {
  10.             $criteria = new CDbCriteria;
  11.             $criteria->mergeWith(array(
  12.                 'condition' => $conditions,
  13.                 'params' => $params
  14.             ));
  15.         }
  16.  
  17.  
  18.         $alias = $model->alias;
  19.  
  20.         if (isset($model->SoftDeleteBehavior) && $model->hideSoftDeleted) {
  21.             if ($model->hasAttribute($model->SoftDeleteBehavior->deleteColumn))
  22.                 $criteria->mergeWith(array('alias' => $alias, 'condition' => $alias . '.deleted!=1 OR ' . $alias . '.deleted IS NULL'));
  23.         }
  24.  
  25.         if ($model->enableFiltering == false)
  26.             return $criteria;
  27.  
  28.  
  29.         $branch_id = $user->profile->branch_id;
  30.         $branchFilter = new CDbCriteria;
  31.         if ($branch_id != Branch::ROOT_BRANCH) {
  32.             $branchFilter->with =  array('owner' => array('alias' => 'ow', 'with' => array('profile' => array('alias' => 'owp'))));
  33.             $branchFilter->condition =  "owp.branch_id=" . $branch_id;
  34.             /*$branchFilter = array(
  35.                 'with' => array('owner' => array('alias' => 'ow', 'with' => array('profile' => array('alias' => 'owp')))),
  36.                 'condition' => "owp.branch_id=" . $branch_id,
  37.             );*/
  38.         }
  39.  
  40.         if (self::mayGenerally($model, $model->accessTerms['readAccessTerm'])) {
  41.             $criteria->mergeWith($branchFilter);
  42.             return $criteria;
  43.         }
  44.  
  45.         $action = 'view';
  46.         $model_class = $model::getModelName();
  47.  
  48.         $authItem = self::createAuthItem($action, $model->accessTerms['generalAccessTerm'], $model_class);
  49.  
  50.         if ($model->autoAuthItemMatch == false) {
  51.             $accessRules = $model->accessRules();
  52.             $authtem = null;
  53.             if (array_key_exists($action, $accessRules)) {
  54.                 if (array_key_exists($model->accessTerms['generalAccessTerm'], $accessRules[$action]))
  55.                     $authItem = $accessRules[$action][$model->accessTerms['generalAccessTerm']];
  56.             }
  57.         }
  58.  
  59.         if (Yii::app()->user->checkAccess($authItem) && $authItem != null) {
  60.             $criteria->mergeWith($branchFilter);
  61.             return $criteria;
  62.         }
  63.  
  64.         $authItem = self::createAuthItem($action, $model->accessTerms['ownAccessTerm'], $model_class);
  65.         if ($model->autoAuthItemMatch == false) {
  66.             $accessRules = $model->accessRules();
  67.             $authtem = null;
  68.             if (array_key_exists($action, $accessRules)) {
  69.                 if (array_key_exists($model->accessTerms['ownAccessTerm'], $accessRules[$action]))
  70.                     $authItem = $accessRules[$action][$model->accessTerms['ownAccessTerm']];
  71.             }
  72.         }
  73.  
  74.         $userSpecificCriteria = new CDbCriteria;
  75.         if (Yii::app()->user->checkAccess($authItem) && $authItem != null) {
  76.             $userSpecificCriteria->mergeWith(array(
  77.                 'alias' => $alias,
  78.                 'condition' => $alias . '.' . $model::$ownerAttribute . '=' . $user->id,
  79.             ));
  80.         }
  81.         if ($model->hasAttribute(self::$coordinatorAttribute)) {
  82.             $authItem = self::createAuthItem($action, $model->accessTerms['coordinatorAccessTerm'], $model_class);
  83.             if (Yii::app()->user->checkAccess($authItem)) {
  84.                 $userSpecificCriteria->mergeWith(array(
  85.                     'alias' => $alias,
  86.                     'condition' => $alias . '.' . self::$coordinatorAttribute . '=' . $user->id,
  87.                         ), 'OR');
  88.             }
  89.         }
  90.  
  91.         $userSpecificCriteria = $model->customUserAccessCheck($userSpecificCriteria);
  92.  
  93.         $branchFilter->mergeWith($userSpecificCriteria);
  94.         $branchFilter = $model->interBranchCriteria($branchFilter);
  95.         $criteria->mergeWith($branchFilter);
  96.        
  97.         $criteria = $model->beforeRestrictReturn($criteria);
  98.         return $criteria;
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement