Dominus77 /
yii2-basic-start
| 1 | <?php |
||
| 2 | |||
| 3 | namespace modules\admin\controllers; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\web\Response; |
||
| 7 | use yii\web\Controller; |
||
| 8 | use yii\web\NotFoundHttpException; |
||
| 9 | use yii\filters\VerbFilter; |
||
| 10 | use yii\filters\AccessControl; |
||
| 11 | use modules\admin\models\search\UserSearch; |
||
| 12 | use modules\admin\models\User; |
||
| 13 | use modules\admin\Module; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class UserController |
||
| 17 | * @package modules\admin\controllers |
||
| 18 | */ |
||
| 19 | class UserController extends Controller |
||
| 20 | { |
||
| 21 | /** @var string|bool $jsFile */ |
||
| 22 | protected $jsFile; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @inheritdoc |
||
| 26 | */ |
||
| 27 | public function behaviors() |
||
| 28 | { |
||
| 29 | return [ |
||
| 30 | 'access' => [ |
||
| 31 | 'class' => AccessControl::class, |
||
| 32 | 'rules' => [ |
||
| 33 | [ |
||
| 34 | 'allow' => true, |
||
| 35 | 'roles' => [\modules\rbac\models\Permission::PERMISSION_MANAGER_USERS], |
||
| 36 | ], |
||
| 37 | ], |
||
| 38 | ], |
||
| 39 | 'verbs' => [ |
||
| 40 | 'class' => VerbFilter::class, |
||
| 41 | 'actions' => [ |
||
| 42 | 'delete' => ['POST'], |
||
| 43 | ], |
||
| 44 | ], |
||
| 45 | ]; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | public function init() |
||
| 52 | { |
||
| 53 | parent::init(); |
||
| 54 | $this->processRegisterJs(); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Publish and register the required JS file |
||
| 59 | */ |
||
| 60 | protected function processRegisterJs() |
||
| 61 | { |
||
| 62 | $this->jsFile = '@modules/users/views/ajax/ajax.js'; |
||
| 63 | $assetManager = Yii::$app->assetManager; |
||
| 64 | $assetManager->publish($this->jsFile); |
||
| 65 | $url = $assetManager->getPublishedUrl($this->jsFile); |
||
| 66 | $this->view->registerJsFile( |
||
| 67 | $url, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | ['depends' => 'yii\web\JqueryAsset',] // depends |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Lists all User models. |
||
| 74 | * @return string |
||
| 75 | * @throws \yii\base\InvalidConfigException |
||
| 76 | */ |
||
| 77 | public function actionIndex() |
||
| 78 | { |
||
| 79 | $searchModel = new UserSearch(); |
||
| 80 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
||
| 81 | $assignModel = new \modules\rbac\models\Assignment(); |
||
| 82 | return $this->render('index', [ |
||
| 83 | 'searchModel' => $searchModel, |
||
| 84 | 'dataProvider' => $dataProvider, |
||
| 85 | 'assignModel' => $assignModel, |
||
| 86 | ]); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Displays a single User model. |
||
| 91 | * @param integer $id |
||
| 92 | * @return mixed |
||
| 93 | * @throws NotFoundHttpException if the model cannot be found |
||
| 94 | */ |
||
| 95 | public function actionView($id) |
||
| 96 | { |
||
| 97 | return $this->render('view', [ |
||
| 98 | 'model' => $this->findModel($id), |
||
| 99 | ]); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Creates a new User model. |
||
| 104 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | public function actionCreate() |
||
| 108 | { |
||
| 109 | $model = new User(); |
||
| 110 | $model->scenario = $model::SCENARIO_ADMIN_CREATE; |
||
| 111 | |||
| 112 | if ($model->load(Yii::$app->request->post())) { |
||
| 113 | $model->generateAuthKey(); |
||
| 114 | if ($model->save()) { |
||
| 115 | return $this->redirect(['view', 'id' => $model->id]); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->render('create', [ |
||
| 120 | 'model' => $model, |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Updates an existing User model. |
||
| 126 | * If update is successful, the browser will be redirected to the 'view' page. |
||
| 127 | * @param integer $id |
||
| 128 | * @return mixed |
||
| 129 | * @throws NotFoundHttpException if the model cannot be found |
||
| 130 | */ |
||
| 131 | public function actionUpdate($id) |
||
| 132 | { |
||
| 133 | $model = $this->findModel($id); |
||
| 134 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
||
| 135 | return $this->redirect(['view', 'id' => $model->id]); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this->render('update', [ |
||
| 139 | 'model' => $model, |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Deletes an existing User model. |
||
| 145 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
| 146 | * @param integer $id |
||
| 147 | * @return Response |
||
| 148 | * @throws NotFoundHttpException |
||
| 149 | * @throws \Exception |
||
| 150 | * @throws \Throwable |
||
| 151 | * @throws \yii\db\StaleObjectException |
||
| 152 | */ |
||
| 153 | public function actionDelete($id) |
||
| 154 | { |
||
| 155 | $model = $this->findModel($id); |
||
| 156 | /** @var object $identity */ |
||
| 157 | $identity = Yii::$app->user->identity; |
||
| 158 | if ($model->id !== $identity->id || $model->isSuperAdmin($model->id)) { |
||
| 159 | $model->delete(); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this->redirect(['index']); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Action Generate new auth key |
||
| 167 | * @param int|string $id |
||
| 168 | * @return array|Response |
||
| 169 | * @throws NotFoundHttpException |
||
| 170 | */ |
||
| 171 | public function actionGenerateAuthKey($id) |
||
| 172 | { |
||
| 173 | $model = $this->processGenerateAuthKey($id); |
||
| 174 | if (Yii::$app->request->isAjax) { |
||
| 175 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 176 | return [ |
||
| 177 | 'success' => $model->auth_key, |
||
| 178 | ]; |
||
| 179 | } |
||
| 180 | return $this->redirect(['index']); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate new auth key |
||
| 185 | * @param int|string $id |
||
| 186 | * @return User |
||
| 187 | * @throws NotFoundHttpException |
||
| 188 | */ |
||
| 189 | private function processGenerateAuthKey($id) |
||
| 190 | { |
||
| 191 | $model = $this->findModel($id); |
||
| 192 | $model->generateAuthKey(); |
||
| 193 | $model->save(false); |
||
| 194 | return $model; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Change Status |
||
| 199 | * @param int|string $id |
||
| 200 | * @return array|\yii\web\Response |
||
| 201 | * @throws NotFoundHttpException |
||
| 202 | */ |
||
| 203 | public function actionStatus($id) |
||
| 204 | { |
||
| 205 | if ($model = $this->processChangeStatus($id)) { |
||
| 206 | if (Yii::$app->request->isAjax) { |
||
| 207 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 208 | return [ |
||
| 209 | 'body' => $model->getStatusLabelName(), |
||
| 210 | 'success' => true, |
||
| 211 | ]; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | return $this->redirect(['index']); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param int|string $id |
||
| 219 | * @return bool|User|null |
||
| 220 | * @throws NotFoundHttpException |
||
| 221 | */ |
||
| 222 | private function processChangeStatus($id) |
||
| 223 | { |
||
| 224 | $model = $this->findModel($id); |
||
| 225 | /** @var object $identity */ |
||
| 226 | $identity = Yii::$app->user->identity; |
||
| 227 | if ($model->id !== $identity->id || $model->isSuperAdmin($model->id)) { |
||
| 228 | $model->setStatus(); |
||
| 229 | $model->save(false); |
||
| 230 | return $model; |
||
| 231 | } |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Finds the User model based on its primary key value. |
||
| 237 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 238 | * @param int|string $id |
||
| 239 | * @return User the loaded model |
||
| 240 | * @throws NotFoundHttpException if the model cannot be found |
||
| 241 | */ |
||
| 242 | protected function findModel($id) |
||
| 243 | { |
||
| 244 | if (($model = User::findOne($id)) !== null) { |
||
| 245 | return $model; |
||
| 246 | } |
||
| 247 | |||
| 248 | throw new NotFoundHttpException(Module::t('users', 'The requested page does not exist.')); |
||
| 249 | } |
||
| 250 | } |
||
| 251 |