监狱长回调应放在rails应用程序中? [英] Where should warden callbacks be placed in a rails app?

查看:145
本文介绍了监狱长回调应放在rails应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比较新的轨道。我设置了Devise,并希望在用户登录后运行一些回调代码。



查看 Warden wiki页面,我可以使用after_set_user回调来执行此逻辑,例如:

  Warden :: Manager.after_set_user do | user,auth,opts | 
除非user.active?
auth.logout
throw(:warden,:message =>用户不活动)
end
end

但是,我不知道我应该在哪里存储这些东西。我的第一个想法是,我可以把它放在config / initializers / devise.rb中。那是对的吗?在配置目录中不要把本质上的控制器代码放在正确的位置。

解决方案

应用程序正在引导,所以Devise的初始化程序在config / initializers / devise.rb里面是一个很好的候选人。



然而,要实现的行为将更好地通过使用这个Devise功能:



https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in


I'm relatively new to rails. I have Devise set up, and want to run some callback code after users sign in.

Looking at the Warden wiki page, I can use the "after_set_user" callback to perform this logic, for example:

Warden::Manager.after_set_user do |user, auth, opts|
  unless user.active?
    auth.logout
    throw(:warden, :message => "User not active")
  end
end

However, I'm not sure where I should be storing this stuff. My first thought is that I could put it in config/initializers/devise.rb. Is that correct? It doesn't feel right putting what is essentially controller code in the config directory.

解决方案

Warden hooks need to be required when your application is booting, so inside Devise's initializer at config/initializers/devise.rb is a good candidate.

However, the behavior you want to achieve will be better accomplished by using this Devise feature:

https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in

这篇关于监狱长回调应放在rails应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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