MemberController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace oauth2\controllers;
  3. use Yii;
  4. use yii\filters\Cors;
  5. use oauth2\behaviors\JWTAuth;
  6. use common\traits\BaseAction;
  7. /**
  8. * Class MemberController
  9. * @package oauth2\controllers
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class MemberController extends \yii\rest\ActiveController
  13. {
  14. use BaseAction;
  15. /**
  16. * @var string
  17. */
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. * 例如: ['index', 'update', 'create', 'view', 'delete']
  22. * 默认全部需要验证
  23. *
  24. * @var array
  25. */
  26. protected $authOptional = [];
  27. /**
  28. * 启始位移
  29. *
  30. * @var int
  31. */
  32. protected $offset = 0;
  33. /**
  34. * 实际每页数量
  35. *
  36. * @var
  37. */
  38. protected $limit;
  39. /**
  40. * 行为验证
  41. *
  42. * @return array
  43. */
  44. public function behaviors()
  45. {
  46. $behaviors = parent::behaviors();
  47. // 跨域支持
  48. $behaviors['corsFilter'] = [
  49. 'class' => Cors::class,
  50. ];
  51. // 授权验证
  52. $behaviors['jwtAuth'] = [
  53. 'class' => JWTAuth::class,
  54. 'optional' => $this->authOptional, // 不进行认证判断方法
  55. ];
  56. return $behaviors;
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function actions()
  62. {
  63. $actions = parent::actions();
  64. // 注销系统自带的实现方法
  65. unset($actions['index']);
  66. return $actions;
  67. }
  68. /**
  69. * @return array|\common\models\member\Member|null|\yii\db\ActiveRecord
  70. */
  71. public function actionIndex()
  72. {
  73. $member_id = Yii::$app->user->identity->member_id;
  74. $member = Yii::$app->services->member->get($member_id);
  75. // TODO 校验返回那些用户信息
  76. return $member;
  77. }
  78. /**
  79. * 权限验证
  80. *
  81. * @param string $action 当前的方法
  82. * @param null $model 当前的模型类
  83. * @param array $params $_GET变量
  84. * @throws \yii\web\BadRequestHttpException
  85. */
  86. public function checkAccess($action, $model = null, $params = [])
  87. {
  88. // 方法名称
  89. if (in_array($action, ['view', 'update', 'create', 'delete'])) {
  90. throw new \yii\web\BadRequestHttpException('您的权限不足,如需要请联系管理员');
  91. }
  92. }
  93. }
粤ICP备19079148号