2011-06-02 73 views
2

我在我的應用程序中有一個管理員用戶,只有管理員可以在此應用程序中創建和激活用戶。如何在沒有登錄的情況下注冊?

當我創建一個用戶時,devise爲這個新用戶做了一個自動登錄。我如何創建一個沒有自動登錄的用戶?

+0

那麼,你希望管理員能夠創建用戶,但是這些用戶在激活之前無法登錄? – Justin 2011-06-02 14:24:39

回答

0

我不太清楚你想要達到的目標:

  1. 只需創建一個用戶實例,然後可以登錄

  2. 創建一個新用戶,並通知他們自己的帳戶已被創建(即「邀請他們」)

在第一種情況下,只需創建一個實例User用適當的信息(檢查哪些字段需要在控制檯來完成:他們依賴於你的配置和「策略」你使用:可確定的,可鎖定等)

在第二種情況下,你可能想看看這樣的事情:https://github.com/scambra/devise_invitable

2

你必須重寫登記控制器(看到像this one教程)

然後,看着原來的代碼(可以發現here),你必須編輯create部分。

原廠1

# POST /resource 
    def create 
    build_resource 

    if resource.save 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_in(resource_name, resource) 
     respond_with resource, :location => redirect_location(resource_name, resource) 
     else 
     set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords(resource) 
     respond_with_navigational(resource) { render_with_scope :new } 
    end 
    end 

你要尋找的是刪除此行sign_in(resource_name, resource)

我希望我理解正確你的問題。

0

假定User是您的模型,請將users表中的名爲is_active的布爾字段添加到表中。然後使用方法active?User模型:

class User < ActiveRecord::Base 

    #this method will be used by devise to determine if the user is "active" 
    def active? 
    #Allow user to log in if the user is confirmed AND if we are allowing 
    #the user to login 
    super and (not self.confirmed_at.nil?) and self.is_active? 
    end 
end 

要從登錄禁用用戶,在User模型中場is_active設置爲false在before_create濾波器。或者,在遷移中將缺省值設置爲false

is_active設置爲true以允許用戶登錄。

相關問題