Module.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = parent::behaviors();
  38. $behaviors['authenticator'] = [
  39. 'class' => CompositeAuth::class,
  40. 'authMethods' => [
  41. HttpBasicAuth::class,
  42. HttpBearerAuth::class,
  43. QueryParamAuth::class,
  44. ],
  45. ];
  46. /*$behaviors['authenticator'] = [
  47. 'class' => HttpBearerAuth::class,
  48. ];*/
  49. return $behaviors;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function init()
  55. {
  56. parent::init();
  57. \Yii::$app->user->enableSession = false;
  58. // custom initialization code goes here
  59. }
  60. }