Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README.md中,举例疑有误 #6

Closed
MrXBear opened this issue Oct 29, 2019 · 4 comments
Closed

README.md中,举例疑有误 #6

MrXBear opened this issue Oct 29, 2019 · 4 comments
Labels
question Further information is requested

Comments

@MrXBear
Copy link

MrXBear commented Oct 29, 2019

项目的 readme.md 文件中

赋予权限给某个用户或角色:

// to user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// to role
Enforcer::addPermissionForUser('writer', 'articles','edit');

删除某个用户或角色的所有权限:

// to user
Enforcer::deletePermissionsForUser('eve');
// to role
Enforcer::deletePermissionsForUser('writer');

两处 均各举 to user 与 to role 的例子
但 注释为 to role 的例子,均为 xxxForUser( )

@leeqvip
Copy link
Member

leeqvip commented Oct 29, 2019

没有问题的,Casbin中用户和角色都按 subject 算,src/RbacApi.php#L137

@leeqvip leeqvip added the question Further information is requested label Oct 29, 2019
@MrXBear
Copy link
Author

MrXBear commented Oct 30, 2019

之所以有这个问题,是因为昨天 试了以下代码,与预期结果不符

// 对eve用户添加 articles read 权限
Enforcer::addPermissionForUser('eve', 'articles', 'read');

// 对 writer 角色添加 articles edit 权限
Enforcer::addPermissionForUser('writer', 'articles','edit');

// 对eve用户添加 writer 角色
Enforcer::addRoleForUser('eve', 'writer');

// 至此 eve 应有 articles read 权限,以及 writer 所包含的 articles edit 权限
Enforcer::hasPermissionForUser('eve', 'articles', 'edit');    // 结果为 false

仔细看完readme.md后,发现实际上有两个方法:

// 决定某个用户是否拥有某个权限
Enforcer::hasPermissionForUser('eve', 'articles', 'edit');    // 结果为 false

// 检查一个用户是否拥有某个权限
Enforcer::enforce('eve', 'articles', 'edit');     // 结果为 true

按结果 来猜测:
hasPermissionForUser( ) 只检查“直接”赋予给 用户的权限;
enforce( ) 检查 赋予用户的权限 以及 用户所拥有的角色的所包含的权限。

仅从现有的中文注释来看,比较难区分二者的区别。

另外,想知道 这里的 第2参数 articles 与 第3参数 read 是 指代 一级权限 与 二级权限 吗?
谢谢

@leeqvip
Copy link
Member

leeqvip commented Oct 30, 2019

@MrXBear hasPermissionForUser() 的确是检查直接权限,enforce() 则是根据 your-model.conf配置里的 matchers 表达式进行检查,可以使用 getImplicitPermissionsForUser() 方法 获取 所有权限(包括继承来的)

参数的顺序则是根据 your-model.conf 里的request_definition 里的配置一一对应,比如 eve articles edit 分别对应 sub obj act,可以配置更多的参数。

@MrXBear
Copy link
Author

MrXBear commented Oct 31, 2019

明白了
谢谢🙏

@leeqvip leeqvip closed this as completed Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants