警告:无法验证CSRF令牌真实性:使用OmniAuth进行设计

时间:2012-08-20 05:17:13

标签: ruby-on-rails security devise omniauth helper

突然间,我在提交表单时开始收到此错误消息。我做的唯一改变是调用ApplicationController中定义的方法,该方法被标记为helper。

应用/控制器/ application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :is_org_admin?

  #Check if current user is Admin in current organization or not
  def is_org_admin?
    session[:current_organization].is_admin?(current_user)
  end
end

is_admin?定义为

class Organization < ActiveRecord::Base
  has_many :memberships
  has_many :users, through: :memberships

  def is_admin?(user)
    member = memberships.where(user_id: user.id).first
    if member.nil?
      false
    else
      member.is_admin?
    end
  end
end

is_org_admin?从视图模板中调用方法

应用/视图/用户/ index.html.haml

 %h1 Users
    - if is_org_admin?
      %p= link_to 'Invite new user', new_invitation_path

如果我在ApplicationController中删除protect_from_forgery或在index.html.haml中调用helper方法if is_org_admin?,它就可以工作。但是当我启用protect_from_forgery或调用ApplicationController中定义的助手的条件时,它会发出警告并显示您需要在页面上继续消息之前登录或注册。

有人请问这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我没有保存整个组织模型,而是在会话中保存它的id以拥有当前组织的实例。因此,根据需要,我会动态搜索。

Organization.find(session[:current_organization_id]).is_admin?(current_user)

我仍然想知道如果组织AR从会话中保存和使用,它会返回401。