Ruby on Rails-Pundit-控制器的未知操作

时间:2018-07-28 18:24:54

标签: ruby-on-rails pundit

此问题是指this特定问题。

我将pundit用作我的授权gem,我希望用户X只能下载属于用户X的用户信息。现在,我以http://localhost:3000/download.csv作为链接来接收用户信息。但是,如果我登录到另一个用户(例如user / 2),则仍然可以输入url并下载user / 3数据。

我现在所拥有的:

user_policy.rb

class UserPolicy < ApplicationPolicy

  def profile?
    true
  end

  def download?

  end

  private

  def user_or_admin
    user.id == record.id || user.admin?
  end


end

application_policy.rb

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    create?
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope.all
    end
  end
end

这是我的user_controller.rb#这不是users_controller,那是不同的视图

    class UserController < ApplicationController
  prepend_view_path(File.join(Rails.root, 'app/views/user/'))


  layout 'application'


  def index

  end

  def billing
  end

  def plan
  end

  def profile
    @user = User.find(params[:id])
    @user_posts = @user.posts.order('created_at DESC')
  end
end

def download
  @user = User.find(params[:id])

  respond_to do |format|
    format.html
    format.csv { send_data @user.csv, filename: "userinfo-#{Date.today}.csv" }
  end
end

  def support
    @user = User.find(params[:id])
    @user_posts = @user.posts.order('created_at DESC')
  end

  def notifications
  end

  def show
    @user = User.find(params[:id])
    @posts = current_user.posts.order('created_at DESC')
    @user_posts = @user.posts.order('created_at DESC')
  end

更新:根据建议,我尝试在user_controller中实施新的下载操作,并尝试在我的视图中使用以下代码:

<p><%= link_to("Export data as CSV", download_path(@user, format: :csv), { :controller => :user, :action => :download }, class: "btn btn-success") %></p> 

但这会引发以下错误:

“接收未知”操作-找不到UserController的“下载”操作

routes.rb

        Rails.application.routes.draw do

  get 'legal/privacy'

  get :datenschutz, to: 'legal#terms_of_service'


  devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'registrieren', edit: 'bearbeiten' }, :controllers => { registrations: 'registrations' }

  get '/users/mitteilungen/:id' => 'user#notifications'
  get '/users/:id/artikel/', :to => 'user#support', :as => :artikel
  get '/users/plan' => 'user#plan'
  get '/users/billing' => 'user#billing'
  get '/users/:id', :to => 'user#profile', :as => :user
  get 'download', :to => 'user#download', :controller => :user, action: :download, :as => :download


  resources :posts, path: :news do
    resources :comments, path: :kommentare do
    end
  end


  devise_scope :user do
    resources :posts, path: :news do
      resources :comments do

      end
    end
  end
  root to: "posts#index", as: :root

end

2 个答案:

答案 0 :(得分:0)

您的条件不正确@user.id = user.id

user@user。我认为您的意思是user.id = record.id

record是您正在授权的实例,例如:

def show
  @user = User.find(params[:id])
  authorize @user
  ...
end

authorize @user基本上是UserPolicy.new(@user, current_user).show?

的快捷方式

顺便说一句,我不确定,但是我认为您可以安全地省略.iduser == record

UPD。 我重新阅读了您的政策。 ApplicationPolicy通常如下所示:

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end
end

您覆盖了构造函数。为什么?让它保持Pundit希望的样子

UPD2。我希望您的UserPolicy如下所示:

class UserPolicy < ApplicationPolicy
  def profile? #this is the action I have the button in to export data
    return true if user_or_admin?
  end

  private

  def user_or_admin?
    user.id == record.id || user.admin?
  end
end

答案 1 :(得分:0)

我们对我的原始答案的评论讨论得太多了。我想澄清我的观点。

您写道:

  

我将pundit用作我的授权gem,我希望用户X只能下载属于用户X的用户数据。现在我以http://localhost:3000/download/x.csv作为链接来接收用户数据。但是,如果我登录到另一个用户,例如user /:id 2,我仍然可以输入URL并下载users / 3数据。

所以问题在于授权不起作用。我说的对吗?

之后,您写了:

  

更新:按照建议,我尝试在自己的手机中实施新的下载操作   user_controller并尝试在我看来使用此代码:   ...

     

但这会引发以下错误:   ...

因此,路由问题不在原始问题范围内,应在其他问题中解决。无论如何,您都可以签出this question

所以。如果您的问题是授权-只需正确编写策略,然后在控制器中使用它们即可。这就是全部。