2017-01-17 10 views
0

私は、Get(int id)、Get()、Insert(T t)、Edit(T t)のような単純な基本的な作業を行う、 )。コードの重複を避けるため、私はGenericControllerを作成し、このGenericControllerから他のすべてのコントローラーを継承しました。すべてがとてもうまく動作します。しかし、継承されたときに同じコントローラアクションで異なるユーザーロールを実装する場合は、問題にぶつかります。例えば、以下のコードのルック取る:上記スニペット挿入アクションでMVC(ASP)継承すると同じアクションで異なるユーザーの役割を実装する

public class GenericController<T>: Controller{ 

    //other actions 

    [HttpGet] 
    public async Task<IEnumrable<T>> Get(){ 
     //necessary action goes here 
    } 
    [HttpPost] 
    public async Task<IActionResult> Insert(T t){ 
     //necessary action with t 
    }  
} 

[Authorize] 
public class ProductController: GenericController<Product>{ 

    //Get action is authorized to everyone 
    //Insert action is authorized to Manager only 
} 

[Authorize] 
public class EmployeeController: GenericController<Employee>{ 

    //Get action is authorized to everyone 
    //Insert action is authorized to Owner only 
} 

を、それをGenericControllerから継承されている製品と汎用コントローラの両方で異なる権限を有しています。

コードを継承したコントローラに複製したくありません。しかし、正しい認可も必要です。誰もが適切な解決策を知っていますか?どんな助けでも大歓迎です。

+0

おそらくそれは合っているかもしれません:なぜあなたはInsertメソッド内のユーザーの役割をチェックしていないのですが、マネージャでない場合はUnauthorizedを返します。 – alecardv

答えて

1

許可フィルタを作成し、以下のようなコントローラとアクションを見つけます。そして、その役割を維持する。

関連する問題