【问题标题】:Use CanCan to limit specific parameters使用 CanCan 限制特定参数
【发布时间】:2012-02-15 01:46:15
【问题描述】:

CanCanCan可以用来限制用户可以访问哪些参数化视图?

我们制作用户购买个人访问权限的图像。

例如:

Bob 可以访问图像 1、3 和 4。 Joe 可以访问图像 2 和 4。 Steve 可以访问图像 5。

网址类似于site.com/images/1

有没有一种方法可以限制用户可以查看的视图数量(参数)?有没有 CanCan?

我使用的是 Rails 3.2.1

谢谢

【问题讨论】:

  • "Can CanCan" - 嗯,我该如何以此来开土拨鼠的玩笑? :)

标签: ruby-on-rails permissions cancan


【解决方案1】:

这是我的一个项目中的一个示例。

class App
  has_many :app_ownerships
end

class User
  has_many :app_ownerships
end

class AppOwnership
  belongs_to :user
  belongs_to :app


end

ability.rb 中的某处

can :read, App do |app|
  # find all ownerships with at least read-only access (access_level == 1)
  active = app.app_ownerships.select do |o|
    o.app_id == app.id && o.user_id == user.id && o.access_level >= 1
  end
  active.length > 0
end

app_controller.rb 中的某处

def show
  @app = App.find params[:id]
  authorize! :show, @app # throws exception if not authorized

  ...
end

希望你可以使用这个 sn-p 来满足你的需要。

【讨论】:

  • 谢谢!我希望木夹头的笑话能传到你头上。它已经逃到了这一点
猜你喜欢
  • 2013-10-05
  • 2013-06-22
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多