簡體   English   中英

Rails Authority的寶石,“顯示”操作遇到麻煩

[英]Rails Authority gem, trouble with 'show' action

這是我第一次使用此gem,它使我發瘋,例如僅授權資源所有者執行show action這樣簡單的事情。

我嘗試了不同的方法,配置了控制器映射和操作,但是總是得到未經授權的show消息,其他操作也可以正常工作。

似乎show並沒有通往ApplicationAuthorizer。

它是這樣配置的:

class EnterpriseAuthorizer < ApplicationAuthorizer
  # This works
  def self.creatable_by?(user)
    user.is_enterpriser?
  end
  # This doesn't
  def readable_by?(user)
    true # Just for testing
  end 
end

class EnterprisesController < ApplicationController
  authorize_actions_for Enterprise
  def show
   @enterprise = Enterprise.find(params[:id])
     respond_to do |format|
      format.html
      format.json { render json: @enterprise }
     end
 end

我在用戶中include Authority::UserAbilities ,在企業模型中include Authority::Abilities 和用戶has_one :enterprise

任何想法? 認真考慮回退到cancan。

提前致謝。

授權具有檢查權限的不同方法。 對於基於集合的操作(例如,新建,創建,索引),請使用authorize_actions_for Model

對於基於實例的操作(例如,編輯,更新,顯示,刪除),必須調用authorize_action_for @instance

將您的代碼更改為此,它應該工作。

class EnterprisesController < ApplicationController
  authorize_actions_for Enterprise
  def show
    @enterprise = Enterprise.find(params[:id])
    authorize_action_for @enterprise
    respond_to do |format|
      format.html
      format.json { render json: @enterprise }
    end
  end
end

如果您想使用一種比較簡單的方法,請將

@enterprise = Enterprise.find(params[:id])
authorize_action_for @enterprise

放入每個實例操作調用的before過濾器。

暫無
暫無

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

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