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.1KB

пре 8 година
пре 8 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  34. }
  35. ],
  36. ],
  37. ],
  38. 'verbs' => [
  39. 'class' => VerbFilter::className(),
  40. 'actions' => [
  41. 'logout' => ['post'],
  42. ],
  43. ],
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function actions()
  50. {
  51. return [
  52. 'error' => [
  53. 'class' => 'yii\web\ErrorAction',
  54. ],
  55. ];
  56. }
  57. public function actionIndex()
  58. {
  59. return $this->render('index');
  60. }
  61. public function actionLogin()
  62. {
  63. if (!\Yii::$app->user->isGuest) {
  64. return $this->goHome();
  65. }
  66. $model = new LoginForm();
  67. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  68. return $this->goBack();
  69. } else {
  70. return $this->render('login', [
  71. 'model' => $model,
  72. ]);
  73. }
  74. }
  75. public function actionLogout()
  76. {
  77. Yii::$app->user->logout();
  78. return $this->goHome();
  79. }
  80. }