简体   繁体   English

Rails应用程序中CRUD项目/微博的所有权

[英]CRUD Ownership of Items/Microposts in Rails App

I have a simple rails app where users can create 'items', but on the master index page that lists all the items, there are 'Show, Edit, and Delete' links next to each 'item'. 我有一个简单的rails应用程序,用户可以在其中创建“项目”,但在列出所有项目的主索引页面上,每个“项目”旁边都有“显示,编辑和删除”链接。 I understand that this is due to the fact that I used scaffolding to accomplish the items, but I'd like to make sure that people can only edit the ones that they created. 我知道这是因为我使用脚手架来完成这些项目,但我想确保人们只能编辑他们创建的项目。 This logic is a little above my head at the moment, as, like I've said before, am totally new to rails. 这个逻辑现在略高于我的头脑,就像我之前说过的那样,对于铁轨来说我是全新的。

User Controller: 用户控制器:

class UsersController < ApplicationController
  def show
    @user = User.find_by_username(params[:id])
  end
  def index
    @user = User.find(:all)
  end
end

Master Item View: 主项目视图:

<div class="well">
  <h1>All Items</h1>
    <table>
  <tr>
    <th>Title</th>
    <th>Details</th>
    <th>Inquire</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
<% @items.each do |item| %>
  <tr>
    <td><%= link_to item.title, item_path(item) %></td>
    <td><%= item.content %></td>
    <td><%= mail_to item.email, "Inquire", :cc => "michaelomchenry@gmail.com",
                 :subject => "OverFlow Inquiry Regarding " + item.title %></td>
    <td><%= link_to 'Show', item %></td>
    <td><%= link_to 'Edit', edit_item_path(item) %></td>
    <td><%= link_to 'Destroy', item, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>
  </table>

<br />
<%= link_to 'New Item', new_item_path %>
</div>

Item Model: 商品型号:

class Item < ActiveRecord::Base
  attr_accessible :content, :user_id, :title
  validates :content, :length => { :maximum => 140 }
  belongs_to :user
  delegate :email, to: :user
end

There is a lot of stuff there. 那里有很多东西。 First of all, to be able to filter the items (or the actions) based on the user, you need to know who is logged at the time, and so enable users to login. 首先,为了能够根据用户过滤项目(或操作),您需要知道当时登录的人员,从而使用户能够登录。 This could be done for example using Devise , an authorization gem for Rails. 这可以通过例如使用Devise (Rails的授权gem)来完成。 Devise is largely used and well documented. Devise在很大程度上被使用并且记录良好

Once Devise is setup, you can check whether the user is logged (for example using a before_filter ), and Devise will create a "current_user" variable for you to use (this is shown in Devise tutorial ). 设置Devise后,您可以检查用户是否已记录(例如使用before_filter ),Devise将创建一个“current_user”变量供您使用(这在Devise教程中显示 )。 You can then use it to filter your item lists with something like : 然后,您可以使用它来过滤项目列表,例如:

editable_items = current_user.items

And then use the editable_items on your view. 然后在视图中使用editable_items。 I would advise you to go read the Devise tutorial as what you are doing is a quite common and well documented task. 我建议你去阅读Devise教程,因为你正在做的是一个非常常见且记录良好的任务。

I would comment if I could, but I feel that this must be said in reference to the answer posted by @momchenr (answering their own question) as a follow up to the chosen answer. 如果可以的话,我会发表评论,但我觉得必须参考@momchenr发表的答案(回答他们自己的问题)作为所选答案的后续行动。

@momcher wrote: @momcher写道:

I ended up doing this: 我最终这样做了:

 <% if item.email == current_user.email %> 

and it worked... is that ok? 它工作......那可以吗?

Probably not. 可能不是。 But that depends on how your system is set up. 但这取决于系统的设置方式。 If users can edit their email addresses and/or email addresses aren't forced to be unique, they may be able to gain editing access to another user's "items" by changing their email address temporarily or just signing up as a new user with a known user's email address. 如果用户可以编辑他们的电子邮件地址和/或电子邮件地址不是唯一的,他们可以通过临时更改他们的电子邮件地址或仅注册为新用户来获得对其他用户的“项目”的编辑访问权限。已知用户的电子邮件地址。

Even if you never display email addresses of users in your application, there is an inherent vulnerability when you leave that much of the authentication process in a user-provided field. 即使您从未在应用程序中显示用户的电子邮件地址,当您将大部分身份验证过程留在用户提供的字段中时,也存在固有的漏洞。

Not knowing exactly how things are set up in Devise—only based on the info you have provided—I'd try the following: 根据您提供的信息,不确切知道如何在Devise中设置内容 - 我会尝试以下方法:

These two might be are a touch slower depending on the state of ActiveRecord when called 根据调用时ActiveRecord的状态,这两个可能会慢一些

  • <% if item.user == current_user %>
  • <% if item.user.id == current_user.id %>

This one ought to be a touch faster since you are not getting the user object from the item object (you are just pulling directly from the user_id method of the user object) 这个应该更快,因为你没有从item对象获取user对象(你只是直接从user对象的user_id方法拉)

  • <% if item.user_id == current_user.id %>

No matter if I am right or wrong in my guesses at speed, this is generally a better solution than the one you said works for you. 无论我在速度上的猜测是对还是错,这通常都是比你所说的更好的解决方案。 Since the user's ID is never under their control directly—unless there are major holes in your code—they cannot easily pose as other users. 由于用户的ID永远不会被他们直接控制 - 除非您的代码中存在重大漏洞 - 他们不能轻易地像其他用户那样构成。

I ended up doing this: 我最终这样做了:

<% if item.email == current_user.email %>

and it worked... is that ok? 它工作......那可以吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM