diff --git a/gmars_rbac.sql b/gmars_rbac.sql index 67858d5..f4647ac 100644 --- a/gmars_rbac.sql +++ b/gmars_rbac.sql @@ -26,13 +26,9 @@ DROP TABLE IF EXISTS `###role`; CREATE TABLE `###role` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '' COMMENT '角色名', - `parent_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '父级id(一般不建议角色的继承)', `description` varchar(200) NOT NULL DEFAULT '' COMMENT '角色描述', `status` smallint(4) unsigned NOT NULL DEFAULT '0' COMMENT '状态1正常0未启用', `sort_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '排序值', - `left_key` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '左值', - `right_key` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '右值', - `level` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '层级', PRIMARY KEY (`id`), KEY `idx_role` (`status`,`left_key`,`right_key`,`level`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色'; diff --git a/src/Rbac.php b/src/Rbac.php index 6b52c9f..a32173d 100755 --- a/src/Rbac.php +++ b/src/Rbac.php @@ -66,12 +66,12 @@ public function __construct() /** * 生成所需的数据表 - * @param string $prefix + * @param string $db */ - public function createTable($prefix = '') + public function createTable($db = '') { $createTable = new CreateTable(); - $createTable->create($prefix); + $createTable->create($db); } /** @@ -164,12 +164,7 @@ public function delPermissionBatch($condition) public function getPermission($condition) { $model = new Permission($this->db); - if (is_numeric($condition)) { - return $model->where('id', $condition)->find(); - } - - return $model->where($condition)->select(); - + return $model->getPermission($condition); } /** @@ -207,6 +202,20 @@ public function delPermissionCategory($id = 0) } } + /** + * 获取权限分组 + * @param $where + * @return array|\PDOStatement|string|\think\Collection|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getPermissionCategory($where) + { + $model = new PermissionCategory($this->db); + return $model->getCategory($where); + } + /** * 编辑角色 * @param array $data @@ -230,18 +239,16 @@ public function createRole(array $data = [], $permissionIds = '') /** * 根据id或标准条件获取角色 * @param $condition + * @param bool $withPermissionId * @return array|\PDOStatement|string|\think\Collection|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ - public function getRole($condition) + public function getRole($condition, $withPermissionId = true) { $model = new Role($this->db); - if (is_numeric($condition)) { - return $model->where('id', $condition)->find(); - } - return $model->where($condition)->select(); + return $model->getRole($condition, $withPermissionId); } /** @@ -336,7 +343,6 @@ public function cachePermission($id, $timeOut = 3600) if (empty($id)) { throw new Exception('参数错误'); } - $model = new Permission($this->db); $permission = $model->userPermission($id, $timeOut); return $permission; diff --git a/src/model/Permission.php b/src/model/Permission.php index 936bad4..00b63a9 100644 --- a/src/model/Permission.php +++ b/src/model/Permission.php @@ -10,6 +10,7 @@ +use think\Db; use think\Exception; use think\facade\Cache; use think\facade\Session; @@ -109,7 +110,7 @@ public function userPermission($userId, $timeOut = 3600) } Cache::set($this->_permissionCachePrefix . $userId, $newPermission, $timeOut); Session::set('gmars_rbac_permission_name', $this->_permissionCachePrefix . $userId); - return $permission; + return $newPermission; } /** @@ -122,10 +123,29 @@ public function userPermission($userId, $timeOut = 3600) */ public function getPermissionByUserId($userId) { - $permission = $this->alias('p') - ->join(['role_permission' => 'rp'], 'p.id = rp.permission_id') - ->join(['user_role' => 'ur'], 'rp.role_id = ur.role_id') + $prefix = $this->getConfig('prefix'); + $permission = Db::name('permission')->setConnection($this->getConnection())->alias('p') + ->join(["{$prefix}role_permission" => 'rp'], 'p.id = rp.permission_id') + ->join(["{$prefix}user_role" => 'ur'], 'rp.role_id = ur.role_id') ->where('ur.user_id', $userId)->select(); return $permission; } + + /** + * 获取权限节点 + * @param $condition + * @return array|\PDOStatement|string|\think\Collection|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getPermission($condition) + { + $model = Db::name('permission')->setConnection($this->getConnection()); + if (is_numeric($condition)) { + return $model->where('id', $condition)->find(); + } else { + return $model->where($condition)->select(); + } + } } \ No newline at end of file diff --git a/src/model/PermissionCategory.php b/src/model/PermissionCategory.php index b3aa6ab..92c8b88 100644 --- a/src/model/PermissionCategory.php +++ b/src/model/PermissionCategory.php @@ -9,6 +9,7 @@ namespace gmars\rbac\model; +use think\Db; use think\Exception; class PermissionCategory extends Base @@ -63,4 +64,22 @@ public function delCategory($id) return true; } + /** + * 获取权限分组 + * @param $where + * @return array|\PDOStatement|string|\think\Collection|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getCategory($where) + { + $model = Db::name('permission_category')->setConnection($this->getConnection()); + if (is_numeric($where)) { + return $model->where('id', $where)->find(); + } else { + return $model->where($where)->select(); + } + } + } \ No newline at end of file diff --git a/src/model/Role.php b/src/model/Role.php index 74988d8..b98b3ad 100644 --- a/src/model/Role.php +++ b/src/model/Role.php @@ -9,6 +9,7 @@ namespace gmars\rbac\model; +use think\Db; use think\Exception; class Role extends Base @@ -103,4 +104,51 @@ public function delRole($condition) return true; } + /** + * 获取角色列表 + * @param $condition + * @param bool $withPermissionId + * @return array|\PDOStatement|string|\think\Collection|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getRole($condition, $withPermissionId = false) + { + $model = Db::name('role')->setConnection($this->getConnection()); + $where = []; + if (is_array($condition)) { + $where = $condition; + } else { + $condition = (int)$condition; + if (is_numeric($condition) && $condition > 0) { + $role = $model->where('id', $condition)->find(); + if (!empty($role) && $withPermissionId) { + $role['permission_ids'] = Db::name('role_permission')->setConnection($this->getConnection()) + ->where('role_id', $condition)->column('permission_id'); + } + return $role; + } + } + $role = Db::name('role')->setConnection($this->getConnection()) + ->where($where)->select(); + if (!empty($role) && $withPermissionId) { + $permission = Db::name('role_permission')->setConnection($this->getConnection()) + ->where('role_id', 'IN', array_column($role, 'id'))->select(); + $roleIdIndexer = []; + if (!empty($permission)) { + foreach ($permission as $v) + { + $roleIdIndexer[$v['role_id']][] = $v['permission_id']; + } + } + foreach ($role as &$v) + { + $v['permission_ids'] = isset($roleIdIndexer[$v['id']])? $roleIdIndexer[$v['id']] : []; + unset($v); + } + } + return $role; + } + } \ No newline at end of file