2011-07-29 194 views
3

我正在使用Devise作爲身份驗證模塊的Rails應用程序,但是我想對其進行自定義,以便CanCan只會允許管理員創建新用戶。我很難理解如何爲Devise定製控制器,以便做到這一點。任何幫助,將不勝感激。設計限制登錄管理員

+0

基本上,您需要將控制器操作限制爲您的管理員角色。我會建議尋找CanCan,您可以使用簡單的定義文件來限制模型上的操作。這裏是一個不錯的自述和wiki的項目頁面:https://github.com/ryanb/cancan –

回答

0

您可以創建一個「用戶」控制,將管理用戶,然後簡單地設置權限路由招它。因此,在您新的用戶控制器,你可以有這樣的:

class UserController < ApplicationController 
    load_and_authorize_resource 

    def index 
    end 

    def new 
    end 

    def show 
    end 

    def create 
    if @user.save 
     flash[:notice] = "Successfully created User." 
     redirect_to root_path 
    else 
     render :action => 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    params[:user].delete(:password) if params[:user][:password].blank? 
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank? 
    if @user.update_attributes(params[:user]) 
     flash[:notice] = "Successfully updated User." 
     redirect_to user_index_path 
    else 
     render :action => 'edit' 
    end 
    end 

    def destroy 
    if @user.destroy 
     flash[:notice] = "Successfully deleted User." 
     redirect_to user_index_path 
    end 
    end 

end 

假設你有管理員設置爲:

can :manage, :all 

你應該是好去。

在你的路由文件,你需要設置你的路由:

resources :user, :controller => "user" 

希望這有助於!

+0

這將破壞設計悲慘,並不會解決問題。要做到這一點:爲設計添加一個前綴(devise_for:users,:path_prefix =>'d'),並確保在devise_for後添加資源。不要刪除:從用戶註冊。此外,此控制器未添加devise所需的授權過濾器。試試:https://github.com/plataformatec/devise/wiki/How-To:-Use-CanCan-+-Devise,-The-easy-way。 (寫我自己) – 2011-07-30 06:28:10

5

你並不需要定製什麼:d

  • 刪除:登記的從設計模型。

  • 創建用戶CRUD *(只是腳手架用戶)

  • 使用康康舞爲你的用戶 控制器的用戶權限。

*檢查制定的關於如何創建一個用戶CRUD維基,有你需要做的