Advertisement
fernandezekiel

Untitled

Feb 11th, 2013
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7.  
  8. /**
  9.  * Description of RestrictedController
  10.  *
  11.  * @author Web Developer
  12.  */
  13. class RestrictedController extends Controller{
  14.     //put your code here
  15.     public function loadModel($id=null, $modelClass = null, $action = null) {
  16.         if($modelClass == null){
  17.             //get id
  18.             //example CustomerCOntroller id = Customer
  19.             $modelClass = ucfirst($this->getId());
  20.         }
  21.        
  22.         if ($action == null){
  23.             $action = Yii::app()->controller->action->id;
  24.  
  25.         }
  26.        
  27.         /**
  28.          * @var RestrictedActiveRecord $model
  29.          *
  30.          */
  31.         if (is_string($modelClass)) {
  32.             $model = $modelClass::model();
  33.         } else if ($modelClass instanceof CActiveRecord) {
  34.             $model = $modelClass;
  35.         }
  36.         $model->hideSoftDeleted = false;
  37.         //$model->enableFiltering = false;
  38.         $model = $model->findByPk($id);
  39.         if ($model == null) {
  40.             Yii::app()->user->setFlash('danger', 'Page does not exist!');
  41.             Yii::app()->request->redirect(Yii::app()->getRequest()->getUrlReferrer());
  42.             // echo Yii::app()->getRequest()->getUrlReferrer();
  43.             //Yii::app()->end();
  44.         }
  45.         $accessTerms = $model->accessTerms;
  46.         $authItem = RestrictedActiveRecord::createAuthItem($action, $accessTerms['ownAccessTerm'], get_class($model));
  47.        
  48.         if($model::mayGenerally($model, $action)){
  49.             Yii::app()->user->setFlash('info', "You may generally $action this item " . get_class($model));
  50.            // Yii::app()->end();
  51.         }
  52.         else if ($model->owner_id != Yii::app()->user->id && Yii::app()->user->checkAccess($authItem)) {
  53.  
  54.             Yii::app()->user->setFlash('danger', 'You are not permitted to view this ' . get_class($model));
  55.             $this->redirect(Yii::app()->getRequest()->getUrlReferrer());
  56.         }
  57.        
  58.        if ($model->deleted){
  59.            Yii::app()->user->setFlash('warning', "this $modelClass is no longer active (deleted)");
  60.        }
  61.         return $model;
  62.        
  63.     }
  64. }
  65.  
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement