ASP.NET MVC の AuthorizeAttribute を拡張して、ロール メンバーシップまたは問題のデータの「所有権」に基づくユーザーの承認の概念をサポートするようにしたいと思います。データ アクセスには LINQ2SQL を使用しています。asp.net mvc authentication using rolesにも同様の質問があります。
私が考えているのは、拡張された AuthorizeAttribute クラスに EntityProperty、UserProperty、RouteParameter、および JoinTableType パラメーターを追加することです。最初の 2 つは、チェックする結合テーブル内のプロパティの名前です。RouteParameter は、一致する EntityProperty の値を抽出するルート パラメーターの名前になります。現在のユーザー名を使用して、ユーザーのテーブルからユーザー ID を取得します。JoinTableType パラメーターは、ルート パラメーター値とユーザー ID が一致する必要がある Entity と UserProperties を含むデータ コンテキスト内のテーブルの型になります。
基本的な考え方は、擬似コードで次のとおりです。
if authorizecore result is true
user is granted access based on role
else if user is not authenticated
redirect to logon
else if user is related to request
user is granted access based on relation
else
user is not authorized, redirect to not authorized error view
is related テストは次のようになります。
result = false
find the matching user from user name
find the entity property value in route data
if user exists and entity property value exists
get table from context matching join table type
if table exists
find row in table matching user id and entity property value
if row exists
result = true
endif
endif
endif
return result
私の質問は、LINQ クエリを作成する際に型とプロパティの名前をどのように使用すればよいかということです。それとも、これをすべてobject
リフレクションで行う必要がありますか。これを簡単にする方法についてのアイデアを本当に探しているので、他の提案もいただければ幸いです。チェックをアクションに直接埋め込むよりも、属性を使用して、他のアクションの処理方法と一貫性を保つことをお勧めします。