toir427 /
yii2-admin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace toir427\admin\controllers; |
||
| 4 | |||
| 5 | use toir427\admin\components\UserStatus; |
||
| 6 | use toir427\admin\models\form\ChangePassword; |
||
| 7 | use toir427\admin\models\form\Login; |
||
| 8 | use toir427\admin\models\form\PasswordResetRequest; |
||
| 9 | use toir427\admin\models\form\ResetPassword; |
||
| 10 | use toir427\admin\models\form\Signup; |
||
| 11 | use toir427\admin\models\searchs\User as UserSearch; |
||
| 12 | use toir427\admin\models\User; |
||
| 13 | use Yii; |
||
| 14 | use yii\base\InvalidParamException; |
||
| 15 | use yii\base\UserException; |
||
| 16 | use yii\filters\VerbFilter; |
||
| 17 | use yii\mail\BaseMailer; |
||
| 18 | use yii\web\BadRequestHttpException; |
||
| 19 | use yii\web\Controller; |
||
| 20 | use yii\web\NotFoundHttpException; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * User controller |
||
| 24 | */ |
||
| 25 | class UserController extends Controller |
||
| 26 | { |
||
| 27 | private $_oldMailPath; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @inheritdoc |
||
| 31 | */ |
||
| 32 | public function behaviors() |
||
| 33 | { |
||
| 34 | return [ |
||
| 35 | 'verbs' => [ |
||
| 36 | 'class' => VerbFilter::className(), |
||
| 37 | 'actions' => [ |
||
| 38 | 'delete' => ['post'], |
||
| 39 | 'logout' => ['post'], |
||
| 40 | 'activate' => ['post'], |
||
| 41 | ], |
||
| 42 | ], |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritdoc |
||
| 48 | */ |
||
| 49 | public function beforeAction($action) |
||
| 50 | { |
||
| 51 | return true; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritdoc |
||
| 56 | */ |
||
| 57 | public function afterAction($action, $result) |
||
| 58 | { |
||
| 59 | if ($this->_oldMailPath !== null) { |
||
| 60 | Yii::$app->getMailer()->setViewPath($this->_oldMailPath); |
||
| 61 | } |
||
| 62 | return parent::afterAction($action, $result); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Lists all User models. |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function actionIndex() |
||
| 70 | { |
||
| 71 | $searchModel = new UserSearch(); |
||
| 72 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
||
| 73 | |||
| 74 | return $this->render('index', [ |
||
| 75 | 'searchModel' => $searchModel, |
||
| 76 | 'dataProvider' => $dataProvider, |
||
| 77 | ]); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Displays a single User model. |
||
| 82 | * @param integer $id |
||
| 83 | * @return mixed |
||
| 84 | */ |
||
| 85 | public function actionView($id) |
||
| 86 | { |
||
| 87 | return $this->render('view', [ |
||
| 88 | 'model' => $this->findModel($id), |
||
| 89 | ]); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Deletes an existing User model. |
||
| 94 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
| 95 | * @param integer $id |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | public function actionDelete($id) |
||
| 99 | { |
||
| 100 | $this->findModel($id)->delete(); |
||
| 101 | |||
| 102 | return $this->redirect(['index']); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Login |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function actionLogin() |
||
| 110 | { |
||
| 111 | if (!Yii::$app->getUser()->isGuest) { |
||
| 112 | return $this->goHome(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 113 | } |
||
| 114 | |||
| 115 | $model = new Login(); |
||
| 116 | if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) { |
||
| 117 | return $this->goBack(); |
||
|
0 ignored issues
–
show
|
|||
| 118 | } else { |
||
| 119 | return $this->render('login', [ |
||
| 120 | 'model' => $model, |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Logout |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function actionLogout() |
||
| 130 | { |
||
| 131 | Yii::$app->getUser()->logout(); |
||
| 132 | |||
| 133 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Signup new user |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function actionSignup() |
||
| 141 | { |
||
| 142 | $model = new Signup(); |
||
| 143 | if ($model->load(Yii::$app->getRequest()->post())) { |
||
| 144 | if ($user = $model->signup()) { |
||
| 145 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->render('signup', [ |
||
| 150 | 'model' => $model, |
||
| 151 | ]); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Request reset password |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function actionRequestPasswordReset() |
||
| 159 | { |
||
| 160 | $model = new PasswordResetRequest(); |
||
| 161 | if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) { |
||
| 162 | if ($model->sendEmail()) { |
||
| 163 | Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); |
||
| 164 | |||
| 165 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 166 | } else { |
||
| 167 | Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.'); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return $this->render('requestPasswordResetToken', [ |
||
| 172 | 'model' => $model, |
||
| 173 | ]); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Reset password |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function actionResetPassword($token) |
||
| 181 | { |
||
| 182 | try { |
||
| 183 | $model = new ResetPassword($token); |
||
| 184 | } catch (InvalidParamException $e) { |
||
| 185 | throw new BadRequestHttpException($e->getMessage()); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->resetPassword()) { |
||
| 189 | Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
||
| 190 | |||
| 191 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 192 | } |
||
| 193 | |||
| 194 | return $this->render('resetPassword', [ |
||
| 195 | 'model' => $model, |
||
| 196 | ]); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Reset password |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function actionChangePassword() |
||
| 204 | { |
||
| 205 | $model = new ChangePassword(); |
||
| 206 | if ($model->load(Yii::$app->getRequest()->post()) && $model->change()) { |
||
| 207 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 208 | } |
||
| 209 | |||
| 210 | return $this->render('change-password', [ |
||
| 211 | 'model' => $model, |
||
| 212 | ]); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Activate new user |
||
| 217 | * @param integer $id |
||
| 218 | * @return type |
||
|
0 ignored issues
–
show
The type
toir427\admin\controllers\type was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 219 | * @throws UserException |
||
| 220 | * @throws NotFoundHttpException |
||
| 221 | */ |
||
| 222 | public function actionActivate($id) |
||
| 223 | { |
||
| 224 | /* @var $user User */ |
||
| 225 | $user = $this->findModel($id); |
||
| 226 | if ($user->status == UserStatus::INACTIVE) { |
||
| 227 | $user->status = UserStatus::ACTIVE; |
||
| 228 | if ($user->save()) { |
||
| 229 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 230 | } else { |
||
| 231 | $errors = $user->firstErrors; |
||
| 232 | throw new UserException(reset($errors)); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | return $this->goHome(); |
||
|
0 ignored issues
–
show
|
|||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Finds the User model based on its primary key value. |
||
| 240 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 241 | * @param integer $id |
||
| 242 | * @return User the loaded model |
||
| 243 | * @throws NotFoundHttpException if the model cannot be found |
||
| 244 | */ |
||
| 245 | protected function findModel($id) |
||
| 246 | { |
||
| 247 | if (($model = User::findOne($id)) !== null) { |
||
| 248 | return $model; |
||
| 249 | } else { |
||
| 250 | throw new NotFoundHttpException('The requested page does not exist.'); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 |