1

DBには次の構造があります。

DomainEntities:
 +EntityID
 +Name
 +ParentID
 +...

Users:
 +UserID
 +Username
 +...

Roles:
 +RoleID
 +Name

UserRolesAssociation:
 +RoleID
 +UserID
 +EntityID

そのため、MVCの組み込みの承認属性を使用して、さまざまなメンバーによって作成されたコントローラーのアクションをフィルター処理したいと思います。

user1がentity1またはその下のエンティティに対して削除アクションを実行した場合に言えることは、彼がそれを実行する適切な役割を持っているかどうかを確認し、それに応じてアクションをフィルタリングできることです。

そのトピックに取り組むためのベストプラクティスは何でしょうか?必要な答えを提供する独自の権限エンジンを作成する必要がありますか、それとも既存の機能を使用できますか?

4

1 に答える 1

2

そのトピックに取り組むためのベストプラクティスは何でしょうか?

カスタム[Authorize]は、このロジックを実装するのに適した場所のようです。

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var authorized = base.AuthorizeCore(httpContext);
        if (!authorized)
        {
            // the use ris not authenticated or not authorized - no need to continue
            return false;
        }

        string username = httpContext.User.Identity.Name;
        // read the entity id that this user is attempting to manipulate
        string entityId = (string)httpContext.Request.RequestContext.RouteData.Values["id"] ?? httpContext.Request["id"];

        return IsAllowed(username, entityId);
    }

    private bool IsAllowed(string username, string entityId)
    {
        // You know what to do here - hit the database and check whether
        // the current user is the owner of the entity
        throw new NotImplementedException();
    }
}

その後:

[HttpDelete]
[MyAuthorize]
public ActionResult Delete(int id)
{
    ...
}
于 2012-09-22T08:54:49.693 に答える