码迷,mamicode.com
首页 > 其他好文 > 详细

Action权限验证

时间:2014-05-16 18:33:23      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

1 Action添加特性

bubuko.com,布布扣
   [PermissionFilterForJson(Name = "AdvertiserId", ActionName = EaActionNames.广告主相关_广告主上线,Type = (int) EaEnum.LoginType.Advertiser)]
        public JsonResult AdvertiserEffect(ExEaAdvertiserModel model)
        {
            MessageInfo msg = _eaAdvertiserAuditBussiness.Online(base.AccountId, model.AdvertiserId, model.IsHidden,
                                                                 model.IsUpdateOnLineTime);
            return Json(msg, JsonRequestBehavior.AllowGet);
        }
bubuko.com,布布扣

 

2 操作项权限验证,返回Json

bubuko.com,布布扣
  public class PermissionFilterForJsonAttribute : PermissionFilterForAttribute
    {
        private readonly IPermissionManagementBussiness _pms = ObjectFactory.GetInstance<IPermissionManagementBussiness>();

        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            bool premission = false;
            MessageInfo msg = new MessageInfo {Status = MessageStatus.Error, Message = "您没有此项功能的操作权限", Title = "pms"};

            try
            {
                int id = 0;
                if (filterContext.ActionParameters.ContainsKey(this.Name))
                {
                    id = filterContext.ActionParameters[this.Name].TryInt();
                }
                else
                {
                    var prop = filterContext.ActionParameters.First().Value.GetType().GetProperty(this.Name);
                    if (prop != null)
                    {
                        var v = prop.GetValue(filterContext.ActionParameters.First().Value, null);
                        if (v != null)
                        {
                            id = Convert.ToInt32(v);
                        }
                    }
                }
                switch (this.Type)
                {
                    case (int) EaEnum.LoginType.Advertiser:
                        premission = _pms.AdvertiserCheckPermission(id, this.AccountId, this.ActionName);
                        break;
                    case (int) EaEnum.LoginType.Media:
                        premission = _pms.MediaCheckPermission(id, this.AccountId, this.ActionName);
                        break;
                }
            }
            catch (Exception e)
            {
                msg.Message = e.Message;
            }

            if (!premission)
            {
                filterContext.Result = new JsonResult
                    {
                        Data = msg,
                        ContentType = "text/json",
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
            }
        }

    }
bubuko.com,布布扣


3 面包屑权限验证,返回View

bubuko.com,布布扣
    public class PermissionFilterForViewAttribute : PermissionFilterForAttribute
    {
        private readonly IPermissionManagementBussiness _pms = ObjectFactory.GetInstance<IPermissionManagementBussiness>();

        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            bool premission = _pms.CheckPms(this.AccountId, this.ActionName);

            if (!premission)
            {
                filterContext.Result = new RedirectResult("/AccountLogin/NoURL");
            }
        }

    }
bubuko.com,布布扣

4 特性参数

bubuko.com,布布扣
    public class PermissionFilterForAttribute : ActionFilterAttribute
    {

        /// <summary>
        /// 类型【广告主/媒体主】
        /// </summary>
        public int Type { get; set; }

        /// <summary>
        /// 动作名称
        /// </summary>
        public string ActionName { get; set; }

        /// <summary>
        /// 是否只校验CMS权限,不校验资源权限
        /// </summary>
        public bool IsCheckCmsOnly { get; set; }

        /// <summary>                   
        /// 当前登录用户ID
        /// </summary>
        public int AccountId
        {
            get
            {
                if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
                    return ((EAIdentity)HttpContext.Current.User.Identity).AccountId;
                else
                    return 0;
            }
        }

        /// <summary>
        /// 匹配不统一的广告主/媒体主命名
        /// </summary>
        private string _name;

        public string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_name))
                {
                    _name = this.Type == (int) EaEnum.LoginType.Advertiser ? "advertiserId" : "mediaId";
                }

                return _name;

            }
            set { _name = value; }
        }

    }
bubuko.com,布布扣

 

 

 

 

Action权限验证,布布扣,bubuko.com

Action权限验证

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/Mylimo/p/3725864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!