Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 阻止用户访问其他用户帐户_Ruby On Rails 3_Authentication - Fatal编程技术网

Ruby on rails 3 阻止用户访问其他用户帐户

Ruby on rails 3 阻止用户访问其他用户帐户,ruby-on-rails-3,authentication,Ruby On Rails 3,Authentication,我有一个一般性的问题 我已经使用Michael Hartl的教程示例构建了我的用户模型。我所遇到的问题是url的改变会给其他用户带来信息。我使用的是Friendly_id,它从id->name更改,但是如果我将顶部的名称更改为其他用户,它将显示他们的主页。我想限制我的用户这样做 如果没有DEVICE或CanCan等的实施,这是可能的吗?尝试抛出一个验证,检查用户是否试图更改url栏上的名称,即检查名称是否为当前登录的用户,如果不是,则重定向回当前用户 如果您需要型号信息,请告诉我 TIA您可以在

我有一个一般性的问题

我已经使用Michael Hartl的教程示例构建了我的用户模型。我所遇到的问题是url的改变会给其他用户带来信息。我使用的是Friendly_id,它从id->name更改,但是如果我将顶部的名称更改为其他用户,它将显示他们的主页。我想限制我的用户这样做

如果没有DEVICE或CanCan等的实施,这是可能的吗?尝试抛出一个验证,检查用户是否试图更改url栏上的名称,即检查名称是否为当前登录的用户,如果不是,则重定向回当前用户

如果您需要型号信息,请告诉我


TIA

您可以在
用户控制器
上的过滤器前放置一个
,以验证此场景,如下所示:

# Each time you make an action on your UsersController this filter will run
before_filter :validate_url_hack

def validate_url_hack
  # Check the params hash to see if the passed :id matches the current user's id
  # (note the .to_i on params[:id], as you are comparing against a Fixnum)
  unless params[:id].to_i == current_user.id
    # This line redirects the user to the previous action
    redirect_to request.referer
  end
end

这只是一个猜测,但我相信类似的东西应该会起作用。

您可以在
用户控制器的
过滤器之前放置
,以验证此场景,如下所示:

# Each time you make an action on your UsersController this filter will run
before_filter :validate_url_hack

def validate_url_hack
  # Check the params hash to see if the passed :id matches the current user's id
  # (note the .to_i on params[:id], as you are comparing against a Fixnum)
  unless params[:id].to_i == current_user.id
    # This line redirects the user to the previous action
    redirect_to request.referer
  end
end

这只是一个猜测,但我相信类似的东西应该会起作用。

您希望限制为特定用户的所有模型都应该与您的用户模型有一个“属于”关联,这样在您的控制器中就可以了

current_user.cars

而且您永远不应该在URL中传递用户id。这假设您正在使用Desive。

您希望限制为特定用户的所有模型都应该与您的用户模型有一个“归属”关联,以便在您的控制器中执行此操作

current_user.cars

而且您永远不应该在URL中传递用户id。这假设您正在使用Deviate。

更好的是,设置您的关系,这样您就可以像这样定义调用的范围:
@account=current\u user.accounts.find(params[:id])
so(对不起,我是新来的)…创建一个帐户模型,应用于用户模型有一个:account和def显示为@account=current\u user.accounts.find(params[:id])?我的当前设置验证用户是否为管理员…然后它将呈现他们的“显示”页面。但若我转到url并更改名称,它也会呈现用户“显示”页面。用户不能访问除“显示”页面以外的任何内容。(如果这有意义的话)更好的是,设置您的关系,这样您就可以像这样定义调用的范围:
@account=current\u user.accounts.find(params[:id])
so(对不起,我是新来的)…创建一个帐户模型,apply-in-user模型有一个:account和def显示为@account=current\u user.accounts.find(params[:id])?我的当前设置验证用户是否为管理员…然后它将呈现他们的“显示”页面。但若我转到url并更改名称,它也会呈现用户“显示”页面。用户不能访问除“显示”页面以外的任何内容。(如果有意义的话)不使用Desive或任何“登录”gem。不使用Desive或任何“登录”gem。