UserRepository.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace oauth2\repository;
  3. use common\models\member\Member;
  4. use oauth2\entity\UserEntity;
  5. use League\OAuth2\Server\Repositories\UserRepositoryInterface;
  6. use League\OAuth2\Server\Entities\ClientEntityInterface;
  7. use League\OAuth2\Server\Entities\UserEntityInterface;
  8. use yii\web\UnprocessableEntityHttpException;
  9. /**
  10. * Class UserRepository
  11. * @package oauth2
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class UserRepository implements UserRepositoryInterface
  15. {
  16. /**
  17. * @param string $username
  18. * @param string $password
  19. * @param string $grantType 使用授权类型
  20. * @param ClientEntityInterface $clientEntity
  21. * @return UserEntity|UserEntityInterface
  22. * @throws UnprocessableEntityHttpException
  23. */
  24. public function getUserEntityByUserCredentials(
  25. $username,
  26. $password,
  27. $grantType,
  28. ClientEntityInterface $clientEntity
  29. ) {
  30. /* @var $member \common\models\base\User */
  31. if (!($member = Member::findByUsername($username))) {
  32. throw new UnprocessableEntityHttpException('找不到用户信息');
  33. }
  34. if (!$member->validatePassword($password)) {
  35. throw new UnprocessableEntityHttpException('密码错误');
  36. }
  37. // 可以验证是否为用户可使用的授权类型($grantType)与客户端($clientEntity)
  38. $user = new UserEntity();
  39. $user->setIdentifier($member->id);
  40. return $user;
  41. }
  42. }
粤ICP备19079148号