現在、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_IDとPerm_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の表にない場合)。