2015-06-18 86 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

为我节省了很多时间:) –