You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SiteController.php 2.0KB

8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use common\models\LoginForm;
  7. use common\models\User;
  8. use yii\filters\VerbFilter;
  9. /**
  10. * Site controller
  11. */
  12. class SiteController extends Controller
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. public function behaviors()
  18. {
  19. return [
  20. 'access' => [
  21. 'class' => AccessControl::className(),
  22. 'rules' => [
  23. [
  24. 'actions' => ['login', 'error'],
  25. 'allow' => true,
  26. ],
  27. [
  28. 'actions' => ['logout', 'index'],
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. 'matchCallback' => function ($rule, $action) {
  32. return Yii::$app->user->identity->status == User::STATUS_ADMIN ;
  33. }
  34. ],
  35. ],
  36. ],
  37. 'verbs' => [
  38. 'class' => VerbFilter::className(),
  39. 'actions' => [
  40. 'logout' => ['post'],
  41. ],
  42. ],
  43. ];
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function actions()
  49. {
  50. return [
  51. 'error' => [
  52. 'class' => 'yii\web\ErrorAction',
  53. ],
  54. ];
  55. }
  56. public function actionIndex()
  57. {
  58. return $this->render('index');
  59. }
  60. public function actionLogin()
  61. {
  62. if (!\Yii::$app->user->isGuest) {
  63. return $this->goHome();
  64. }
  65. $model = new LoginForm();
  66. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  67. return $this->goBack();
  68. } else {
  69. return $this->render('login', [
  70. 'model' => $model,
  71. ]);
  72. }
  73. }
  74. public function actionLogout()
  75. {
  76. Yii::$app->user->logout();
  77. return $this->goHome();
  78. }
  79. }