【发布时间】:2015-06-17 08:42:30
【问题描述】:
假设有 2 个角色 - 可以管理所有内容的管理员,以及已注册并需要管理员批准他们可以在每个控制器上执行的特定 CRUD 操作的用户。 就像 X 只能创建和阅读文章,而 Y 可以执行更新和删除操作。 我还如何让管理员从网站本身为用户分配这些特定角色?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 roles
假设有 2 个角色 - 可以管理所有内容的管理员,以及已注册并需要管理员批准他们可以在每个控制器上执行的特定 CRUD 操作的用户。 就像 X 只能创建和阅读文章,而 Y 可以执行更新和删除操作。 我还如何让管理员从网站本身为用户分配这些特定角色?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 roles
这个宝石https://github.com/stffn/declarative_authorization/怎么样?
你可以让它非常颗粒。定义角色,然后您可以相应地分配您的 CURD。它还有一个非常好的 Helper 方法,您可以使用它来确定哪个角色在 View 或 Controller 上执行什么任务。
假设基于
这是一个快速的图表(我假设你会照顾模型)
这是我为 DRY 一些常见任务而制作的模块。
注意:Declarative Authorization 有很多帮助器,以下只是为了补充它们,有些功能可能是多余的,可以重构和编写得更好。它只是一个夜间建造的 :)
module AuthenticationRelated
def what_are_current_user_roles
objArray = []
current_user.roles.each do |role|
objArray.push role.name
end
return objArray
end
def does_user_have_this_role?(role_name)
result = false
obj_array = what_are_current_user_roles
obj_array.each do |a_role|
if a_role == role_name
result = true
end
end
result
end
def is_admin?
athu = false
if signed_in?
current_user.roles.each do |role|
if role.name == 'admin'
athu = true
end
end
end
return athu
end
# class_instance MUST be a parent class
# If you need to Authenticate
def user_allowed_create_and_edit?(class_model, class_instance)
user_is_allowed = false
if permitted_to? :create, class_model.new and has_user_own_this(class_instance)
user_is_allowed = true
else
user_is_allowed = false
end
# Override everything if user is admin
if is_admin?
user_is_allowed = true
end
return user_is_allowed
end
# Authentication for Builder
# relation has to be set to access_list
def has_user_own_this(model)
user_who_owns_this = model.access_list.user_id
if current_user.id == user_who_owns_this
return true
else
return false
end
end
def find_current_user_acls
acl = []
acls = AccessList.joins(:user).where("users.id = ?",current_user.id)
acls.each do |an_acl|
acl.push an_acl.id
end
acl
end
end
【讨论】:
你应该检查 rolify gem:https://github.com/RolifyCommunity/rolify
但是如果您只需要 2 个角色(用户和管理员),您可以将新列添加到用户表 :admin, :boolean, default: false,然后您可以签入您的控制器,您可以编写 before_filter 来检查 if @current_user.admin,如果不是重定向或做其他事情。
在您的视图中,您可以隐藏用于更新和删除unless @current_user.admin的按钮
【讨论】: