English 中文(简体)
铁路:核查多个控制器的正确用户
原标题:Rails: Verify correct user across multiple controllers

我有几名控制人员,他们需要一个正确的用户来开展编辑/更新/编辑工作。 实现以下目标所需要的铁路:

目前,每名控制员中都有 我有以下法典:

class FooController < ApplicationController
  before_filter :correct_user, :only => [:edit, :update, :destroy]

  # normal controller code

  private

  def correct_user
    @foo = Foo.find params[:id]
    redirect_to some_path unless current_user == @foo.user
  end
end

我在3名控制人员中有类似的守则。 我开始向这样的帮助者伸出援手:

module ApplicationHelper
  def correct_user( object, path )
    if object.respond_to? :user
      redirect_to path unless object.user == current_user
  end
end

但我很想知道,这是否是这样做的好办法。 解决这一问题的公认方式是什么?

Thank you

http://www.ohchr.org。

这里正确的用户检查是因为我只想确保它能够向每个物体 make/del。

为了澄清,目标就象问题和员额。 我不想使用像Can这样的东西,因为它为像这种情况这样简单的东西夸大了。

最佳回答

I really like using RyanB s CanCan, which allows you to both restrict access to actions based on the user, and centralize such authorization into basically a single file.

EDIT

No problem. I hear you on CanCan - it takes a little while to get up and running on it, but it s designed to do exactly what you re asking - per object authorization.

Alternative: Another way to do this is move your authoriship/current_user check to the ApplicationController class, from which all of your other Controllers inherit (so they will get that code through inheritance - and you don t need to write the same code in multiple Controllers), and it would look something like...

class ApplicationController < ActionController::Base
  ...

  helper_method :correct_user

  private
    def correct_user( object, path )
      redirect_to path unless object.user == current_user
    end

end
问题回答

您应:

def edit
  @foo = current_user.foos.find(params[:id])
end

这样,只有当目前的用户是<代码>的所有人时,才会如此。 Foo 他将能够看到。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签