ページ権限設計の考え方の概要
7877 ワード
1、ユーザーオブジェクト設計
2、拡張Html.Authorize(拡張方法)
3、使用例
1 public class User
2 {
3 public string UserName { get; set; }
4 public string Password { get; set; }
5 //
6 public List<string> Permission { get; private set; }
7 public bool CheckPermission(string code)
8 {
9 if (this.Permission != null && this.Permission.Contains(code))
10 {
11 return true;
12 }
13 return false;
14 }
15 /// <summary>
16 ///
17 /// </summary>
18 /// <param name="code"></param>
19 public void SetPermission(string code)
20 {
21 if (this.Permission == null)
22 {
23 this.Permission = new List<string>();
24 }
25 this.Permission.Add(code);
26 }
27 }
2、拡張Html.Authorize(拡張方法)
public static class AuthorizeExtensions
{
public static bool IsAuthorized(this HtmlHelper helper, string permissionCode)
{
return UserService.GetCurrentUser().CheckPermission(permissionCode);
}
public static string Authorize(this string html, string permissionCode)
{
if (UserService.GetCurrentUser().CheckPermission(permissionCode);)
{
return html;
}
else
{
return string.Empty;
}
}
public static IHtmlString Authorize(this IHtmlString html, string permissionCode)
{
if (UserService.GetCurrentUser().CheckPermission(permissionCode);)
{
return html;
}
else
{
return MvcHtmlString.Empty;
}
}
}
3、使用例
1 @if (Html.IsAuthorized(Utility.AuthConst.IcsonCodeAddVirtualQty))
2 {
3 <a onclick="XX">
4 </a>
5 }