AuthLogic.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\tenantapi\logic\auth;
  15. use app\common\model\auth\TenantAdminRole;
  16. use app\common\model\auth\TenantSystemMenu;
  17. use app\common\model\auth\TenantSystemRoleMenu;
  18. /**
  19. * 权限功能类
  20. * Class AuthLogic
  21. * @package app\tenantapi\logic\auth
  22. */
  23. class AuthLogic
  24. {
  25. /**
  26. * @notes 获取全部权限
  27. * @return mixed
  28. * @author 段誉
  29. * @date 2022/7/1 11:55
  30. */
  31. public static function getAllAuth()
  32. {
  33. return TenantSystemMenu::distinct(true)
  34. ->where([
  35. ['is_disable', '=', 0],
  36. ['perms', '<>', '']
  37. ])
  38. ->column('perms');
  39. }
  40. /**
  41. * @notes 获取当前管理员角色按钮权限
  42. * @param $roleId
  43. * @return mixed
  44. * @author 段誉
  45. * @date 2022/7/1 16:10
  46. */
  47. public static function getBtnAuthByRoleId($admin)
  48. {
  49. if ($admin['root']) {
  50. return ['*'];
  51. }
  52. $menuId = TenantSystemRoleMenu::whereIn('role_id', $admin['role_id'])
  53. ->column('menu_id');
  54. $where[] = ['is_disable', '=', 0];
  55. $where[] = ['perms', '<>', ''];
  56. $roleAuth = TenantSystemMenu::distinct(true)
  57. ->where('id', 'in', $menuId)
  58. ->where($where)
  59. ->column('perms');
  60. $allAuth = TenantSystemMenu::distinct(true)
  61. ->where($where)
  62. ->column('perms');
  63. $hasAllAuth = array_diff($allAuth, $roleAuth);
  64. if (empty($hasAllAuth)) {
  65. return ['*'];
  66. }
  67. return $roleAuth;
  68. }
  69. /**
  70. * @notes 获取管理员角色关联的菜单id(菜单,权限)
  71. * @param int $adminId
  72. * @return array
  73. * @author 段誉
  74. * @date 2022/7/1 15:56
  75. */
  76. public static function getAuthByAdminId(int $adminId): array
  77. {
  78. $roleIds = TenantAdminRole::where('admin_id', $adminId)->column('role_id');
  79. $menuId = TenantSystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  80. return TenantSystemMenu::distinct(true)
  81. ->where([
  82. ['is_disable', '=', 0],
  83. ['perms', '<>', ''],
  84. ['id', 'in', array_unique($menuId)],
  85. ])
  86. ->column('perms');
  87. }
  88. }