设计防止注册后自动登录
Devise prevent auto sign-in after registration
TL;DR : 我正在尝试从管理员帐户创建一个新的 Devise 用户,但在注册完成后,管理员断开连接并以新用户身份登录。
这是我的场景:
用户可以通过输入他们的大学登录名在我的网站上注册。
我的应用程序根据大学 LDAP 检查此登录名,如果存在,它将在我自己的 LDAP 上复制大学 LDAP 条目 + 在 rails 应用程序上为用户创建一个数据库条目。到目前为止这很好,并且有效:
我通过重写 Devise Registration Controller 来做到这一点
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 的名称,还有一个表单,它将 POST 登录到上面 Student::RegistrationsController
...
正在使用 AJAX
我的问题
AJAX 表单运行良好,它在我们的 LDAP 中成功复制了大学 LDAP 条目 + 创建数据库条目
但是
它还会创建一个与新用户对应的会话(甚至不要求输入密码)。因此,使用模式注册学生的管理员与他的会话断开连接(最重要的是,这是通过 AJAX 完成的,他不会注意到他已经 deconnected/reconnected 作为另一个用户,除非他重新加载页面)
当然,预期的行为是使用模式应该只创建 LDAP+DB 条目,而不更改会话。
编辑
实际上,它还会在 html/POST 注册后连接用户。我的代码通过电子邮件将密码发送给刚刚注册的用户,所以我不希望新用户只需输入有效的登录名即可自动连接 =_= !
抱歉,我没有真正理解devise的sign_up
功能。我认为此方法正在完成新用户记录的创建,但它只是尝试 sign_in
作为新创建的用户。
所以评论行sign_up
就足够了
我的母语不是英语,但我认为 sign_in
意味着实际连接一个人的凭据,而 sign_up
是订阅网站的行为(不一定是之后才连接) .我错了吗?
TL;DR : 我正在尝试从管理员帐户创建一个新的 Devise 用户,但在注册完成后,管理员断开连接并以新用户身份登录。
这是我的场景:
用户可以通过输入他们的大学登录名在我的网站上注册。 我的应用程序根据大学 LDAP 检查此登录名,如果存在,它将在我自己的 LDAP 上复制大学 LDAP 条目 + 在 rails 应用程序上为用户创建一个数据库条目。到目前为止这很好,并且有效:
我通过重写 Devise Registration Controller 来做到这一点
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 的名称,还有一个表单,它将 POST 登录到上面 Student::RegistrationsController
...
正在使用 AJAX
我的问题
AJAX 表单运行良好,它在我们的 LDAP 中成功复制了大学 LDAP 条目 + 创建数据库条目
但是
它还会创建一个与新用户对应的会话(甚至不要求输入密码)。因此,使用模式注册学生的管理员与他的会话断开连接(最重要的是,这是通过 AJAX 完成的,他不会注意到他已经 deconnected/reconnected 作为另一个用户,除非他重新加载页面)
当然,预期的行为是使用模式应该只创建 LDAP+DB 条目,而不更改会话。
编辑
实际上,它还会在 html/POST 注册后连接用户。我的代码通过电子邮件将密码发送给刚刚注册的用户,所以我不希望新用户只需输入有效的登录名即可自动连接 =_= !
抱歉,我没有真正理解devise的sign_up
功能。我认为此方法正在完成新用户记录的创建,但它只是尝试 sign_in
作为新创建的用户。
所以评论行sign_up
我的母语不是英语,但我认为 sign_in
意味着实际连接一个人的凭据,而 sign_up
是订阅网站的行为(不一定是之后才连接) .我错了吗?