UsersController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. parent::init();
  32. if(Yii::$app->user->isGuest)
  33. {
  34. return $this->redirect('/user/login');
  35. }
  36. if(!Yii::$app->user->can("admin"))
  37. {
  38. return $this->redirect('/site/index');
  39. }
  40. }
  41. public function actionIndex()
  42. {
  43. $searchModel = new UserSearch();
  44. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  45. return $this->render('index', [
  46. 'searchModel' => $searchModel,
  47. 'dataProvider' => $dataProvider,
  48. ]);
  49. }
  50. /**
  51. * Displays a single Users model.
  52. * @param integer $id
  53. * @return mixed
  54. */
  55. public function actionView($id)
  56. {
  57. return $this->render('view', [
  58. 'model' => $this->findModel($id),
  59. ]);
  60. }
  61. /**
  62. * Creates a new Users model.
  63. * If creation is successful, the browser will be redirected to the 'view' page.
  64. * @return mixed
  65. */
  66. /*
  67. public function actionCreate()
  68. {
  69. $model = new Users();
  70. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  71. return $this->redirect(['view', 'id' => $model->id]);
  72. } else {
  73. return $this->render('create', [
  74. 'model' => $model,
  75. ]);
  76. }
  77. }
  78. */
  79. /**
  80. * Updates an existing Users model.
  81. * If update is successful, the browser will be redirected to the 'view' page.
  82. * @param integer $id
  83. * @return mixed
  84. */
  85. public function actionUpdate($id)
  86. {
  87. $model = $this->findModel($id);
  88. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  89. return $this->redirect(['view', 'id' => $model->id]);
  90. } else {
  91. return $this->render('update', [
  92. 'model' => $model,
  93. ]);
  94. }
  95. }
  96. /**
  97. * Deletes an existing Users model.
  98. * If deletion is successful, the browser will be redirected to the 'index' page.
  99. * @param integer $id
  100. * @return mixed
  101. */
  102. public function actionDelete($id)
  103. {
  104. $this->findModel($id)->delete();
  105. return $this->redirect(['index']);
  106. }
  107. /**
  108. * Finds the Users model based on its primary key value.
  109. * If the model is not found, a 404 HTTP exception will be thrown.
  110. * @param integer $id
  111. * @return Users the loaded model
  112. * @throws NotFoundHttpException if the model cannot be found
  113. */
  114. protected function findModel($id)
  115. {
  116. if (($model = User::findOne($id)) !== null) {
  117. return $model;
  118. } else {
  119. throw new NotFoundHttpException('The requested page does not exist.');
  120. }
  121. }
  122. }