diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/src/Rbac.php b/src/Rbac.php index 3bd00f8..c592b6e 100755 --- a/src/Rbac.php +++ b/src/Rbac.php @@ -42,6 +42,10 @@ class Rbac */ private $userTable = "user"; + /** + * @var string 权限缓存前缀 + */ + private $_permissionCachePrefix = "_RBAC_PERMISSION_CACHE_"; public function __construct() { @@ -52,6 +56,7 @@ public function __construct() isset($rbacConfig['user_role']) && $this->userRoleTable = $rbacConfig['user_role']; isset($rbacConfig['role_permission']) && $this->rolePermissionTable = $rbacConfig['role_permission']; isset($rbacConfig['user']) && $this->userTable = $rbacConfig['user']; + isset($rbacConfig['permission_cache_prefix']) && $this->_permissionCachePrefix = $rbacConfig['permission_cache_prefix']; } } @@ -400,7 +405,12 @@ public function cachePermission($id) } } - cache("permission", $newPermission); + //生成唯一缓存名称存入session + $cacheName = $this->_permissionCachePrefix . $id . '_'; + session("permission_name", $cacheName); + + //把权限列表写入缓存 + cache($cacheName, $newPermission); return true; } @@ -412,7 +422,13 @@ public function cachePermission($id) */ public function can($path) { - $permissionList = cache("permission"); + //获取session中的缓存名 + $cacheName = session("permission_name"); + if (empty($cacheName)) { + throw new Exception('获取权限列表时出错'); + } + + $permissionList = cache($cacheName); if (empty($permissionList)) { throw new Exception('你还没有登录或在登录后没有获取权限缓存'); }