2017-09-28 8 views
0

現在、C#MVCプロジェクトを作成中です。私はCustomAuthorizationAttributeを使用して、私のプロジェクトのすべてのユーザーを認証しました。Razorビューのカスタム権限

は私が3つの役割があります。スーパー管理者、管理者およびユーザー

対応する役割を追加するためのオプションがあり、新規ユーザーを作成している間に、追加できません(「ユーザーの役割をしている人を編集することができますユーザーを編集する)。それぞれ、「PermissionFunctionを」私は2つのテーブルを作成し、この認証を設定し、「許可」に

PermissionFunction

|---------------------|------------------| 
| PermFunc_ID  |  bigint  | 
|---------------------|------------------| 
| PermFunc_Name  |  varchar(50) | 
|---------------------|------------------| 

許可

|---------------------|------------------| 
| Perm_ID   |  bigint  | 
|---------------------|------------------| 
| Perm_RollID  |  bigint  | 
|---------------------|------------------| 
| Perm_PermFuncID |  bigint  | 
|---------------------|------------------| 

PermFunc_IDPerm_ID表の詳細を以下に示します。それぞれテーブルのプライマリキーです。 PermFunc_Nameは、すべてのコントローラー内の各アクションを参照する名前です。 許可テーブルには、ロールPermissionFunctionの両方の外部キーが含まれています。ここでは参考のため

は私役割テーブルです:認証のために

|---------------------|------------------| 
| Rol_ID   | bigint  | 
|---------------------|------------------| 
| Rol_Name   | varchar(50) | 
|---------------------|------------------| 

私はCustomAuthorizationAttributeクラスを追加し、すべてのコントローラのアクションに、許可属性を追加しました。例えば

PermissionFunction表は以下のようにバレス検討:

|---------------------|-------------------| 
| PermFunc_ID  | PermFunc_Name | 
|---------------------|-------------------| 
|  1    | userIndex  | 
|---------------------|-------------------| 
|  2    | userCreate  | 
|---------------------|-------------------| 

と私のにHomeController

public class UserController : Controller 
{ 

    [CustomAuthorization(IdentityRoles = "userIndex")] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [CustomAuthorization(IdentityRoles = "userCreate")] 
    public ActionResult Create() 
    { 
     return View(); 
    } 

} 

CustomAuthorizationAttributeクラス:

public class CustomAuthorizationAttribute : AuthorizeAttribute 
{ 
    private PermissionRepository _permission = new PermissionRepository(); 
    private PermissionFuncRepository _permissionFun = new PermissionFuncRepository(); 

    // roles start 
    public string IdentityRoles 
    { 
     get { return _permissionName ?? String.Empty; } 
     set { _permissionName = value; } 
    } 

    private string _permissionName; 

    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     //do the base class AuthorizeCore first 
     if (httpContext.User.Identity.IsAuthenticated) 
     { 
      string RoleID = FormsAuthentication.Decrypt(httpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name.Split('|')[1]; 

      var permisionID = _permissionFun.FindByName(_permissionName); 

      if(permisionID != null) 
      { 
       var permis = _permission.GetPermission().Where(a => a.Perm_PermFuncID == permisionID.PermFunc_ID && a.Perm_RollID.ToString() == RoleID).FirstOrDefault(); 
       if (permis != null) 
       { 
        return true; 
       } 
      } 

     } 
     return false; 

    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 

     //if the user is not logged in use the deafult HandleUnauthorizedRequest and redirect to the login page 
     if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
     { 
      base.HandleUnauthorizedRequest(filterContext); 
     } 
     //if the user is logged in but is trying to access a page he/she doesn't have the right for show the access denied page 
     else 
     { 
      // the controller action was invoked with an AJAX request 
      if (filterContext.HttpContext.Request.IsAjaxRequest()) 
      { 
       filterContext.Result = new RedirectResult("~/Home/AccessDeniedNew"); 
      } 
      else 
      { 
       filterContext.Result = new RedirectResult("~/Home/AccessDenied"); 
      } 
     } 
    } 

} 

これは問題なく動作しています。


私の質問


これは私のインデックス htmlです:

<button class="btn btn-sm btn-rounded btn-success" type="button" id ="CreateButton" onclick="UserCreate()"> Create New User </button> // create new user 

    ..... // code continues 

// code for modal popup 
<div id="edit-user" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 


</div> 

<script type="text/javascript"> 

    function UserCreate() { 
     var url = "/User/Create/"; 
      $.get(url, function (data) { 
       $('#edit-user').html(data); 
       $('#edit-user').modal('show'); 
      }); 
    } 
</script> 

CreateButtonは、モーダルポップアップをクリック

詳細を追加するために来て、新しいユーザーを作成します。


ユーザーが新規ユーザーを作成するためのアクセスがない場合、私が作成した新しいボタンを隠すことができどのような方法がありますか。? (つまり:のユーザー作成PermissionFunctionの表にない場合)。

答えて

0

必要に応じて、権限を確認してページにボタンを追加できます。許可情報を取得する方法はすでに知っているので、いくつかの情報をViewDataまたはViewBagに保存して、作成ボタンがそこにあるかどうかを確認できます。情報は単純なbooleanにすることができます。

@if(ViewBag.CanCreate) 
{ 
    <button class="btn btn-sm btn-rounded btn-success" type="button" id ="CreateButton" onclick="UserCreate()"> Create New User </button> 
} 

ここで、CanCreateはコントローラから設定できます。

0

私はむしろあなたは私があなたにも最後を取り除くことができると思いソリューションは非常にエレガントではありませんしかし、あなたは

@html.AuthorizeActionLink("button name","your action/controller name","your security role") 

のように、独自のアクションリンクを作成することができthis thread

からアイデアを取ることをお勧めパラメータを指定して、接続しているユーザーと、表示するかどうかを選択する呼び出し元のアクションに必要な権限を自動的にチェックします。

本当に特定のhtmlが必要な場合は、解決策を提案してください。this thread 1ページに複数のアクセス権を処理する方が簡単です。実際にViewbagsは、管理する人がたくさんいる場合には苦痛になります。 implementionためauthorizeActionLinkを私はthis

が見つかりました:

希望これは

よろしく

編集を支援します

関連する問題