【问题标题】:How do I stop users from editing other user posts in Omniauth (rails)如何阻止用户在 Omniauth (rails) 中编辑其他用户帖子
【发布时间】:2013-07-25 16:04:03
【问题描述】:

我有一个人们可以写帖子的网络,他们有自己帖子的个人信息流,以及每个人的网络信息流。

问题是,任何经过全域认证的用户都可以通过在 URL 上输入 /edit 或 delete 来编辑另一个用户。

不能将这个用于实时网站!

有没有人能快速回答如何阻止这个问题?我有:

before_filter :authenticate_user!, :except => [:index, :show]

但我无法确定如何将编辑锁定到创建帖子的用户。

模型-user.rb

class User
  has_many :posts
end

模型 - post.rb

class Post
  belongs_to :user
end

如果您想了解更多信息,请告诉我 - 感谢您的帮助! -D

【问题讨论】:

    标签: ruby-on-rails ruby authentication omniauth posts


    【解决方案1】:

    如果您在没有 CanCan Gem 的情况下从头开发应用程序,请使用此方法

    def logged_in_user
       unless current_user?(@model.user)
         flash[:danger] = "You do not have authorization to edit this post" #if not logged in as the owner of the post
         redirect_to login_url
       end
      end
    

    并确保您已设置好之前的操作

    before_action :logged_in_user,仅:[:edit, :update, :delete]

    【讨论】:

      【解决方案2】:

      如果我没看错的话,您的用户是通过 OmniAuth 进行身份验证的,但现在您正在寻找一种方法来根据权限或所有权为特定资源/操作“授权”他们。

      为用户、组、角色和中间人授权资源/操作的最受欢迎的 gem 是 CanCan。

      Can, can you do the CanCan?

      还有一个railscast video帮助我了解gem在我的应用程序中的应用。

      一旦您知道如何在 CanCan 中编写能力,take a look here 就可以根据父用户解决您关于帖子编辑的原始问题。

      【讨论】:

      • 感谢科尔森,他的脑洞大开。谢谢你让我重新上路。干杯
      • 不用担心。它发生在我们所有人身上。 ^_^
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多