【问题标题】:Allowing admins to add users with Devise允许管理员使用 Devise 添加用户
【发布时间】:2013-09-26 01:43:13
【问题描述】:

我正在努力做到这一点,以便只有管理员可以通过设计添加用途。我已经让它大部分工作了,但是现在当我以管理员身份登录并提交注册表单时,它把我踢回来并出现错误:You are already signed in.

我已尝试按照此处的说明进行操作:http://wiki.summercode.com/rails_authentication_with_devise_and_cancan,但似乎没有提及这种情况。

我是否需要在 editors_controller 中进一步覆盖以允许这样做?

这是我的路线(“editors”是我的用户模型的名称):

devise_for :admins, :skip => [:registrations]

as :admin do
  get 'admin/editors'        => 'editors#index',                  as: :admin_editors
  get 'admin/editors/new'    => 'editors#new',                    as: :new_editor
  delete 'admin/editors/:id' => 'editors#destroy',                as: :destroy_editor
end


devise_for :editors, :skip => [:registrations],  :controllers => { :registrations => "editors" }

还有我在“app/controllers/”中的editors_controller

    class EditorsController < Devise::RegistrationsController
  before_filter :check_permissions, :only => [:new, :create, :cancel]
  skip_before_filter :require_no_authentication

  def dashboard
    render "editors/dashboard.html.haml"
  end

  def index
    @editors = Editor.all
    respond_to do |format|
      format.html
    end
  end

  private
    def check_permissions
      authorize! :create, resource
    end
end

编辑 当我提交表单时,我在日志中注意到了这个Processing by Devise::RegistrationsController#create as HTML。我曾怀疑skip_before_filter :require_no_authentication 可能没有被调用,但假设因为EditorsController 是从RegistrationController 继承的,所以过滤器可以正常工作。不是这样吗?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    您需要在EditorsController 上实现自己的create 方法,而不是从Devise::RegistrationsController 继承该操作。如您所见,Devise::RegistrationsController 中的方法将首先检查您是否已登录,如果已登录则将您踢回。如果您未登录,它将创建一个User 帐户,然后以该用户身份登录。

    您正在尝试使用skip_before_filter :require_no_authentication 解决该问题,但您的表单很可能是POSTing 到/editors,而不是/admin/editors。因此,您需要添加一条路线,让您可以在EditorsController 上到达create

    as :admin do
      post 'admin/editors' => 'editors#create'
      # your other :admin routes here
    end
    

    然后你会想要实现一个缩小版的create。你可能想要这样的东西:

    class EditorsController < Devise::RegistrationsController
      def create
        build_resource(sign_up_params)
        if resource.save
          redirect_to admin_editors_path
        else
          clean_up_passwords resource
          respond_with resource
        end
      end
    
      # your other methods here
    end
    

    您还需要确保 admin/editors/new 模板将表单指向正确的路径 ('admin/editors')。

    【讨论】:

    • 你好 Jeremey 我已经尝试过你的创建操作,但它仍然绕过表单并用“你已经登录”消息舔我。有什么想法吗?
    【解决方案2】:

    当我尝试它们时,没有一个可以用谷歌搜索的解决方案。这行得通

    我所做的是在控制器中创建一个新操作并为其创建一个新路由,然后连接我的视图上通常连接的链接以创建现在调用我的路由和操作。

    但这还不够。因为 Devise 正在倾听,并且会抓取您尝试执行的任何添加并通过它自己的代码对其进行验证。因此,我只是使用 sql 插入添加新的用户记录。

    添加这条路线

    post 'savenew', to: 'users#savenew'
    

    将此操作添加到用户控制器:

    def savenew
      rawsql = "insert into users (email, created_at,updated_at) values ('#{user_params[:email]}',now(), now())"
      sql = rawsql
      ActiveRecord::Base.connection.execute(sql)
      redirect_to action: 'index''
    end
    

    查看:new.html.erb 更改 form_for 以便提交将转到新的路线和操作,而不是默认的 Rails 之一。

    <%= form_for User, :url => {:action => "savenew"} do |f| %>
    

    【讨论】:

    • 我无法让您的解决方案为我工作,但它给了我一些可以使用的想法。谢谢。
    【解决方案3】:

    在这里使用 Rails 4.2.6(我的模型是用户而不是编辑器)。以下解决方案绕过(我认为)任何可能干扰管理员创建新用户的设计操作:

    将此操作添加到用户控制器:

    def savenew
      User.create_new_user(user_params)
      redirect_to action: 'index'
    end
    

    将此私有方法添加到用户控制器(如果它不存在):

    private
    
    def user_params
      params.require(:user).permit(:email, :password,      
                                   :password_confirmation)
    end
    

    将此添加到 config/routes.rb:

    match '/savenew', to: 'users#savenew', via: :post
    

    将此类方法添加到 User 模型中:

    def self.create_new_user(params)
      @user = User.create!(params)
    end
    

    我的应用程序中没有单独的 Admin 类。相反,我为用户定义了一个管理属性,并在 UsersController 中使用 :validate_admin before_action 过滤器检查它。

    我希望能够从:index 视图创建一个新用户,所以我添加了一个按钮:

    <%= button_to 'New User', '/new_user', class: 'btn btn-primary',
                              method: :get %>
    

    如果您在 User 模型中有任何 after_create 操作(例如发送欢迎电子邮件),您可能需要调整上述解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多