簡體   English   中英

設計和康康寶石:has_many協會

[英]Devise and cancan gems: has_many association

我使用的devisecancan的寶石,擁有簡單的模型關聯: user has_many subscriptionssubscription belongs_to :user 具有以下SubscriptionsController

class SubscriptionsController < ApplicationController

  load_and_authorize_resource :user
  load_and_authorize_resource :subscription, through: :user

  before_filter :authenticate_user!

  def index
    @subscriptions = @user.subscriptions.paginate(:page => params[:page]).order(:created_at)
  end

  #other actions
end

Cancan Ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)

    user ||=User.new

    can [:read], [Edition, Kind]

    if user.admin?
      can :manage, :all
    elsif user.id
      can [:read, :create, :destroy, :pay], Subscription, user_id: user.id
      can [:delete_from_cart, :add_to_cart, :cart], User, id: user.id
    end

  end

end

問題是我不能以用戶身份使用subscriptions操作,但可以以管理員身份使用。 並且與UsersController沒有問題。 當我從SubscriptionsController刪除以下SubscriptionsController

  load_and_authorize_resource :user
  load_and_authorize_resource :subscription, through: :user

  before_filter :authenticate_user!

完全沒有問題。 因此,這些行或Ability.rb 有什么建議么?

更新:有趣的是,如果我可以添加smth can? :index, Subscription can? :index, Subscription html模板時顯示true 如果可以添加水深can? :index, Subscription.first can? :index, Subscription.first (另一個用戶的訂閱),它顯示false 看起來Cancan可以正常工作。 但是怎么了?

更新 :如果更改SubscriptionsControlle像:

class SubscriptionsController < ApplicationController

  #load_and_authorize_resource :user
  #load_and_authorize_resource :subscription, through: :user

  before_filter :authenticate_user!

  def show
    @user = User.find params[:user_id] #line 1
    @subscription = @user.subscriptions.find params[:id] #line 2
    @container_items = @subscription.container_items.paginate(:page => params[:page])
    authorize! :show, @subscription #line 4
  end

  #some actions
end

它可以完美工作並在需要時防止未經授權的用戶訪問。 #1、2和4行不等於注釋嗎?

更新 :在routes.rb具有以下routes.rb

resources :users, except: [:show] do
    member do
      get 'cart'
      delete 'delete_from_cart' => 'users#delete_from_cart'
      post 'add_to_cart' => 'users#add_to_cart'
    end
    resources :subscriptions do
      member do
        post 'pay'
      end
    end
  end

更新 :下一個解決方案防止未經授權訪問除index之外的所有訂閱操作:

class SubscriptionsController < ApplicationController

  load_resource :user
  load_resource :subscription, through: :user
  authorize_resource through: :current_user

  before_filter :authenticate_user!

  #actions
end

那么,阻止訪問index操作的最佳方法是什么?

僅找到以下解決方案:

  before_filter :authorize_index, only: [:index]

  def authorize_index
    raise CanCan::AccessDenied unless params[:user_id] == current_user.id.to_s
  end

它應該是

load_and_authorize_resource :subscription

要不就

load_and_authorize_resource

在您的情況下,當您想要嵌套資源時,

load_and_authorize_resource :through => :current_user

參見https://github.com/ryanb/cancan/wiki/Nested-Resources

暫無
暫無

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

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