2011-07-26 8 views
1

ロールベースのアクセス許可を使用し、テーブルの各行がエンティティを表しているとします。 Productsテーブル、各行はProductエンティティを表します。ロールに基づくエンティティの行レベルのアクセス許可

ロールに基づいて製品レベルのセキュリティを提供する方法を教えてください。

Salesグループは、IDが1,234,432,532,34の製品にアクセスすることができます。

複数のロールには任意の製品に対する権限を与えることができます。

目標は、のようなクエリのeffecientデータベース呼び出しを提供することである:

多対多の関係は、別のテーブルに格納されている
var products = ProductDao.GetProductsByRole(234); // roleID = 234 

答えて

2

create table Products( 
    ProductId int not null identity (1,1), 
    Name nvarchar(256) not null, 
    Description nvarchar(max), 
    constraint PK_Products primary key (ProductId), 
    constraint UNQ_Products_Name unique (Name)); 

create table Roles(
    RoleId int not null identity (1,1), 
    Name nvarchar(256) not null, 
    Description nvarchar(max), 
    constraint PK_Roles primary key (RoleId), 
    constraint UNQ_Roles_Name unique (Name)); 
go 

create table ProductRolePermissions (
    ProductId int not null, 
    RoleId int not null, 
    constraint FK_ProductRolePermissions_Products 
     foreign key (ProductId) 
     references Products(ProductId), 
    constraint FK_ProductRolePermissions_roles 
     foreign key (RoleId) 
     references Roles(RoleId)); 
go 

create unique clustered index CDX_ProductRolePermissions 
    on ProductRolePermissions (RoleId, ProductId); 
create unique nonclustered index NDX_ProductRolePermissions 
    on ProductRolePermissions (ProductId, RoleId); 
go 


create function dbo.GetProductsByRole(@roleId int) 
returns table 
with schemabinding 
as return (
    select ProductId 
    from dbo.ProductRolePermissions 
    where RoleId = @roleId); 
go 


insert into Products (Name) 
     values ('P1'), ('P2'), ('P3'), ('P4'); 
insert into Roles (Name) 
     values ('G1'), ('G2'); 
insert into ProductRolePermissions (ProductId, RoleId) 
     values (1,1), (3,1), (2,2), (3,2); 
go 

select 'Products permitted for G1', p.* 
from dbo.GetProductsByRole(1) r 
join Products p on r.ProductId = p.ProductId; 

select 'Products permitted for G2', p.* 
from dbo.GetProductsByRole(2) r 
join Products p on r.ProductId = p.ProductId; 

物事は少し多くを得ます複数のロールメンバーシップを持つ読み取り/書き込み/フルアクセスの古典的な許可/拒否/取り消しアクセス許可モデルに従う場合は、複雑です。

+0

あなたは、テーブルのための最良の解決策は100000行などのより多くの行を持っていると思いますか?私はあなたのソリューションを実装する場合は、より多くのパフォーマンスとユーザー管理の問題が発生すると思う。あなたは私の問題を考えていますか? tnx / – Alex

関連する問題