Skip to content

Commit

Permalink
rbac管理功能完善加入jwt方式验证
Browse files Browse the repository at this point in the history
rbac管理功能完善加入jwt方式验证
  • Loading branch information
gmars committed Apr 18, 2019
1 parent 6a8ffd6 commit 5190c14
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 23 deletions.
4 changes: 0 additions & 4 deletions gmars_rbac.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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='角色';
Expand Down
36 changes: 21 additions & 15 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 24 additions & 4 deletions src/model/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@



use think\Db;
use think\Exception;
use think\facade\Cache;
use think\facade\Session;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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();
}
}
}
19 changes: 19 additions & 0 deletions src/model/PermissionCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace gmars\rbac\model;


use think\Db;
use think\Exception;

class PermissionCategory extends Base
Expand Down Expand Up @@ -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();
}
}

}
48 changes: 48 additions & 0 deletions src/model/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace gmars\rbac\model;


use think\Db;
use think\Exception;

class Role extends Base
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 5190c14

Please sign in to comment.