Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
修复权限缓存时缓存名重复导致权限覆盖的bug
  • Loading branch information
gmars committed Aug 16, 2018
1 parent 22c577c commit 1147a8b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
20 changes: 18 additions & 2 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Rbac
*/
private $userTable = "user";

/**
* @var string 权限缓存前缀
*/
private $_permissionCachePrefix = "_RBAC_PERMISSION_CACHE_";

public function __construct()
{
Expand All @@ -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'];
}

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

Expand All @@ -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('你还没有登录或在登录后没有获取权限缓存');
}
Expand Down

0 comments on commit 1147a8b

Please sign in to comment.