【发布时间】:2020-09-18 00:13:35
【问题描述】:
我发现this example Ruby on rails app 展示了devise gem,它与角色一起使用。
他们在自述文件中提到:
普通用户无法更改其角色
普通用户可以查看(和编辑)自己的用户资料
然而,看着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 之外,所有操作都只允许管理员用户使用,其中正在测试当前登录的用户是否是我们正在尝试获取/显示的@user。这对于自述文件“普通用户可以看到自己的用户配置文件”的这一部分是有道理的。
我不明白的是,自述文件说用户也可以编辑自己的个人资料,但 update 操作只允许由管理员用户执行(然后,管理员只能更改用户的角色?permit(:role))。
【问题讨论】:
标签: ruby-on-rails ruby devise roles