2015-06-18 138 views
0

TL; DR:我試圖從管理員帳戶創建一個新的Devise用戶,但註冊完成後,管理員會斷開連接並以新用戶身份登錄。設計註冊後禁止自動登錄

這裏是我的情況:

用戶可以通過輸入自己的大學登錄我的網站上註冊。 我的應用根據大學LDAP檢查此登錄,如果存在,它將在我自己的LDAP上覆制大學LDAP條目+在Rails應用上爲用戶創建數據庫條目。到目前爲止,這是好的,工作原理:

我做到這一點通過重寫制定登記控制器

class Student::RegistrationsController < Devise::RegistrationsController 

    skip_before_filter :access_denied 

    def create 
    login = sign_up_params[:login] 
    if already_in_our_ldap?(login) 
     redirect_to root_path and return 
    else 
     # Ask our university LDAP 
     university_ldap_entry = get_university_student(login) 
     # If the entry was found on the LDAP 
     if university_ldap_entry 
     add_to_our_ldap(university_ldap_entry) 
     # resume controller action 
     else 
     redirect_to new_user_registration_path and return 
     end 
    end 

    # Rest is more or less copy paste from Devise RegistrationController 
    build_resource(sign_up_params) 
    resource_saved = resource.save 
    yield resource if block_given? 
    if resource_saved 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_flashing_format? 
     sign_up(resource_name, resource) 
     respond_with resource, location: after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? 
     expire_data_after_sign_in! 
     respond_with resource, location: after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     set_minimum_password_length 
     respond_with resource 
    end 
    end 
end 

在我的情況

管理員必須能夠註冊學生,而已經連接上我網站。

所以,我有一個觀點,這將產生一個模式在那裏是有可能查詢名大學LDAP,並且將發佈登錄上述Student::RegistrationsController形式...

使用AJAX

我的問題

的AJAX形式效果很好,它成功地複製我們的LDAP +大學LDAP條目創建數據庫條目

BUT

它還創建一個對應於新用戶的會話(甚至不需要密碼)。因此,使用模式註冊學生的管理員與他的會話斷開連接(最重要的是,這是通過AJAX完成的,並且他不會注意到他作爲另一個用戶已斷開/重新連接,除非他重新加載頁面)

當然,預期的行爲是使用模式只需創建LDAP +數據庫條目,而無需更改會話。

EDIT

實際上,它也是用戶一個HTML/POST登記後連接。我的代碼是通過電子郵件將密碼發送給剛剛註冊的用戶,所以我不希望新用戶能夠通過輸入有效的登錄= _ =來自動連接!

回答

3

我的歉意,我真的不明白設計的sign_up功能。我認爲這種方法正在完成新用戶記錄的創建,但它只是試圖以sign_in作爲新創建的用戶。

所以這已經足夠了評論的線sign_up

我不是以英語爲母語,但我認爲sign_in意味着實際上掛職連接而sign_up是訂閱網站的行爲(不一定之後連接)。我錯了嗎 ?

+0

sign_in是您在進入某個網站時所要做的。設計sign_up方法,我相信,它是一個函數,當他們在sign_up看到這裏時登錄一個用戶----> http://www.rubydoc.info/github/plataformatec/devise/Devise/RegistrationsController#sign_up-instance_method – BKSpurgeon

+0

爲我節省了很多時間:) –