【问题标题】:How to list only current user orders if not an admin (Ruby on Rails)?如果不是管理员(Ruby on Rails),如何仅列出当前用户订单?
【发布时间】:2013-11-10 10:19:56
【问题描述】:

我对 Rails 很陌生,而且我刚刚开发我的第一个 Rails 应用程序,所以这对某些人来说可能是一个愚蠢的问题。如果他们不是管理员,我想让 current_user 只看到他们自己的订单。我能够设置只有管理员可以查看所有订单,但我很难让当前用户只能查看、列出和删除他们自己的订单。我的应用程序有一个 :orders 模型 belongs_to :users 和一个 :users 模型 has_many :orders。

这就是我的 orders_controller.rb 的样子:

class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy]

  # GET /orders
  # GET /orders.json
  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @orders = Order.all
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end

  # GET /orders/new
  def new
    @order = Order.new
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render action: 'show', status: :created, location: @order }
      else
        format.html { render action: 'new' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /orders/1
  # PATCH/PUT /orders/1.json
  def update
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    @order.destroy
    respond_to do |format|
      format.html { redirect_to orders_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:user_id, :drop_address)
    end
end

我的问题是如何只允许当前用户列出并仅查看他们下的所有订单?

谢谢

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-4 cancan


    【解决方案1】:

    有一个名为cancan 的宝石供您使用。 请阅读wiki page。 需要更多帮助?让我知道:)

    定义能力

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new # guest user (not logged in)
        if user.admin?
          can :manage, :all
        else
          can :read, Order, :user_id => user.id
        end
      end
    end
    

    来自accessible_by的控制器查询

    @orders = Order.accessible_by(current_ability)
    

    【讨论】:

    • 谢谢,我已经安装了 cancan,这是一种更简单的方法。这实际上解决了我问题的一部分。我只能让当前用户看到他的订单,但他仍然可以通过直接访问其他用户的订单(例如 http://..../orders/2)来单独访问某人的订单。我该如何解决这个问题?
    • 我认为你应该把来自cancanload_and_authorize_resource的魔法方法放在你的控制器中。
    • @gotva 太棒了,再简单不过了!谢谢
    • @gotva 实际上,虽然设置load_and_authorize_resource 阻止非管理员用户访问其他人的订单,但它也阻止他们创建/编辑/删除自己的订单。有什么建议吗?
    • load_and_authorize_resource 方法对能力不利。您可以根据需要定义能力。
    【解决方案2】:
    1. 你必须在两个层面上做到这一点。在索引中,您必须为当前用户获取订单,以便用户只能看到他的订单。第二级是您确保用户可以输入不属于他的订单网址,因此请在其他操作(编辑、更新、删除、显示)中检查。

    2. 或者您可以使用声明性授权 gem。很有帮助https://github.com/stffn/declarative_authorization

    -提示:为了命名约定,将belongs_to :users 更改为belongs_to :user (belongs_to 总是单数)

    你的控制器应该是这样的

    #this is the filter called before each method to make sure the user is authorized to access the order
    before_filter :authorize_user, :only => [:edit,:update,:delete,:show]
    
    def index
      authorize! :index, @user, :message => 'Not authorized as an administrator.'
      #here fetch the orders of the current user only
      @orders = current_user.orders
    end
    
    #and then goes all your methods here as normal 
    
    private 
    
    def authorize_user
      unless current_user.eql?(@order.user)
        flash[:notice]="You are not authorized to access this order"
        redirect_to orders_path 
      end
    end
    

    【讨论】:

    • 由于某种原因,当我尝试访问 http://.../orders/ 时,我收到一个错误,因为我不是管理员,当我尝试访问 http://.../orders /1 我得到他的错误:未定义的方法user' for nil:NilClass from def authorize_user 除非 current_user.eql?(@order.user)`。有什么想法吗?
    • 尝试在一次调用 before_action 中实现:set_order,:authorize_user, :only => [:edit,:update,:destroy,:show]
    • 不,很遗憾没有这样做。
    • ok in set_order 方法在找到订单后放入该行,看看你得到什么 -raise @order.inspect
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多