轨道4 - 设计,访客会导致过滤器链叫停 [英] Rails 4 - Devise, guest users causes filter chain halted

查看:121
本文介绍了轨道4 - 设计,访客会导致过滤器链叫停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在这里我使用制定进行用户身份验证一个Rails 4(4.2.3)的应用程序工作。我希望用户能够通过创建一个测试项目,并为来宾用户签署签署UPP之前玩的应用程序。当用户注册(或)我要分配测试项目为新的当前用户。

我从Platformatec本指南被以下内容:<一href=\"https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user\">https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user

创建guest用户的作品,但在签署或具有活动guest用户会议上,我得到以下错误:

 过滤器链停止为:require_no_authentication渲染或重定向

如果清除我的会话它的作品。该管理我的访客用户的方法是这样的:

 高清current_or_guest_user
  如果CURRENT_USER
    如果会话[:guest_user_id&放大器;&安培;会话[:guest_user_id]!= current_user.id
      在登录
      guest_user(with_retry = FALSE)。尝试(:毁灭)
      会话[:guest_user_id] =零
    结束
    当前用户
  其他
    guest_user
  结束
结束

如前所述,创建guest用户似乎工作就好了。但这种逻辑不会发生:

 #应该删除guest用户并清除会话。
如果CURRENT_USER
  如果会话[:guest_user_id&放大器;&安培;会话[:guest_user_id]!= current_user.id
    在登录
    guest_user(with_retry = FALSE)。尝试(:毁灭)
    会话[:guest_user_id] =零
  结束
  当前用户

我是pretty确保我的客人用户会话与我的新用户发生冲突并导致该错误设计(因为guest用户永远不会注册时删除):

 过滤器链停止为:require_no_authentication渲染或重定向

我的客人用户逻辑的其余部分看起来像这样挂指南正是或多或少:<一href=\"https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user\">https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user.我还添加了code从验证段落/例如:<一href=\"https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user#authentication-this-may-interfere-with-the-current_user-helper\">https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user#authentication-this-may-interfere-with-the-current_user-helper.

这是什么,我缺少的,任何想法如何,我可以让我的 current_or_guest_user 使用设计时要删除guest用户上注册和签名?

更新

这是我的路线目前的样子:

  devise_for:用户控制器:{会话:用户/会话,注册用户/注册}根:以= GT; 公共#索引资源:应用程序获得用户/秀,以:用户#秀
获得用户,以:用户#指数后客人/ receive_guest,到:宾客#receive_guest

更新2

该指南有如下语句:


  

当(如果)用户寄存器或原木中,我们删除了guest用户
  并清除会话变量。


它并不能解释如何以及在哪里做。我猜我得叫 current_or_guest_user 再什么地方。但我不知道在哪里,因为我不是那熟悉的设计。

更新3

为了让更多的清晰。这就是我要实现的步骤。


  1. 用户创建一个测试项目。

  2. 创建后测试项目中的guest用户和会话被创建。一旦会话结束或#3的guest用户应该被删除。

  3. 如果来宾用户注册,他/她应该得到登录和测试项目应该得到分配到新的真正的用户。

  4. 来宾用户应该被删除。


解决方案

谢谢您的回答skahlert和SsouLlesS。跳绳监狱长战略并帮助和定义回调似乎是一个干净的方法。除此之外,我有一些其他问题。

一个是我的自定义设计的控制器从未被调用。创建为另一个问题:<一href=\"http://stackoverflow.com/questions/33844122/rails-4-devise-for-not-using-custom-controllers\">Rails 4 - devise_for不使用自定义控制器的。该解决方案是我使用了错误的URL在我的模式申请表。我的窗体现在看起来是这样的:

  =的form_for(资源,如:RESOURCE_NAME,网址:user_registration_path做| F |

然后我一样skahlert建议并删除了<一个href=\"https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user#authentication-this-may-interfere-with-the-current_user-helper\"相对=nofollow>监狱长战略从我的应用。

在我的自定义色器件用户:: RegistrationsController&LT;设计:: RegistrationsController 控制器接到电话,我决定来覆盖创建操作。这不是一个完全的测试,但它看起来是这样的:

 #POST /资源
打造高清
  #删除guest用户和复位会话。
  new_user_from_guest = guest_user
  guest_user(with_retry = FALSE)。尝试(:毁灭)
  会话[:guest_user_id] =零  build_resource(sign_up_params)  resource.save
  如果block_given产生的资源?
  如果resource.persisted?
    如果resource.active_for_authentication?
      set_flash_message:通知:signed_up如果is_flashing_format?
      sign_up(RESOURCE_NAME,资源)      #来宾用户分配的应用程序到新的当前用户。
      new_user_from_guest.apps.each做|应用|
        app.user_id = current_user.id
        app.save!
      结束
      respond_with资源,地点:after_sign_up_path_for(资源)
    其他
      set_flash_message:通知:signed_up_but _#{} resource.inactive_message如果is_flashing_format?
      expire_data_after_sign_in!
      respond_with资源,地点:after_inactive_sign_up_path_for(资源)
    结束
  其他
    #重置测试用户如果注册失败,我没有测试过这部分呢。
    guest_user
    guest_user.apps&LT;&LT; new_user_from_guest.apps
    guest_user.save!    clean_up_passwords资源
    set_minimum_password_length
    respond_with资源
  结束
结束

一切似乎是现在的工作。可以做一些重构,并可能尝试回调方法,SsouLlesS在答复建议。

I have just started working on a Rails 4 (4.2.3) app where I use Devise for user authentication. I want users to be able to play around with the app before signing upp by creating a test project and be signed in as a guest user. When the user signs up (or in) I want to assign the test project to the new current user.

I have been following this guide from Platformatec: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user

Creating the guest user works, but when signing up or in with an active guest user session I get the following error:

Filter chain halted as :require_no_authentication rendered or redirected

If I clear my session it works. The method that manages my guest user looks like this:

def current_or_guest_user
  if current_user
    if session[:guest_user_id] && session[:guest_user_id] != current_user.id
      logging_in
      guest_user(with_retry = false).try(:destroy)
      session[:guest_user_id] = nil
    end
    current_user
  else
    guest_user
  end
end

As mentioned, creating guest users seems to work just fine. But this logic never happens:

# should delete the guest user and clear the session. 
if current_user
  if session[:guest_user_id] && session[:guest_user_id] != current_user.id
    logging_in
    guest_user(with_retry = false).try(:destroy)
    session[:guest_user_id] = nil
  end
  current_user

I'm pretty sure that my guest user session is conflicting with my new user and causes this Devise error (since the guest user never gets deleted on sign up):

Filter chain halted as :require_no_authentication rendered or redirected

The rest of my guest user logic looks more or less exactly like this linked guide: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user. I also added the code from the Authentication paragraph / example: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user#authentication-this-may-interfere-with-the-current_user-helper.

Any ideas on what I'm missing, and how I can get my current_or_guest_user to delete the guest user on signup and signing when using Devise?

Update

This is how my routes look currently:

devise_for :users, controllers: { sessions: "users/sessions", registrations:  "users/registrations" }

root :to => 'public#index'

resources :apps

get 'users/show', to: "users#show"
get 'users', to: "users#index"

post 'guests/receive_guest', to: "guests#receive_guest"

Update 2

The guide have the following statement:

When (and if) the user registers or logs in, we delete the guest user and clear the session variable.

It doesn't explain much how and where to do it. I'm guessing I have to call current_or_guest_user somewhere again. But I'm not sure where since I'm not that familiar with Devise.

Update 3

To make it a bit more clear. This is the steps I want to achieve.

  1. User creates a test project.
  2. Upon creation of the test project a guest user and session gets created. The guest user should get deleted once the session ends or #3.
  3. If the guest user signs up, he /she should get logged in and the test project should get assigned to the new real user.
  4. The guest user should get deleted.

解决方案

Thanks for your answers skahlert and SsouLlesS. Skipping the warden strategy did help and defining callbacks seems like a clean approach. Beside that I had some other issues.

One was that my custom Devise controllers never got called. Created another question for that: Rails 4 - devise_for not using custom controllers. The solution was that I used the wrong url in my modal signup form. My form now looks like this:

= form_for(resource, as: resource_name, url: user_registration_path do |f|

Then I did as skahlert suggested and removed the warden strategy from my app.

Once my custom devise Users::RegistrationsController < Devise::RegistrationsController controller got called I decided to override the create action. It's not completely tested, but it looks like this:

# POST /resource
def create
  # Delete guest user and reset session. 
  new_user_from_guest = guest_user
  guest_user(with_retry = false).try(:destroy)
  session[:guest_user_id] = nil

  build_resource(sign_up_params)

  resource.save
  yield resource if block_given?
  if resource.persisted?
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_flashing_format?
      sign_up(resource_name, resource)

      # Assign the guest users app to the new current user.
      new_user_from_guest.apps.each do |app|
        app.user_id = current_user.id
        app.save!
      end
      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
    # Reset test user if signup fails, I have not tested this part yet. 
    guest_user
    guest_user.apps << new_user_from_guest.apps
    guest_user.save!

    clean_up_passwords resource
    set_minimum_password_length
    respond_with resource
  end
end

Everything seems to work now. Could do some refactoring and possibly try the callback approach that SsouLlesS suggested in his answer.

这篇关于轨道4 - 设计,访客会导致过滤器链叫停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆