访问级别/安全角色设计选择:单独角色或继承
请考虑以下场景:一个具有 4 个访问级别的 Web 应用程序:admin >经理>代表>客户>无角色(公共访问页面)
通过我当前的应用程序设置,我可以通过两种方式允许访问:
我可以编写代码,该代码将假定角色优先级,即如果用户是经理 - 应用程序将自动假定他/她有权进入客户和客户所在的区域。代表可以,但管理员不行。
我可以在表中单独分配每个角色。例如,一个用户将被分配 3 个角色。因此应用程序不会承担角色优先/继承。我可以让管理员为用户分配角色,也可以修改一些代码,如果授予更高的访问级别,这些代码将自动为用户分配额外的角色。
从可维护性的角度来看,这两种方法哪种更好?
PS
我认为这并不重要,但我正在使用 Rails 3 和 CanCan & 。设计。 另外,我对角色和用户之间关系的设置如下:
Role <=> (HABTM)<=>用户
Please consider this scenario: a web app that has 4 levels of access: admin > manager > representative > customer > no role (public access pages)
With my current app setup I can allow access in 2 ways:
I can write code, that will assume role precedence, i.e. if a user is a a manager - app will automatically assume that he/she has the right to access areas that customer & representative can, but not the admin.
I can assign each role individually in a table. For instance a user will have 3 roles assigned to them. So the app will not assume role precedence / inheritance. I can either let the admin assign users with roles, or right some code that will automatically assign extra roles to a user if a higher access level is granted.
Which of this two approaches is better from the standpoint of maintainability?
P.S.
I don't think this matters but I'm using Rails 3 with CanCan & Devise.
Also my setup for the relationship between roles and users is the following:
Role <=> (HABTM) <=> User
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的角色要求,因此选择了方法 1。很自然地,您的角色层次结构越高,您拥有的访问权限就越多。因此,说经理可以访问代表所拥有的资源是可以的。
另外,由于您使用的是 CanCan,因此很容易设置失败。从初始化块顶部访问权限最少的角色开始,然后依次向下。
I have a similar role requirement and I've chosen approach 1. It's natural to assume that the higher up the role hierarchy you go, the more access you have. So, saying a manager has access to the resources a representative has is OK.
Also, since you're using CanCan, the fall through is very easily set up. Start with the role with the least access at the top of the initialize block and work your way down.