簡體   English   中英

集成繼承的資源和權限

[英]Integrating inherited_resources and authority

我目前正在嘗試將Inherited_resources和權限集成到我的Rails應用程序中。

關於最好的地方,我有些困惑,無法根據資源檢查執行控制器操作的能力。 此代碼是權威示例:

  def edit
    @llama = Llama.find(params[:id])
    authorize_action_for(@llama)        # Check to see if you're allowed to edit this llama. failure == SecurityViolation
  end

  def update
    @llama = Llama.find(params[:id])
    authorize_action_for(@llama)        # Check to see if you're allowed to edit this llama.
    @llama.attributes = params[:llama]  # Don't save the attributes before authorizing
    authorize_action_for(@llama)        # Check again, to see if the changes are allowed.
    if @llama.save?
    # etc
  end

因為在Inherited_resources中發現者被抽象化了,所以我認為也可以將authorise_action_for檢查authorise_action_for到這些抽象的發現者上,這是很好的。

在更新(可能是創建)的情況下,請注意權限的雙重檢查。

我依靠ActiveSupport::Concern來簡化模塊。 我將我的顧慮存儲在app下的名為concerns的目錄中。 我稱這個為inherited_resources_with_authority.rb ,您可能需要修改application.rbautoload_paths才能從此文件夾加載文件。

module InheritedResourcesWithAuthority

    extend ActiveSupport::Concern

    included do
        inherit_resources
        authorize_actions_for :resource_class

        alias_method_chain :resource, :authority
        alias_method_chain :build_resource, :authority
        alias_method_chain :update_resource, :authority
    end

    protected

    def resource_with_authority
        resource_without_authority
        authorize_action_for(get_resource_ivar)
    end

    def build_resource_with_authority
        build_resource_without_authority
        authorize_action_for(get_resource_ivar)
    end

    def update_resource_with_authority(object, attributes)
        object.assign_attributes(*attributes)
        authorize_action_for(object)
        object.save
    end

end

我們基本上是在鏈接重要的inherited_resources的抽象方法,並在必要時插入我們的授權代碼。 最后一個是最棘手的問題,因為我們無法調用要鏈接到的原始方法,因此我們必須在此處復制一些inherited_resources代碼。

要使用此關注點,只需從控制器調用include InheritedResourcesWithAuthority

請注意 ,您不得在控制器上使用激活inherited_resources的資源的類繼承方法,因為我們已經在此問題中使用了其他方法。

完整的文章在這里: https ://coderwall.com/p/tp5sig

建議絕對歡迎:D

暫無
暫無

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

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