如何使用Authlogic和STI获得动作级别保护?

时间:2010-02-12 02:46:04

标签: ruby-on-rails authlogic single-table-inheritance

鉴于如何使用before_filter进行单个用户分类,我很难记录,我无法获得多种用户类型的动作级保护。让我解释一下:

我有这样的事情......

class ApplicationController < ActionController::Base
  class << self 
    attr_accessor :standard_actions
  end
  @standard_actions = [:index, :show, :new, :edit, :create, :update, :destroy]

  def require_guardian
    unless current_user and current_user.is_a?(Guardian)
      store_location
      redirect_to home_url
      return false
    end
  end

  def require_admin
    unless current_user and current_user.is_a?(Administrator)
      store_location
      redirect_to register_url
      return false
    end
  end
end

在GuardiansController中,我想只允许管理员执行标准操作,但所有其他操作都需要Guardian。所以我尝试了这个......

class GuardiansController < ApplicationController
  before_filter :require_admin, :only => ApplicationController::standard_actions
  before_filter :require_guardian, :except => ApplicationController::standard_actions
  ...
end

最终会进行递归重定向。必须有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

好的,这是另一个不仔细看错了的情况。我无意中设置了以递归方式重定向用户的路由。正确设置路径时,上述解决方案可以正常工作:

def require_guardian
  unless current_user and current_user.is_a?(Guardian)
    store_location
    # this route (home_url) sent the user to another controller which ran a before_filter sending them back here again.
    # redirect_to home_url 
    # So I changed it to a neutral route and it works great!
    redirect_to register_url
    return false
  end
end