| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @link http://www.writesdown.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2015 WritesDown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license http://www.writesdown.com/license/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace backend\controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use common\models\search\User as UserSearch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use common\models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use yii\filters\AccessControl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\filters\VerbFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\web\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use yii\web\ForbiddenHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use yii\web\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * UserController, controlling the actions for User model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Agiel K. Saputra <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class UserController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 30 |  | View Code Duplication |     public function behaviors() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'access' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 'class' => AccessControl::className(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'rules' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                         'actions' => ['index', 'create', 'update', 'delete', 'bulk-action'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                         'allow' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                         'roles' => ['administrator'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                         'actions' => ['profile', 'view', 'reset-password'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         'allow' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         'roles' => ['@'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'verbs' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'class' => VerbFilter::className(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'actions' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     'delete' => ['post'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'bulk-action' => ['post'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Lists all User models. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     public function actionIndex() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $searchModel = new UserSearch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $dataProvider = $searchModel->search(Yii::$app->request->queryParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $this->render('index', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'searchModel' => $searchModel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'dataProvider' => $dataProvider, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Displays a single User model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function actionView($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->render('view', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'model' => $this->findModel($id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Creates a new User model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * If creation is successful, the browser will be redirected to the 'view' page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function actionCreate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // Set scenario to register, so that the password is required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $model = new User(['scenario' => 'register']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if ($model->load(Yii::$app->request->post())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $model->generateAuthKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $model->setPassword($model->password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             if ($model->save()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 Yii::$app->authManager->assign(Yii::$app->authManager->getRole($model->role), $model->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 return $this->redirect(['view', 'id' => $model->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->render('create', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             'model' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Updates an existing User model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * If update is successful, the browser will be redirected to the 'view' page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @throws \yii\web\ForbiddenHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws \yii\web\NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function actionUpdate($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if ($model->load(Yii::$app->request->post()) && $model->save()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             Yii::$app->authManager->revokeAll($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             Yii::$app->authManager->assign(Yii::$app->authManager->getRole($model->role), $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             return $this->redirect(['view', 'id' => $model->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return $this->render('update', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'model' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Deletes an existing User model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * If deletion is successful, the browser will be redirected to the 'index' page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @throws \yii\web\ForbiddenHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @throws \yii\web\NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function actionDelete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $model->delete(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $this->redirect(['index']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * Bulk action for User triggered when button 'Apply' clicked. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * The action depends on the value of the dropdown next to the button. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * Only accept POST HTTP method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function actionBulkAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         if (Yii::$app->request->post('action') === 'active') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             foreach (Yii::$app->request->post('ids', []) as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 $model->updateAttributes(['status' => User::STATUS_ACTIVE]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 170 |  | View Code Duplication |         } elseif (Yii::$app->request->post('action') === 'not-active') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             foreach (Yii::$app->request->post('ids', []) as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $model->updateAttributes(['status' => User::STATUS_NOT_ACTIVE]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } elseif (Yii::$app->request->post('action') === 'removed') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             foreach (Yii::$app->request->post('ids', []) as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                 $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 $model->updateAttributes(['status' => User::STATUS_REMOVED]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } elseif (Yii::$app->request->post('action') === 'deleted') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             foreach (Yii::$app->request->post('ids', []) as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 $model->delete(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         } elseif (Yii::$app->request->post('action') === 'changerole') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             foreach (Yii::$app->request->post('ids', []) as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 $model = $this->findModel($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 $this->checkPermission($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                 Yii::$app->authManager->revokeAll($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 Yii::$app->authManager->assign(Yii::$app->authManager->getRole(Yii::$app->request->post('role')), $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * Update the user data for the user who has logged in. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @return string|\yii\web\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @throws \yii\web\NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 204 |  | View Code Duplication |     public function actionProfile() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $model = $this->findModel(Yii::$app->user->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         if ($model->load(Yii::$app->request->post()) && $model->save()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             return $this->redirect(['view', 'id' => $model->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         return $this->render('profile', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             'model' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      * Reset password for logged user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      * It requires three inputs from users to reset password, they are the old password, new password, and repeat password. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |      * The old password will be checked by User::passwordValidation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      * @return string|\yii\web\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |      * @throws \yii\web\NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |     public function actionResetPassword() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         $model = $this->findModel(Yii::$app->user->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         $model->setScenario('resetPassword'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         if ($model->load(Yii::$app->request->post())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             $model->setPassword($model->password); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             if ($model->save()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |                 return $this->redirect(['view', 'id' => $model->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         return $this->render('reset-password', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             'model' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * Finds the User model based on its primary key value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * If the model is not found, a 404 HTTP exception will be thrown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      * @param integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |      * @return User the loaded model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      * @throws NotFoundHttpException if the model cannot be found | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 249 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |     protected function findModel($id) | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |         if (($model = User::findOne($id)) !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |             return $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |         throw new NotFoundHttpException('The requested page does not exist.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * @param $model User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      * @throws ForbiddenHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     protected function checkPermission($model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         if (!$model->checkPermission()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |             throw new ForbiddenHttpException(Yii::t('writesdown', 'You are not allowed to perform this action.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 269 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 270 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.