Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

104 linhas
2.6KB

  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 BackendController
  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. 'actions' => ['change-etablissement'],
  38. 'allow' => true,
  39. 'roles' => ['@'],
  40. 'matchCallback' => function ($rule, $action) {
  41. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  42. }
  43. ],
  44. ],
  45. ],
  46. 'verbs' => [
  47. 'class' => VerbFilter::className(),
  48. 'actions' => [
  49. 'logout' => ['post'],
  50. ],
  51. ],
  52. ];
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function actions()
  58. {
  59. return [
  60. 'error' => [
  61. 'class' => 'yii\web\ErrorAction',
  62. ],
  63. ];
  64. }
  65. public function actionIndex()
  66. {
  67. return $this->render('index');
  68. }
  69. public function actionLogin()
  70. {
  71. if (!\Yii::$app->user->isGuest) {
  72. return $this->goHome();
  73. }
  74. $model = new LoginForm();
  75. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  76. return $this->goBack();
  77. } else {
  78. return $this->render('login', [
  79. 'model' => $model,
  80. ]);
  81. }
  82. }
  83. public function actionLogout()
  84. {
  85. Yii::$app->user->logout();
  86. return $this->goHome();
  87. }
  88. public function actionChangeEtablissement($id)
  89. {
  90. Yii::$app->user->identity->id_etablissement = $id ;
  91. Yii::$app->user->identity->save() ;
  92. $this->redirect(['commande/index']) ;
  93. }
  94. }