Module.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\modules\api;
  3. use app\models\user\User;
  4. use yii\filters\auth\CompositeAuth;
  5. use yii\filters\auth\HttpBasicAuth;
  6. use yii\filters\auth\HttpBearerAuth;
  7. use yii\filters\auth\QueryParamAuth;
  8. /**
  9. * api module definition class
  10. */
  11. class Module extends \yii\base\Module
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public $controllerNamespace = 'app\modules\api\controllers';
  17. public function behaviors()
  18. {
  19. $behaviors = parent::behaviors();
  20. /*$behaviors['authenticator'] = [
  21. 'class' => CompositeAuth::class,
  22. 'authMethods' => [
  23. [
  24. 'class' =>HttpBasicAuth::class,
  25. 'auth' => function ($username, $password) {
  26. $user = User::find()->where(['username' => $username])->one();
  27. if ($user->verifyPassword($password)) {
  28. return $user;
  29. }
  30. return null;
  31. },
  32. ],
  33. HttpBearerAuth::class,
  34. QueryParamAuth::class,
  35. ],
  36. ];*/
  37. $behaviors['authenticator'] = [
  38. 'class' => HttpBearerAuth::class,
  39. ];
  40. return $behaviors;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function init()
  46. {
  47. parent::init();
  48. \Yii::$app->user->enableSession = false;
  49. // custom initialization code goes here
  50. }
  51. }