簡體   English   中英

CanCan嵌套資源索引操作授權

[英]CanCan Nested Resource Index Action Authorization

我有帖子,在Feed中。

為了能夠查看提要中的帖子,提要中必須有一個。

在我的路線中,帖子嵌套在提要下。

在我的PostsController中,我有:

class Api::V1::PostsController < ApiController
  load_resource :network, find_by: :external_id, only: [:index, :create]
  load_resource :post, :through => :network, only: [:create]

  load_resource find_by: :external_id, except: [:create]

  authorize_resource

  def index
    @posts = @network.posts.page(params.has_key?(:page) ? params[:page] : 1).per(15)

  render status: 200,
           json: @posts
  end

對應能力:

can [:index, :create], Post do |post|
      user.in_feed? post.feed_id
end

由於某種原因,所有用戶均會通過授權。 我該如何解決?

這是CanCanCan的預期行為。 從有關使用塊定義能力的文檔中:

僅當存在實際實例對象時才評估該塊。 在檢查類的權限時(例如在index操作中),不會評估它。

由於indexcreate操作都不存在實例,因此該塊永遠不會運行。 你要

can [:create, :index], Post if user.in_feed? post.feed_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM