2013-12-11 25 views
0

我設法通過檢查控制器驗證用戶:讓某些類型的用戶編輯內容

before_filter :authenticate_user!, only: [:new, :edit] 

我怎樣才能讓只有權利「管理員」和「編輯」編輯和創建內容的用戶?

我想somethng這樣的:

before_filter :current_user.rights == 'editor', only: [:new, :edit] 

檢查當前登錄的用戶的價值在DB的專欄「權利」

順便說一句,我使用設計和只有1個用戶登記表。有一個用戶權限複選框(因爲'類型'是不允許的,我不想改變這一點)。

Regitration form

回答

2

您可以添加其他before_filter

before_filter :authorize_admin, :only => [...] 

def authorize_admin 
    unless currenct_user.admin? # Add your logic here 
    redirect_to(root_path, :notice => 'You are not authorized') and return 
    end 
end 

如果有多個用戶角色和權限,你可以在這裏嘗試CanCan gem

+0

我可以使用類似current_user.right ==「編輯」 ???我需要這種類型的邏輯,但是這種輸入不起作用 – AlexEfremo

+1

是的,您可以使用該邏輯。你有什麼錯誤嗎? – Santhosh

+0

哈哈。對不起,我忘記了我一點也沒有聽過。我的壞 – AlexEfremo

1

使用康康舞的寶石,你可以找到很好的文檔https://github.com/ryanb/cancan 剛一行將在爲用戶定義能力之後爲您完成

load_and_authorize_resource 

您也可以選擇傳遞方法的參數只有和除

相關問題