Guest User

Untitled

a guest
Oct 5th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. <?php
  2.  
  3. class UserController extends Controller
  4. {
  5.     /**
  6.      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  7.      * using two-column layout. See 'protected/views/layouts/column2.php'.
  8.      */
  9.     public $layout='//layouts/column2';
  10.  
  11.     /**
  12.      * @return array action filters
  13.      */
  14.     public function filters()
  15.     {
  16.         return array(
  17.             'accessControl', // perform access control for CRUD operations
  18.             'postOnly + delete', // we only allow deletion via POST request
  19.         );
  20.     }
  21.  
  22.     /**
  23.      * Specifies the access control rules.
  24.      * This method is used by the 'accessControl' filter.
  25.      * @return array access control rules
  26.      */
  27.     public function accessRules()
  28.     {
  29.         return array(
  30.             array('allow',  // allow all users to perform 'index' and 'view' actions
  31.                 'actions'=>array('view'),
  32.                 'users'=>array('@'),
  33.             ),
  34.             array('allow',  // allow all users to perform 'index' and 'view' actions
  35.                 'actions'=>array('index','view','create','update','admin','delete'),
  36.                 'users'=>array('@'),
  37.                                 'expression'=>'$user->isAdmin()',
  38.             ),
  39.             array('deny',  // deny all users
  40.                 'users'=>array('*'),
  41.             ),
  42.         );
  43.     }
  44.  
  45.     /**
  46.      * Displays a particular model.
  47.      * @param integer $id the ID of the model to be displayed
  48.      */
  49.     public function actionView($id)
  50.     {
  51.         $this->render('view',array(
  52.             'model'=>$this->loadModel($id),
  53.         ));
  54.     }
  55.  
  56.     /**
  57.      * Creates a new model.
  58.      * If creation is successful, the browser will be redirected to the 'view' page.
  59.      */
  60.     public function actionCreate()
  61.     {
  62.         $model=new User('insert');
  63.  
  64.         // Uncomment the following line if AJAX validation is needed
  65.         // $this->performAjaxValidation($model);
  66.                
  67.         if(isset($_POST['User']))
  68.         {
  69.                    
  70.             $model->attributes=$_POST['User'];
  71.                         if ($model->validate()){
  72.                         $model->salt = uniqid();
  73.                         $hashPassword = User::model()->hashPassword($model->password, $model->salt);
  74.                         $model->password = $hashPassword;
  75.                        
  76.             if($model->save(false))
  77.                 $this->redirect(array('view','id'=>$model->id));
  78.                     }
  79.         }
  80.                
  81.                
  82.         $this->render('create',array(
  83.             'model'=>$model,
  84.         ));
  85.     }
  86.  
  87.     /**
  88.      * Updates a particular model.
  89.      * If update is successful, the browser will be redirected to the 'view' page.
  90.      * @param integer $id the ID of the model to be updated
  91.      */
  92.     public function actionUpdate($id)
  93.     {
  94.         $model=$this->loadModel($id);
  95.                 $oldpassword = $model->password;
  96.                 $model->password='';
  97.  
  98.         // Uncomment the following line if AJAX validation is needed
  99.         // $this->performAjaxValidation($model);
  100.  
  101.         if(isset($_POST['User']))
  102.         {
  103.             $model->attributes=$_POST['User'];
  104.                         if ($model->password==''){
  105.                             $model->setScenario('noPassChange');
  106.                             $model->password = $oldpassword;
  107.                             if($model->save())
  108.                 $this->redirect(array('view','id'=>$model->id));
  109.                            
  110.                         }
  111.                         else{
  112.                         $model->salt =  uniqid();
  113.                         $hashPassword = User::model()->hashPassword($model->password, $model->salt);
  114.                         $model->password = $hashPassword;
  115.             if($model->save())
  116.                 $this->redirect(array('view','id'=>$model->id));
  117.                         }
  118.         }
  119.  
  120.         $this->render('update',array(
  121.             'model'=>$model,
  122.         ));
  123.     }
  124.  
  125.     /**
  126.      * Deletes a particular model.
  127.      * If deletion is successful, the browser will be redirected to the 'admin' page.
  128.      * @param integer $id the ID of the model to be deleted
  129.      */
  130.     public function actionDelete($id)
  131.     {
  132.         $this->loadModel($id)->delete();
  133.  
  134.         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  135.         if(!isset($_GET['ajax']))
  136.             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  137.     }
  138.  
  139.     /**
  140.      * Lists all models.
  141.      */
  142.     public function actionIndex()
  143.     {
  144.         $dataProvider=new CActiveDataProvider('User');
  145.         $this->render('index',array(
  146.             'dataProvider'=>$dataProvider,
  147.         ));
  148.     }
  149.  
  150.     /**
  151.      * Manages all models.
  152.      */
  153.     public function actionAdmin()
  154.     {
  155.         $model=new User('search');
  156.         $model->unsetAttributes();  // clear any default values
  157.         if(isset($_GET['User']))
  158.             $model->attributes=$_GET['User'];
  159.  
  160.         $this->render('admin',array(
  161.             'model'=>$model,
  162.         ));
  163.     }
  164.  
  165.     /**
  166.      * Returns the data model based on the primary key given in the GET variable.
  167.      * If the data model is not found, an HTTP exception will be raised.
  168.      * @param integer the ID of the model to be loaded
  169.      */
  170.     public function loadModel($id)
  171.     {
  172.         $model=User::model()->findByPk($id);
  173.         if($model===null)
  174.             throw new CHttpException(404,'The requested page does not exist.');
  175.         return $model;
  176.     }
  177.  
  178.     /**
  179.      * Performs the AJAX validation.
  180.      * @param CModel the model to be validated
  181.      */
  182.     protected function performAjaxValidation($model)
  183.     {
  184.         if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
  185.         {
  186.             echo CActiveForm::validate($model);
  187.             Yii::app()->end();
  188.         }
  189.     }
  190. }
Add Comment
Please, Sign In to add comment