我发现了这个示例 Ruby on rails 应用程序展示devise gem
,它与角色一起使用。
在自述文件中,他们提到:
普通用户不能改变他们的角色
普通用户可以查看(和编辑)他们自己的用户资料
但是,查看用户控制器
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :admin_only, :except => :show
def show
@user = User.find(params[:id])
unless current_user.admin?
unless @user == current_user
redirect_to root_path, :alert => "Access denied."
end
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def admin_only
unless current_user.admin?
redirect_to root_path, :alert => "Access denied."
end
end
def secure_params
params.require(:user).permit(:role)
end
end
我们可以看到,只有管理员用户才允许所有操作,除了show
正在测试当前登录用户是否是@user
我们正在尝试获取/显示的用户。这对于自述文件的这一部分“普通用户可以看到他们自己的用户配置文件”是有意义的。
我不明白的是,自述文件说用户也可以编辑他们自己的个人资料,但该update
操作只允许由管理员用户执行(然后事件,管理员只能更改用户的角色?permit(:role)
)。
我建议遵循有关此类事情的最新指南:https : //altalogy.com/blog/rails-6-user-accounts-with-3-types-of-roles/
您链接的 repo 上次更新是 4 年前。我试图拉下 repo 并在本地测试您提出的这些要点,但我在尝试这样做时遇到了太多问题。在别处寻找这方面的指导。
编辑:虽然我确实查看了代码,但我不完全确定此应用程序如何执行自述文件所说的操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句