Ruby on Rails 2.3.8:有没有办法使每个控制器的销毁操作仅在满足条件时发生?

发布于 2024-11-19 18:15:50 字数 181 浏览 1 评论 0原文

目前,在我的应用程序中,我发现如果您拦截要执行删除命令的数据包,您可以将 ID 更改为任何 id,并且该对象将被删除,无论它是否属于创建它的人或不是。

所以,我的问题是,有没有办法以某种方式对销毁/删除的工作方式进行全局修改,以便 current_user 必须拥有要删除的项目(或任何其他条件,因为许多应用程序比简单的用户所有权)

Currently, in my app, I've discovered that if you intercept a packet about to perform a delete command, you can change the ID to any id, and that object will get deleted, regardless if it belong to the person who made it or not.

So, my question is, is there a way to somehow make a global modification to the way destroy / delete works such that the current_user must own the item about to be deleted (or whatever other condition, as many apps are a bit more complicated than simple user ownership)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2024-11-26 18:15:50

为销毁行动制定全球规则并不是一个好主意。最简单的方法是,您只需要检查控制器中的访问权限:

class MyController < ApplicationController
  before_filter :access, :only => [:edit, :update, :destroy]

  ...

  private
  def access
    my_object = MyObject.find(params[:id])
    unless logged_in? && my_object.user == current_user
      render :template => "/error/401.html.erb", :status => 401
    end
  end
end

您还应该查看 CanCan gem

It is not a good idea to write global rule for destroy action. In the simplest way you just need to check access in your controllers:

class MyController < ApplicationController
  before_filter :access, :only => [:edit, :update, :destroy]

  ...

  private
  def access
    my_object = MyObject.find(params[:id])
    unless logged_in? && my_object.user == current_user
      render :template => "/error/401.html.erb", :status => 401
    end
  end
end

Also you should look into CanCan gem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文