如何只允许管理员创建新用户?
How to allow ONLY Admins to create new users?
我正在使用 Devise,我希望只有管理员 可以创建新用户。我已经查看了 This 个答案,但它看起来已经过时了。我已经尝试了很多可能的答案,但没有任何效果。我正在寻找一个详细的答案,因为我还是个新手。
管理员在用户中标有布尔值table我正在努力保持最小化。
最简单的方法是向您的用户控制器添加 before_action 限制创建和编辑以及您想要的任何其他操作特定条件
before_action :create_user , only: [:edit , :update , :new, :correct_user]
然后你可以定义一个create user private方法
def create_user
@user=User.find(params[:id])
redirect_to @user unless @user.criteria == criteria
end
希望这就是您要找的。如果没有,请发表评论并提供更多详细信息。
#
# In Users_Controller
#
before_action :check_user_role , only: [:edit , :update , :new, :create]
def check_user_role
redirect_to home_path if current_user.role != "Your Role"
end
您可以通过多种方式实现这一点。我过去所做的是结合 showing\hiding 视图中的链接和检查控制器中的用户。我假设您有一个包含用户详细信息的表单,您将提交给用户控制器。
我在下面开发的一个应用程序中添加了一个控制器。
我做的第一件事是检查用户是否已通过身份验证(我们为此使用 google,但如果您已经设置了设备,则不需要此功能,并且可能有您自己的身份验证地方)。如果您已登录,设计将创建 current_user 对象,其中应包含您的 "role" 属性。在标准用户创建中,您可以检查当前 user.role 并在 current_user.role 不为 1 时简单地重定向(我假设 1 表示管理员)。
class UsersController < ApplicationController
# Set the user record before each action
before_action :set_user, only: [:show, :edit, :update, :destroy]
# User must authenticate to use all actions in the users controller
before_filter :authenticate_user!
def create
if current_user.role = 1 then
@user = User.new(user_params)
@user.password = Devise.friendly_token[0,20]
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
else
format.html { redirect_to @user, notice: 'You do not have sufficient rights to set up a new user.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:notice] = "User record does not exist"
redirect_to users_url
end
end
我正在使用 Devise,我希望只有管理员 可以创建新用户。我已经查看了 This 个答案,但它看起来已经过时了。我已经尝试了很多可能的答案,但没有任何效果。我正在寻找一个详细的答案,因为我还是个新手。
管理员在用户中标有布尔值table我正在努力保持最小化。
最简单的方法是向您的用户控制器添加 before_action 限制创建和编辑以及您想要的任何其他操作特定条件
before_action :create_user , only: [:edit , :update , :new, :correct_user]
然后你可以定义一个create user private方法
def create_user
@user=User.find(params[:id])
redirect_to @user unless @user.criteria == criteria
end
希望这就是您要找的。如果没有,请发表评论并提供更多详细信息。
#
# In Users_Controller
#
before_action :check_user_role , only: [:edit , :update , :new, :create]
def check_user_role
redirect_to home_path if current_user.role != "Your Role"
end
您可以通过多种方式实现这一点。我过去所做的是结合 showing\hiding 视图中的链接和检查控制器中的用户。我假设您有一个包含用户详细信息的表单,您将提交给用户控制器。
我在下面开发的一个应用程序中添加了一个控制器。
我做的第一件事是检查用户是否已通过身份验证(我们为此使用 google,但如果您已经设置了设备,则不需要此功能,并且可能有您自己的身份验证地方)。如果您已登录,设计将创建 current_user 对象,其中应包含您的 "role" 属性。在标准用户创建中,您可以检查当前 user.role 并在 current_user.role 不为 1 时简单地重定向(我假设 1 表示管理员)。
class UsersController < ApplicationController
# Set the user record before each action
before_action :set_user, only: [:show, :edit, :update, :destroy]
# User must authenticate to use all actions in the users controller
before_filter :authenticate_user!
def create
if current_user.role = 1 then
@user = User.new(user_params)
@user.password = Devise.friendly_token[0,20]
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
else
format.html { redirect_to @user, notice: 'You do not have sufficient rights to set up a new user.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:notice] = "User record does not exist"
redirect_to users_url
end
end