MVC 3 プロジェクトのリソースに対してユーザーを承認する必要があります。次のようにするのは良い習慣ですか?
interface IResource
{
string Id { get; set; }
bool Authorize(User user);
}
class User
{
// some logic for user
}
class ResourceA : IResource
{
public string Id { get; set; }
public ResourceA(string id)
{
Id = id;
}
bool Authorize(User user)
{
// Query database to check if user has permission to the resource
// return true if the user has, otherwise false
}
}
class TestController : Controller
{
// Get details of resource A
public ActionResult Get(User user, string resourceId)
{
IResource resource = new ResourceA(resourceId);
if (resource.Authorize(user))
{
// do stuff
}
else
{
throw HttpException(403, "No permission");
}
}
}
上記の実装では:
Web サイトのすべてのリソースは、強制的に IResource インターフェイスを実装します。
リソースと対話するすべてのリクエストは、承認コードを実装する必要があります。
どんな助けでも大歓迎です。