我正试图为我的应用程序实现一个基于角色的访问控制单元,但考虑到我的限制,我不知道如何实现它。
我用设计作为我的登录/向上操作。我只有一个模型用户,但是每个用户都有不同的user_type。我有八种不同身份验证级别(整数)的唯一用户类型。
这些是限制用户如何与站点交互的因素:
user1和auth_level = 2可以访问控制器C1,但是具有相同auth_level = 2的user2可以访问控制器C2。换句话说,他们在同一层次上做着不同的工作。发布于 2017-03-31 12:04:57
自从几天前我提出这个问题以来,有一段时间,我一直在思考和评估可用的宝石,它们适合我的用途,但我找不到任何simple、easy-to-use和lightweight将其导入到我的应用程序中;所以我接受了这一斗争,并编写了我自己的创业板出入控制股,它满足了我在问题+问题中的限制。它非常轻巧,而且很容易使用,而且适用于任何目的。
在这个创业板中,每个请求要么通过,要么不传递,访问规则可以涵盖rails应用程序的任何方面。下面是规则集的一个示例,有关更多细节,您可以查看这里提供的文档。
# config/initializers/acu_rules.rb
Acu::Rules.define do
# anyone makes a request could be count as everyone!
whois :everyone { true }
whois :admin, args: [:user] { |c| c and c.user_type == :ADMIN.to_s }
whois :client, args: [:user] { |c| c and c.user_type == :CLIENT.to_s }
# any request that doesn't match with a rule will be denied by default;
# but this can be configured to not to!
# the default namespace
namespace do
controller :home, except: [:some_secret_action] do
allow :everyone
end
controller :home do
allow [:admin, :client], on: [:some_secret_action]
end
end
# the admin namespace
namespace :admin do
allow :admin
controller :contact, only: [:send_message] do
allow :everyone
end
controller :contact do
action :support {
allow :client
}
end
end
end发布于 2017-03-27 15:31:14
我建议您使用路由约束(http://guides.rubyonrails.org/routing.html#advanced-constraints)来实现控制器访问。
使用request.env["warden"].authenticate,可以对约束类中matches?方法中的请求的当前用户进行身份验证。如果使用request.env["warden"].authenticate!,甚至会将用户重定向到登录页面,或者显示适当的错误消息(取决于您的设置)。
https://stackoverflow.com/questions/43050008
复制相似问题