Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function restrictCriteria($model, $conditions = '', $params = array(), $options = array()) {
- $user = self::getUser();
- if (is_string($model)) {
- $model = OwnedActiveRecord::model($model);
- }
- if (is_object($conditions) && $conditions instanceof CDbCriteria) {
- $criteria = $conditions;
- } else {
- $criteria = new CDbCriteria;
- $criteria->mergeWith(array(
- 'condition' => $conditions,
- 'params' => $params
- ));
- }
- $alias = $model->alias;
- if (isset($model->SoftDeleteBehavior) && $model->hideSoftDeleted) {
- if ($model->hasAttribute($model->SoftDeleteBehavior->deleteColumn))
- $criteria->mergeWith(array('alias' => $alias, 'condition' => $alias . '.deleted!=1 OR ' . $alias . '.deleted IS NULL'));
- }
- if ($model->enableFiltering == false)
- return $criteria;
- $branch_id = $user->profile->branch_id;
- $branchFilter = new CDbCriteria;
- if ($branch_id != Branch::ROOT_BRANCH) {
- $branchFilter->with = array('owner' => array('alias' => 'ow', 'with' => array('profile' => array('alias' => 'owp'))));
- $branchFilter->condition = "owp.branch_id=" . $branch_id;
- /*$branchFilter = array(
- 'with' => array('owner' => array('alias' => 'ow', 'with' => array('profile' => array('alias' => 'owp')))),
- 'condition' => "owp.branch_id=" . $branch_id,
- );*/
- }
- if (self::mayGenerally($model, $model->accessTerms['readAccessTerm'])) {
- $criteria->mergeWith($branchFilter);
- return $criteria;
- }
- $action = 'view';
- $model_class = $model::getModelName();
- $authItem = self::createAuthItem($action, $model->accessTerms['generalAccessTerm'], $model_class);
- if ($model->autoAuthItemMatch == false) {
- $accessRules = $model->accessRules();
- $authtem = null;
- if (array_key_exists($action, $accessRules)) {
- if (array_key_exists($model->accessTerms['generalAccessTerm'], $accessRules[$action]))
- $authItem = $accessRules[$action][$model->accessTerms['generalAccessTerm']];
- }
- }
- if (Yii::app()->user->checkAccess($authItem) && $authItem != null) {
- $criteria->mergeWith($branchFilter);
- return $criteria;
- }
- $authItem = self::createAuthItem($action, $model->accessTerms['ownAccessTerm'], $model_class);
- if ($model->autoAuthItemMatch == false) {
- $accessRules = $model->accessRules();
- $authtem = null;
- if (array_key_exists($action, $accessRules)) {
- if (array_key_exists($model->accessTerms['ownAccessTerm'], $accessRules[$action]))
- $authItem = $accessRules[$action][$model->accessTerms['ownAccessTerm']];
- }
- }
- $userSpecificCriteria = new CDbCriteria;
- if (Yii::app()->user->checkAccess($authItem) && $authItem != null) {
- $userSpecificCriteria->mergeWith(array(
- 'alias' => $alias,
- 'condition' => $alias . '.' . $model::$ownerAttribute . '=' . $user->id,
- ));
- }
- if ($model->hasAttribute(self::$coordinatorAttribute)) {
- $authItem = self::createAuthItem($action, $model->accessTerms['coordinatorAccessTerm'], $model_class);
- if (Yii::app()->user->checkAccess($authItem)) {
- $userSpecificCriteria->mergeWith(array(
- 'alias' => $alias,
- 'condition' => $alias . '.' . self::$coordinatorAttribute . '=' . $user->id,
- ), 'OR');
- }
- }
- $userSpecificCriteria = $model->customUserAccessCheck($userSpecificCriteria);
- $branchFilter->mergeWith($userSpecificCriteria);
- $branchFilter = $model->interBranchCriteria($branchFilter);
- $criteria->mergeWith($branchFilter);
- $criteria = $model->beforeRestrictReturn($criteria);
- return $criteria;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement