Ejemplo n.º 1
0
        protected List <MenuControlAttribute> GetMenuForUser(ClaimsPrincipal user, List <MenuControlAttribute> data)
        {
            try
            {
                var current = user;
                if (current == null || !current.Identity.IsAuthenticated)
                {
                    return(null);
                }
                else
                {
                    List <MenuControlAttribute> cache = CacheExtends.GetDataFromCache <List <MenuControlAttribute> >(User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Email).Value + "menmu");
                    if (cache != null)
                    {
                        return(cache);
                    }
                    if (current.IsInRole("administrator"))
                    {
                        CacheExtends.SetObjectFromCache(User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Email).Value + "menmu", Cookies.ExpiresLogin, data);
                        return(data);
                    }
                    else
                    {
                        List <MenuControlAttribute> menuControls = new List <MenuControlAttribute>();
                        var access = new CPAccessService();
                        int count  = data.Count;
                        var cl     = User.Claims.FirstOrDefault(o => o.Type == "RoleID");
                        if (cl == null)
                        {
                            return(null);
                        }
                        int RoleID = cl != null?int.Parse(cl.Value) : 0;

                        for (int i = 0; i < count; i++)
                        {
                            var item = data[i];
                            if (access.GetPermission(RoleID, item.CModule, "index"))
                            {
                                menuControls.Add(item);
                            }
                        }
                        if (menuControls != null)
                        {
                            CacheExtends.SetObjectFromCache(User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Email).Value + "menmu", Cookies.ExpiresLogin, menuControls);
                            return(menuControls);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 public CPAccessController()
 {
     _service = new CPAccessService();
 }
Ejemplo n.º 3
0
 public CPAccessController(CPAccessService accessService)
 {
     _service = accessService;
 }
Ejemplo n.º 4
0
 public CPRoleController(CPRoleService roleService, CPAccessService accessService)
 {
     _service       = roleService;
     _accessService = accessService;
 }
Ejemplo n.º 5
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            Controller controller = (Controller)context.Controller;
            string     ctrlName   = context.RouteData.Values["Controller"].ToString();
            string     actName    = context.RouteData.Values["Action"].ToString();
            var        user       = controller.User;

            if ((ctrlName.ToLower() == "cpaccounts" && (actName.ToLower() == "signin" || actName == "signout" || actName == "register")) ||
                (ctrlName.ToLower() == "cphome" && (user != null && user.Identity.IsAuthenticated)) ||
                (user != null && user.Identity.IsAuthenticated && user.IsInRole("administrator")))
            {
                base.OnActionExecuting(context);
            }
            else
            {
                if (user == null || !user.Identity.IsAuthenticated)
                {
                    string _returnUrl = System.Net.WebUtility.UrlEncode(ctrlName + "/" + actName);
                    context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "cpaccounts", action = "signin", returnurl = _returnUrl }));
                }
                else
                {
                    var res       = context.HttpContext.Response;
                    var access    = new CPAccessService();
                    var claimRole = user.Claims.SingleOrDefault(o => o.Type == "RoleID");
                    if (claimRole != null)
                    {
                        int roleID = int.Parse(claimRole.Value);
                        if (access.GetPermission(roleID, ctrlName, actName))
                        {
                            base.OnActionExecuting(context);
                        }
                        else
                        {
                            var error = new Dictionary <string, string>
                            {
                                { "code", "403" },
                                { "msg", "bạn không đủ quyền hạn để thực hiện chức năng này !!" }
                            };
                            controller.TempData["error"] = error;
                            if (controller.HttpContext.Request.Method.ToLower() != "get")
                            {
                                context.Result = new RedirectToRouteResult(
                                    new RouteValueDictionary(new
                                {
                                    controller = ctrlName,
                                    action     = "index"
                                }));
                            }
                            else
                            {
                                context.Result = new RedirectToRouteResult(
                                    new RouteValueDictionary(new
                                {
                                    controller = "cphome",
                                    action     = "index"
                                }));
                            }
                        }
                    }
                    else
                    {
                        var error = new Dictionary <string, string>
                        {
                            { "code", "403" },
                            { "msg", "bạn không đủ quyền hạn để thực hiện chức năng này!!" }
                        };
                        controller.TempData["error"] = error;
                        context.Result = new RedirectToRouteResult(
                            new RouteValueDictionary(new
                        {
                            controller = "cphome",
                            action     = "index"
                        }));
                    }
                }
            }
        }