Auth.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Ycbl\AdminAuth;
  3. use Hyperf\Di\Annotation\Inject;
  4. use Qbhy\HyperfAuth\Authenticatable;
  5. use Qbhy\HyperfAuth\AuthManager;
  6. use Ycbl\AdminAuth\Service\AuthService;
  7. class Auth
  8. {
  9. /**
  10. * @Inject
  11. * @var AuthService
  12. */
  13. protected $auth;
  14. /**
  15. * @Inject
  16. * @var AuthManager
  17. */
  18. protected $authManager;
  19. /**
  20. * 退出登录方法(清除权限缓存)
  21. * @return mixed
  22. */
  23. public function logout()
  24. {
  25. $this->auth->cleanCache();
  26. return $this->getAuthManager()->logout();
  27. }
  28. /**
  29. * 登录方法
  30. * @param Authenticatable $user
  31. * @return mixed
  32. */
  33. public function login(Authenticatable $user)
  34. {
  35. return $this->getAuthManager()->login($user);
  36. }
  37. /**
  38. * 检查是否登录方法
  39. * @return bool
  40. */
  41. public function isLogin()
  42. {
  43. return $this->getAuthManager()->check();
  44. }
  45. /**
  46. * 获取当前用户信息
  47. * @return Authenticatable|null
  48. */
  49. public function user()
  50. {
  51. return $this->getAuthManager()->user();
  52. }
  53. /**
  54. * 获取认证类
  55. * @return AuthManager
  56. */
  57. public function getAuthManager()
  58. {
  59. return $this->authManager;
  60. }
  61. /**
  62. * 是否为超级管理员
  63. * @return bool
  64. */
  65. public function isSuperAdmin()
  66. {
  67. return $this->auth->isSuperAdmin();
  68. }
  69. /**
  70. * 检查权限
  71. * @param $name
  72. * @param string $uid
  73. * @param string $relation
  74. * @return bool
  75. */
  76. public function check($name, $uid = '', $relation = 'or')
  77. {
  78. return $this->auth->check($name, $uid, $relation);
  79. }
  80. }