当用户只能访问父对象时,如何构建子对象的安全性

发布于 2024-12-14 07:23:11 字数 622 浏览 2 评论 0原文

我有一个类似于以下内容的数据库结构:

User
----
Id
Name

UserCustomerLink
----------------
UserId
CustomerId

Customer
--------
Id
Name

Address
-------
Id
CustomerId
Address1

Invoice
-------
Id
AddressId
Number

这是一个 asp.net mvc 站点,因此用户可以访问像 http://localhost/invoice/details/1 这样的地址,该地址将返回发票id 为 1。

用户需要登录并分配给发票(通过地址)所属的客户(通过 UserCustomerLink 表)。

我的问题是我应该在哪里检查用户是否可以查看发票?

我应该检查发票是否存在并且用户可以查看它然后从数据库返回它,或者我应该从数据库检索该项目然后检查?

我担心为查找此信息而执行的数据库查询的数量,并且我正在寻找一种有效的方法。这是结构的简单视图,一些子属性的深度超过 3。

在站点注释中,我首先使用实体​​框架代码,因此如果有一种方法可以创建在发票对象中包含客户 ID 的映射,则可能可以解决此问题。

I have a database structure something similar to the following:

User
----
Id
Name

UserCustomerLink
----------------
UserId
CustomerId

Customer
--------
Id
Name

Address
-------
Id
CustomerId
Address1

Invoice
-------
Id
AddressId
Number

This a asp.net mvc site so a user can go to an address like http://localhost/invoice/details/1 which will return the invoice with an id of 1.

A user needs to be logged in and be assigned to the customer (via the UserCustomerLink table) that the invoice (via the address) belongs to.

My question is where should I be doing the check that the user can view the invoice?

Should I check the invoice exists and the user can view it and then return it from the database or should I retrieve the item from the database and then check?

I'm concerned about the number of database queries that will be executed to find this information and I'm looking for an efficient method. This a simplistic view of the structure and some of the child properties go more than 3 deep.

On a site note I'm using Entity Framework code first so if there's a way to create a mapping that includes the Customer Id in the Invoice object this could potential get around this issue.

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

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

发布评论

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

评论(2

话少心凉 2024-12-21 07:23:11

没有理由两次访问数据库。您可以使用联接并进行一项选择。您的所有表都有链接记录的外键。当查询返回时,您检查用户是否链接到该发票。你可以使用 LINQ 做同样的事情

SELECT 
    i.*,
    c.Name
FROM 
    INVOICE i JOIN 
    Address a JOIN
        on i.addressId = a.id
    Customer c JOIN
        on a.customerID = c.id
    UserCustomerLink ucl join
        on c.id = ucl.CustomerId
    User u
        on ucl.Userid = User.id
WHERE 
    i.Id = @invoiceId

There is no reason to make two trips to the database. You can use joins and do one select. All your tables have foreign keys that link the records. When the query returns you check that the User is linked to that invoice. You could do this same thing with LINQ

SELECT 
    i.*,
    c.Name
FROM 
    INVOICE i JOIN 
    Address a JOIN
        on i.addressId = a.id
    Customer c JOIN
        on a.customerID = c.id
    UserCustomerLink ucl join
        on c.id = ucl.CustomerId
    User u
        on ucl.Userid = User.id
WHERE 
    i.Id = @invoiceId
差↓一点笑了 2024-12-21 07:23:11

查看授权属性。您可以将您的用户放入角色,然后让框架为您进行检查。

[Authorize(Roles="AllowedUser")]
public ViewResult GetInvoies()
{
   return View();
}

Look into the Authorize attribute. You could put your users into Roles and then let the framework check for you.

[Authorize(Roles="AllowedUser")]
public ViewResult GetInvoies()
{
   return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文