AbstractAuthGuard.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of qbhy/hyperf-auth.
  5. *
  6. * @link https://github.com/qbhy/hyperf-auth
  7. * @document https://github.com/qbhy/hyperf-auth/blob/master/README.md
  8. * @contact qbhy0715@qq.com
  9. * @license https://github.com/qbhy/hyperf-auth/blob/master/LICENSE
  10. */
  11. namespace Qbhy\HyperfAuth\Guard;
  12. use Qbhy\HyperfAuth\Authenticatable;
  13. use Qbhy\HyperfAuth\AuthGuard;
  14. use Qbhy\HyperfAuth\UserProvider;
  15. abstract class AbstractAuthGuard implements AuthGuard
  16. {
  17. protected array $config;
  18. protected string $name;
  19. protected UserProvider $userProvider;
  20. /**
  21. * AbstractAuthGuard constructor.
  22. */
  23. public function __construct(array $config, string $name, UserProvider $userProvider)
  24. {
  25. $this->config = $config;
  26. $this->name = $name;
  27. $this->userProvider = $userProvider;
  28. }
  29. public function getName(): string
  30. {
  31. return $this->name;
  32. }
  33. public function check(): bool
  34. {
  35. return $this->user() instanceof Authenticatable;
  36. }
  37. public function guest(): bool
  38. {
  39. return ! $this->check();
  40. }
  41. public function getProvider(): UserProvider
  42. {
  43. return $this->userProvider;
  44. }
  45. public function id()
  46. {
  47. return $this->user()->getId();
  48. }
  49. }