簡體   English   中英

在存儲庫模式.Net中檢查CRUD操作的權限

[英]Checking the rights for CRUD operations in repository pattern, .Net

大家下午好! 我正在使用存儲庫模式來訪問EDM,我想使用自定義屬性AccessByRole開發某種權限檢查,如下所示:

public abstract class RepositoryBase: IRepository
{
    ...
    [AccessByRole]
    public virtual void Add(T entity)
    {
        ....
    }
    [AccessByRole]
    public virtual void Update(T entity)
    {
        ...
    }
    [AccessByRole]
    public virtual void Delete(T entity)
    {
        ...
    }        
    [AccessByRole]
    public virtual T GetById(long id)
    {
        ...
    }        
}

存儲庫的用法(我正在使用Autofac for IoC):

public class Service
{
   private readonly IRepository repository;
   public Service(IRepository repository)
   {
       this.repository = reporitory;
   }
   ....
   public UpdateUserEntities(...)
   {
   ...
      reporitory.Update(T); // There is a need for check user rights before calling this method.
   }

}

在調用CRUD操作之前,必須檢查用戶的權限。 所以我的問題是:屬性源代碼應如何顯示,以便在檢查權限后調用CRUD操作?

好吧,最簡單的方法是檢查每個動作中的角色,如果未授權,則將其短路。 例如:

 if(AuthorizedForRole[someAction]==true)
 {
     [some code]
 }else{
     Return "Unauthorized access attept";
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM