Access.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\model;
  10. use think\Model;
  11. use think\facade\Request;
  12. /**
  13. * 统一授权模型
  14. * @package app\admin\model
  15. */
  16. class Access extends Model
  17. {
  18. // 设置当前模型对应的完整数据表名称
  19. protected $name = 'admin_access';
  20. /**
  21. * 获取用户授权节点
  22. * @param int $uid 用户id
  23. * @param string $group 权限分组,可以以点分开模型名称和分组名称,如user.group
  24. * @author 蔡伟明 <314013107@qq.com>
  25. * @return array|bool
  26. */
  27. public function getAuthNode($uid = 0, $group = '')
  28. {
  29. if ($uid == 0 || $group == '') {
  30. $this->error = '缺少参数';
  31. return false;
  32. }
  33. if (strpos($group, '.')) {
  34. list($module, $group) = explode('.', $group);
  35. } else {
  36. $module = Request::module();
  37. }
  38. $map = [
  39. 'module' => $module,
  40. 'group' => $group,
  41. 'uid' => $uid
  42. ];
  43. return $this->where($map)->column('nid');
  44. }
  45. /**
  46. * 检查用户的某个节点是否授权
  47. * @param int $uid 用户id
  48. * @param string $group $group 权限分组,可以以点分开模型名称和分组名称,如user.group
  49. * @param int $node 需要检查的节点id
  50. * @author 蔡伟明 <314013107@qq.com>
  51. * @return bool
  52. */
  53. public function checkAuthNode($uid = 0, $group = '', $node = 0)
  54. {
  55. if ($uid == 0 || $group == '' || $node == 0) {
  56. $this->error = '缺少参数';
  57. return false;
  58. }
  59. // 获取该用户的所有授权节点
  60. $nodes = $this->getAuthNode($uid, $group);
  61. if (!$nodes) {
  62. $this->error = '该用户没有授权任何节点';
  63. return false;
  64. }
  65. $nodes = array_flip($nodes);
  66. if (isset($nodes[$node])) {
  67. return true;
  68. } else {
  69. $this->error = '未授权';
  70. return false;
  71. }
  72. }
  73. }