绕过标记为公共的特定记录的设计授权的最佳方法是什么

时间:2012-03-05 20:17:21

标签: ruby-on-rails devise cancan

我在Rails 3.2项目中使用了devise和cancan。我有一个事件模型 布尔标志公开。如果事件标记为 public =>是的然后我想要任何人登录或不能使用

访问记录
GET /events/:id

如果标记为 public =>错误然后一系列康康技能将决定授权和访问上述资源。

实现这一目标的最佳模式是什么?

2 个答案:

答案 0 :(得分:21)

你可以跳过authenticate_user来做到这一点!如果你有这个args

  skip_before_filter :authenticate_user!, :only => :show, :if => lambda { 
    if params[:id]
      @event = Event.find(params[:id])
      @event and @event.public?
    else
      false
    end
  }

答案 1 :(得分:-2)

不,不要跳过身份验证。您想跳过授权。更好的解决方案是使用public =>明确授权事件。真。

在你的康康技能课程中:

can :read, Event do |e|
  some_other_authorization_boolean || e.public?
end

这项能力将授予所有用户;即使是那些没有登录的人。

相关问题