2009-10-09 61 views
1

我試圖讓permit方法使用rails-authorization-pluginauthlogic上班,我一直運行到這個錯誤:Rails的授權插件出錯

當我嘗試:

class ApplicationController < ActionController::Base 
    ... 
    before_filter permit 'admin' 
    ... 

我得到這個:

Authorization::CannotObtainUserObject in HomeController#index 
Couldn't find #current_user or @user, and nothing appropriate found in hash 

現在,我有我的current_user方法設置,和它的作品,因爲我在我的應用程序使用它幾乎無處不在其他:

class ApplicationController < ActionController::Base 
    ... 

    helper_method :current_user 

    private 

    def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
    end 

    def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.record 
    end 

    ... 

我也知道,我有我的數據庫中相應角色的用戶,因爲這種方法的工作原理:

def require_admin 
    unless current_user.is_admin? || current_user.is_root? 
     flash[:warning] = 'You are not an administrator and cannot access this page.' 
     redirect_to root_path 
    end 
end 

我可以做一切工作,如果我只是在用戶級檢查使用這樣的:

before_filter :require_admin, :only => 'index' 

...但我不應該能夠與permitpermit?有效是一回事嗎?

任何幫助將不勝感激。如果您需要查看更多代碼,請告訴我,我很樂意發佈。谷歌真的沒有什麼能夠讓我們能夠讓這兩個系統相互合作。

回答

2

好吧,我想我想通了。

作爲賈裏德正確地指出的那樣,合適的用法是

permit 'admin' 

(不作爲before_filter的一部分)。

無論其...

...默認:get_user_method設置爲#current_user,這是什麼acts_as_authenticated插件使用。如前所述,我正在使用AuthLogic,在那裏我定義的方法爲current_user(不含英鎊符號)。

所以,我已經試過如下:

permit 'admin', :get_user_method => current_user 

只能由一個不錯的錯誤消息,說明我沒有這樣的變量或方法來迎接。然而,我遺漏的是,散列選項需要字符串,而不是直接調用該方法! (愚蠢的錯誤,我知道了!)

所以

permit 'admin', :get_user_method => 'current_user' 

...似乎爲我工作。

我愛Ruby和Rails,但有時它的簡單可能是它自己的詛咒;我總是擁有簡單的東西。 :)

0

您正在使用該插件不正確。它不應該放在過濾器之前。

在全球範圍內,只需聲明:

permit 'admin' 

就是這樣。

您的所有操作都將查找current_user或@user對象,如果不是,則重定向到登錄頁面。

在每個行動水平,你把它作爲一個塊:

def index 
    permit 'admin' do 
    @some_models = SomeModel.all 
    end 
end 
+0

我改變它像你所建議的(在全球範圍內),所以現在它只是讀'允許'管理員',但我仍然得到相同的錯誤。當我嘗試在每個操作級別上執行操作時,我會得到同樣的錯誤。 ?? – neezer 2009-10-10 14:51:22