AuthLogic.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\adminapi\logic\auth;
  15. use app\common\model\auth\Admin;
  16. use app\common\model\auth\AdminRole;
  17. use app\common\model\auth\SystemMenu;
  18. use app\common\model\auth\SystemRoleMenu;
  19. /**
  20. * 权限功能类
  21. * Class AuthLogic
  22. * @package app\adminapi\logic\auth
  23. */
  24. class AuthLogic
  25. {
  26. /**
  27. * @notes 获取全部权限
  28. * @return mixed
  29. * @author 段誉
  30. * @date 2022/7/1 11:55
  31. */
  32. public static function getAllAuth()
  33. {
  34. return SystemMenu::distinct(true)
  35. ->where([
  36. ['is_disable', '=', 0],
  37. ['perms', '<>', '']
  38. ])
  39. ->column('perms');
  40. }
  41. /**
  42. * @notes 获取当前管理员角色按钮权限
  43. * @param $roleId
  44. * @return mixed
  45. * @author 段誉
  46. * @date 2022/7/1 16:10
  47. */
  48. public static function getBtnAuthByRoleId($admin)
  49. {
  50. if ($admin['root']) {
  51. return ['*'];
  52. }
  53. $menuId = SystemRoleMenu::whereIn('role_id', $admin['role_id'])
  54. ->column('menu_id');
  55. $where[] = ['is_disable', '=', 0];
  56. $where[] = ['perms', '<>', ''];
  57. $roleAuth = SystemMenu::distinct(true)
  58. ->where('id', 'in', $menuId)
  59. ->where($where)
  60. ->column('perms');
  61. $allAuth = SystemMenu::distinct(true)
  62. ->where($where)
  63. ->column('perms');
  64. $hasAllAuth = array_diff($allAuth, $roleAuth);
  65. if (empty($hasAllAuth)) {
  66. return ['*'];
  67. }
  68. return $roleAuth;
  69. }
  70. /**
  71. * @notes 获取管理员角色关联的菜单id(菜单,权限)
  72. * @param int $adminId
  73. * @return array
  74. * @author 段誉
  75. * @date 2022/7/1 15:56
  76. */
  77. public static function getAuthByAdminId(int $adminId): array
  78. {
  79. $roleIds = AdminRole::where('admin_id', $adminId)->column('role_id');
  80. $menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  81. return SystemMenu::distinct(true)
  82. ->where([
  83. ['is_disable', '=', 0],
  84. ['perms', '<>', ''],
  85. ['id', 'in', array_unique($menuId)],
  86. ])
  87. ->column('perms');
  88. }
  89. }