AccessTokenService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace services\oauth2;
  3. use common\models\oauth2\AccessToken;
  4. use common\components\Service;
  5. use yii\web\UnprocessableEntityHttpException;
  6. /**
  7. * Class AccessTokenService
  8. * @package services\oauth2
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class AccessTokenService extends Service
  12. {
  13. /**
  14. * @param $client_id
  15. * @param $grant_type
  16. * @param $access_token
  17. * @param $expires
  18. * @param $member_id
  19. * @param $scopes
  20. */
  21. public function create($client_id, $grant_type, $access_token, $expires, $member_id, $scopes)
  22. {
  23. if (!($model = $this->findByClientId($client_id, $grant_type, $member_id))) {
  24. $model = new AccessToken();
  25. $model->client_id = $client_id;
  26. $model->grant_type = $grant_type;
  27. $model->member_id = (string)$member_id;
  28. }
  29. $model->expires = $expires;
  30. $model->access_token = $access_token;
  31. $model->scope = $scopes;
  32. if (!$model->save()) {
  33. throw new UnprocessableEntityHttpException($this->getError($model));
  34. }
  35. }
  36. /**
  37. * @param $tokenId
  38. */
  39. public function deleteByAccessToken($tokenId)
  40. {
  41. AccessToken::deleteAll(['access_token' => $tokenId]);
  42. }
  43. /**
  44. * @param $tokenId
  45. * @param string $client_id
  46. * @return array|\yii\db\ActiveRecord|null
  47. */
  48. public function findByAccessToken($tokenId, $client_id = '')
  49. {
  50. return AccessToken::find()
  51. ->where(['access_token' => $tokenId])
  52. ->andFilterWhere(['client_id' => $client_id])
  53. ->one();
  54. }
  55. /**
  56. * @param $client_id
  57. * @param $grant_type
  58. * @return array|\yii\db\ActiveRecord|null
  59. */
  60. public function findByClientId($client_id, $grant_type, $member_id)
  61. {
  62. return AccessToken::find()
  63. ->where([
  64. 'client_id' => $client_id,
  65. 'grant_type' => $grant_type,
  66. 'member_id' => $member_id,
  67. ])
  68. ->one();
  69. }
  70. }
粤ICP备19079148号