2013-09-26 271 views
8

我試圖讓它只有管理員可以添加用途與設計。我已經得到它主要工作,但現在當我作爲管理員登錄,並提交註冊形式它踢我回來的錯誤:You are already signed in.允許管理員添加用戶與設計

我試圖按照這裏的說明:http://wiki.summercode.com/rails_authentication_with_devise_and_cancan但它似乎沒有提到這種情況。

我是否需要在editors_controller中進一步覆蓋以允許這樣做?

這裏是我的路線( 「編輯」 我的用戶模型的名稱):

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" } 

和我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沒有被調用,但假設因爲EditorsControllerRegistrationController繼承過濾器將正常工作之前。情況並非如此嗎?

回答

6

您需要在EditorsController上實施自己的create方法,而不是從Devise::RegistrationsController繼承該操作。正如你所看到的,Devise::RegistrationsController中的方法將首先檢查你是否已經登錄,如果你是的話,你會回來。如果您未登錄,它將創建一個User帳戶,然後以該用戶的身份登錄。

你試圖避開這一問題skip_before_filter :require_no_authentication,但很可能是你的表格是POST荷蘭國際集團以/editors而不是/admin/editors。所以,你需要添加一個路線,讓你去createEditorsController

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')。

+0

hello Jeremey我已經嘗試過你的創建動作,但它仍然繞過表單,並用「你已經登錄」的信息舔我。有任何想法嗎? – BKSpurgeon

1

沒有任何googleable解決方案工作時,我嘗試了他們。這工作

我所做的是在控制器中創建一個新的動作,併爲它創建一個新的路由,並連接我的視圖上的鏈接,這些鏈接通常連接到創建,現在調用我的路由和操作。

但這還不夠。因爲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| %> 
+0

我無法讓你的解決方案爲我工作,但它給了我一些我可以使用的想法。謝謝。 – guero64

0

在這裏使用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 

一下添加到配置/ routes.rb中:

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

添加此類方法將用戶模式:

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

我沒有在我的應用程序單獨管理類。相反,我爲用戶定義了一個管理員屬性,並使用UsersController中的:validate_adminbefore_action篩選器進行檢查。

我希望能夠創建一個從:index視圖的新用戶,所以我增加了一個按鈕:

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

您可能需要調整上述方案,如果你有在用戶模型中的任何after_create行動(例如發送歡迎電子郵件)。

相關問題