UsersController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\controllers;
  3. use app\models\User;
  4. use Yii;
  5. use app\models\UsersSearch;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. /**
  10. * UsersController implements the CRUD actions for Users model.
  11. */
  12. class UsersController extends Controller
  13. {
  14. public function behaviors()
  15. {
  16. return [
  17. 'verbs' => [
  18. 'class' => VerbFilter::className(),
  19. 'actions' => [
  20. 'delete' => ['post'],
  21. ],
  22. ],
  23. ];
  24. }
  25. /**
  26. * Lists all Users models.
  27. * @return mixed
  28. */
  29. public function init()
  30. {
  31. if(Yii::$app->user->isGuest)
  32. {
  33. return $this->redirect('/user/login');
  34. }
  35. if(!Yii::$app->user->can("admin"))
  36. {
  37. return $this->redirect('/site/index');
  38. }
  39. }
  40. public function actionIndex()
  41. {
  42. $searchModel = new UserSearch();
  43. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  44. return $this->render('index', [
  45. 'searchModel' => $searchModel,
  46. 'dataProvider' => $dataProvider,
  47. ]);
  48. }
  49. /**
  50. * Displays a single Users model.
  51. * @param integer $id
  52. * @return mixed
  53. */
  54. public function actionView($id)
  55. {
  56. return $this->render('view', [
  57. 'model' => $this->findModel($id),
  58. ]);
  59. }
  60. /**
  61. * Creates a new Users model.
  62. * If creation is successful, the browser will be redirected to the 'view' page.
  63. * @return mixed
  64. */
  65. /*
  66. public function actionCreate()
  67. {
  68. $model = new Users();
  69. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  70. return $this->redirect(['view', 'id' => $model->id]);
  71. } else {
  72. return $this->render('create', [
  73. 'model' => $model,
  74. ]);
  75. }
  76. }
  77. */
  78. /**
  79. * Updates an existing Users model.
  80. * If update is successful, the browser will be redirected to the 'view' page.
  81. * @param integer $id
  82. * @return mixed
  83. */
  84. public function actionUpdate($id)
  85. {
  86. $model = $this->findModel($id);
  87. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  88. return $this->redirect(['view', 'id' => $model->id]);
  89. } else {
  90. return $this->render('update', [
  91. 'model' => $model,
  92. ]);
  93. }
  94. }
  95. /**
  96. * Deletes an existing Users model.
  97. * If deletion is successful, the browser will be redirected to the 'index' page.
  98. * @param integer $id
  99. * @return mixed
  100. */
  101. public function actionDelete($id)
  102. {
  103. $this->findModel($id)->delete();
  104. return $this->redirect(['index']);
  105. }
  106. /**
  107. * Finds the Users model based on its primary key value.
  108. * If the model is not found, a 404 HTTP exception will be thrown.
  109. * @param integer $id
  110. * @return Users the loaded model
  111. * @throws NotFoundHttpException if the model cannot be found
  112. */
  113. protected function findModel($id)
  114. {
  115. if (($model = User::findOne($id)) !== null) {
  116. return $model;
  117. } else {
  118. throw new NotFoundHttpException('The requested page does not exist.');
  119. }
  120. }
  121. }