我々が追加するために必要な唯一のものアクセス制御フィルタリングは、追加の述語を追加します。たとえば、
public override IQueryable<Patient> Get()
{
var qry = base.Get();
return qry.Where(itm => itm.Name.FirstName.Contains("R"));
}
コントローラインフラストラクチャがのIQueryable <T>を列挙するときには、作業ユニット全体を評価し、最終的なクエリを生成します。
私はデータベースに対してトレースを実行し、それを
http://localhost/MyService/Patients?$filter=Surgeries/any(d:d/PreOpDataComplete%20eq%20true).
(PreOpDataCompleteフラグが真であるところ言い換えれば手術を見つける。)のような、より複雑なのODataクエリを与えると、私は次のクエリが実行されて表示されます。
exec sp_executesql N'SELECT
[Project1].[C1] AS [C1],
[Project1].[Id] AS [Id],
[Project1].[Name_FirstName] AS [Name_FirstName],
[Project1].[Name_LastName] AS [Name_LastName],
[Project1].[Gender] AS [Gender],
[Project1].[BirthDate] AS [BirthDate],
[Project1].[MedicalRecordId] AS [MedicalRecordId],
[Project1].[Surgeon_Id] AS [Surgeon_Id]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name_FirstName] AS [Name_FirstName],
[Extent1].[Name_LastName] AS [Name_LastName],
[Extent1].[Gender] AS [Gender],
[Extent1].[BirthDate] AS [BirthDate],
[Extent1].[MedicalRecordId] AS [MedicalRecordId],
[Extent1].[Surgeon_Id] AS [Surgeon_Id],
1 AS [C1]
FROM [dbo].[Patient] AS [Extent1]
WHERE [Extent1].[Name_FirstName] LIKE N''%R%''
) AS [Project1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Surgery] AS [Extent2]
WHERE ([Project1].[Id] = [Extent2].[Patient_Id]) AND ([Extent2].[PreOpDataComplete] = @p__linq__0)
)',N'@p__linq__0 bit',@p__linq__0=1
そして、私は外科医が自分の患者を所有し、単純なセキュリティモデルを仮定し、私は私が許されてる外科医IDのリストを持っている場合は、私が期待した結果
{
"@odata.context":"http://localhost/MyService/$metadata#Patients",
"value":[{
"Name":{
"FirstName":"Ronn",
"LastName":"Black"
},
"Gender":"M",
"BirthDate":"1917-02-02T00:00:00-08:00",
"MedicalRecordId":"MRN 0001",
"Id":"8bf6dcc4-3f00-4a40-980c-ceb13f8f5360"
}]
}
を取得請求でのアクセス。
public partial class Surgeon : IBaseEntity
{
[Key]
public Guid Id { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
. . .
}
私は、以下の変更を行う場合は今、私は私が見ることが許されてるだけの患者に任意の検索を制限することができます:
public override IQueryable<Patient> Get()
{
//IQueriable from the OData Selection
var qry = base.Get();
//Enforce Access Security
var accessList = {get list of authorized surgeon id's from claims};
var finalQry = uow.Surgeons
.Where(s => accessList.Contains(s.Id)) //Restrict to Surgeons I'm allowed to see
.SelectMany(surgeon => surgeon.Patients) //All the patients I'm allowed to see (Left)
.Join(qry, //Join to Query (Right)
allowedPatients => allowedPatients.Id, //Key for Left
qryPatients => qryPatients.Id, //Key for Right
(allowedPatients, qryPatients) => //Iterate through Matches
qryPatients); //Return the Matches from Right
return finalQry;
}
私はこのようになります外科医のエンティティを持っているでしょう