【问题标题】:Approach to limit the visibility of data限制数据可见性的方法
【发布时间】:2009-12-23 20:24:15
【问题描述】:

好的,假设有这个数据库架构(关系):

|User    | (1-->n) |Customer | (1-->n) |Car  | (1-->n) |Support    |
|--------|         |---------|         |-----|         |-----------|
|id      |         | user_id |         |Brand|         |Description|
|username|         |lastname |         |PS   |         |Cost       |
|password|         |firstname|         |seats|         |hours      |
|...     |         |..       |         |...  |         |...        |

表 User 由 Authlogic 生成。

我有 2 个注册用户,每个用户都有他的客户,等等。。使用 Authlogic,我可以只允许经过身份验证的用户访问控制器/视图。没关系,这就是 Authlogic 的用途。

现在我需要确保用户#1 永远不会获得属于用户#2 客户的信息。

换句话说: 如果用户#1 转到http://myapp.com/cars,他将看到属于用户#1 客户的汽车列表

如果 id=131 的汽车属于用户#1 的客户,则只有用户#1 必须能够访问此信息 (http://myapp.com/car/1)。如果用户#2 在浏览器中插入相同的链接,他不必能够看到此信息。

有人建议我在用户和每个 db 表之间创建关系,以检查记录是否与 current_user 关联。

你怎么看?最好的方法/解决方案是什么?

【问题讨论】:

    标签: ruby-on-rails authentication authorization authlogic denormalization


    【解决方案1】:

    所以你有两件事:

    1. 在汽车控制器的索引页面中,仅应显示属于当前用户的汽车。
    2. 您希望将节目页面限制为所有者。

    至于索引,我建议如下:

    def index
      # find logic
      @cars = Car.find(:all,:conditions=>{:customer_id => current_user.customers.collect(&:id)})
      # whatever else
      # ...
    end
    

    还有展示页面:

    def show
      # .....
      # after the find logic
      redirect_to :index unless current_user.customers.collect(&:id).include? @car.customer_id 
      # whatever else
      # ...
    end
    

    这种方法在大多数情况下都可以,但是更好的性能方法是在客户表中添加一个 user_id 列,这称为非规范化,但在性能方面它是可以接受的。

    【讨论】:

    • 谢谢你。看起来数据库非规范化是要遵循的方法。所以我需要在用户和每个表之间添加一个 1->n 关系(在这种情况下,在汽车和支持中也是如此)。
    猜你喜欢
    • 2014-08-21
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    相关资源
    最近更新 更多