您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

89 行
2.1KB

  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. }