before_action for standard/admin users : 如何赋予管理员所有其他用户的权限?
before_action for standard/admin users : how to give admin the rights of all other users?
我想为我的管理员用户提供比普通用户更多的权限,这是我的 before_action :
# users_controller.rb
before_action :correct_user, only: [:edit, :update, :show]
before_action :admin_user, only: [:destroy, :index, :admin_toggle]
因此,如果您是正确的用户,您可以:编辑、更新、显示您自己的个人资料。
如果你是 admin_user,你可以看到列表(索引),销毁你自己的个人资料,切换管理员任何用户,edit/update/show你自己的个人资料(admin_user还有一个 correct_user).
我想要一个 admin_user 能够编辑、更新、显示其他成员的个人资料:我需要编写特定的方法还是 before_action 有技巧吗?
似乎 admin_user 应该拥有 correct_user 的权利-给定任何用户 ID-。
如果您使用某种特定的身份验证工具,例如 Devise for ex。然后我建议实施授权解决方案
cancancan gem。您的模型目录下有一个特定的能力文件,您可以在其中声明不同用户角色的访问权限。
使您以后的代码更加简洁易读。
###编辑:
正如前面的答案所指出的那样,Rails4 中也有一个 CanCan gem but as much as I know then it is not supported。在写这个答案时,github 中的 CanCanCan build-status 被标记为 failing
但我已经在我的项目中使用它很长时间了,我很高兴 :)
###
ability.rb
示例:
def secretary_abilities
can [:create, :read, :update], Person
end
def superadmin_abilities
# superadmin can do everything that secretary can
secretary_abilities
# ...and can also do destructive actions
can [:destroy], Person
end
之后,您可以像这样在视图中添加检查:
<% if can? :show, Person %>
<%= link_to 'Show', person_path(@person) %>
<% end %>
我想为我的管理员用户提供比普通用户更多的权限,这是我的 before_action :
# users_controller.rb
before_action :correct_user, only: [:edit, :update, :show]
before_action :admin_user, only: [:destroy, :index, :admin_toggle]
因此,如果您是正确的用户,您可以:编辑、更新、显示您自己的个人资料。
如果你是 admin_user,你可以看到列表(索引),销毁你自己的个人资料,切换管理员任何用户,edit/update/show你自己的个人资料(admin_user还有一个 correct_user).
我想要一个 admin_user 能够编辑、更新、显示其他成员的个人资料:我需要编写特定的方法还是 before_action 有技巧吗?
似乎 admin_user 应该拥有 correct_user 的权利-给定任何用户 ID-。
如果您使用某种特定的身份验证工具,例如 Devise for ex。然后我建议实施授权解决方案 cancancan gem。您的模型目录下有一个特定的能力文件,您可以在其中声明不同用户角色的访问权限。
使您以后的代码更加简洁易读。
###编辑:
正如前面的答案所指出的那样,Rails4 中也有一个 CanCan gem but as much as I know then it is not supported。在写这个答案时,github 中的 CanCanCan build-status 被标记为 failing
但我已经在我的项目中使用它很长时间了,我很高兴 :)
###
ability.rb
示例:
def secretary_abilities
can [:create, :read, :update], Person
end
def superadmin_abilities
# superadmin can do everything that secretary can
secretary_abilities
# ...and can also do destructive actions
can [:destroy], Person
end
之后,您可以像这样在视图中添加检查:
<% if can? :show, Person %>
<%= link_to 'Show', person_path(@person) %>
<% end %>