Ruby rails rails-设计角色示例应用程序

Ruby on rails rails-devise-roles example app

我发现 this example Ruby on rails app 展示 devise gem 并且它与角色一起使用。

在自述文件中他们提到:

an ordinary user can’t change their role

an ordinary user can see (and edit) their own user profile

但是,查看 users controller

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,如果他是我们正在尝试 [=32] 的@user,正在测试当前登录的用户=].这对于自述文件的这一部分“普通用户可以看到他们自己的用户配置文件”是有意义的。

我不明白的是,自述文件说用户也可以编辑自己的个人资料,但是 update 操作只允许管理员用户执行(然后事件,管理员可以只更改用户的角色?permit(:role)).

我建议遵循有关此类事情的最新指南: https://altalogy.com/blog/rails-6-user-accounts-with-3-types-of-roles/

您链接的存储库最后更新于 4 年前。我试图拉下回购并在本地测试你提出的这些要点,我 运行 尝试这样做时遇到了太多问题。在别处寻找这方面的指导。

编辑:虽然我确实查看了代码,但我不完全确定该应用程序如何按照 README 中的说明进行操作。