Ruby on rails rails-devise-roles 示例应用程序



我发现这个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/

您链接的存储库上次更新是在 4 年前。我试图拉下存储库并在本地测试您提出的这些要点,但在尝试这样做时遇到了太多问题。在其他地方寻找这方面的指导。

编辑:我确实浏览了代码,但我不完全确定这个应用程序如何按照自述文件所说的去做。

最新更新