Exemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            ViewData["UserInfo"] = CurrentUser;
            UserInfoCache userInfoCache = (UserInfoCache)HttpContext.RequestServices.GetService(typeof(UserInfoCache));

            ViewData["CacheMenu"]         = userInfoCache.GetMenuCaches();
            ViewData["CurrentController"] = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
            SetBreadcrumb(context);
            requestContext = new RequestContext()
            {
                RequestTime = DateTime.Now,
                UserId      = CurrentUser.Id
            };
        }
Exemplo n.º 2
0
        private void SetBreadcrumb(ActionExecutingContext context)
        {
            UserInfoCache userInfoCache     = (UserInfoCache)context.HttpContext.RequestServices.GetService(typeof(UserInfoCache));
            string        CurrentController = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
            var           cacheMenu         = userInfoCache.GetMenuCaches();

            if (cacheMenu == null)
            {
                return;
            }
            var currentMenu = cacheMenu.Where(prop => prop.Controller == CurrentController).FirstOrDefault();

            if (currentMenu == null)
            {
                //this.PageHeader = new PageHeaderViewModel() {};
                return;
            }
            this.PageHeader = new PageHeaderViewModel()
            {
                Title = currentMenu.DisplayName,
                Path  = new List <PageHeaderPath>()
            };
            if (string.IsNullOrEmpty(currentMenu.HierarchyCode))
            {
                return;
            }
            var Paths = cacheMenu.Where(p => currentMenu.HierarchyCode.StartsWith(p.HierarchyCode)).OrderBy(p => p.HierarchyCode);

            foreach (var item in Paths)
            {
                PageHeader.Path.Add(new PageHeaderPath()
                {
                    Name       = item.Name,
                    Controller = item.Controller
                });
            }

            ViewBag.Title = Paths.Select(p => p.DisplayName).LastOrDefault();
        }
Exemplo n.º 3
0
        private bool Authorize(AuthorizationFilterContext actionContext)
        {
            try
            {
                var    request = actionContext.HttpContext.Request;
                string token   = request.Cookies[SecurityManager._securityToken];
                //Kiểm tra token có hợp lệ hay không
                bool tokenValid = SecurityManager.IsTokenValid(token, request.Headers["User-Agent"]);
                if (!tokenValid)
                {
                    return(tokenValid);
                }
                UserInfoCache userInfoCache = (UserInfoCache)actionContext.HttpContext.RequestServices.GetService(typeof(UserInfoCache));
                string        UserId        = SecurityManager.getUserId(token);
                userInfo = userInfoCache.GetUser(UserId);
                if (userInfo == null)
                {
                    IUserRepository userRepository = (IUserRepository)actionContext.HttpContext.RequestServices.GetService(typeof(IUserRepository));
                    userInfo = (from obj in userRepository.All
                                where obj.Id == UserId
                                select new UserInfo()
                    {
                        Id = obj.Id,
                        DayOfBirth = obj.DayOfBirth,
                        Email = obj.Email,
                        FullName = obj.FullName,
                        Gender = obj.Gender,
                        PhoneNo = obj.PhoneNo,
                        UserName = obj.UserName,
                        RoleInfo = obj.UserRole.Select(p => new RoleInfo()
                        {
                            Id = p.RoleId,
                            RoleCode = p.Role.RoleCode,
                            RoleName = p.Role.RoleName
                        })
                    }).FirstOrDefault();
                    if (userInfo != null)
                    {
                        userInfoCache.SetUser(userInfo);
                    }
                }

                cacheMenu = userInfoCache.GetMenuCaches();
                if (cacheMenu == null)
                {
                    IMenuRepository menuRepository = (IMenuRepository)actionContext.HttpContext.RequestServices.GetService(typeof(IMenuRepository));
                    cacheMenu = menuRepository.All.Select(p => new CacheMenu
                    {
                        Order         = p.Order,
                        Name          = p.Name,
                        DisplayName   = p.DisplayName,
                        HierarchyCode = p.HierarchyCode,
                        Icon          = p.Icon,
                        Controller    = p.Controller,
                        Roles         = p.MenuRoles.Select(r => r.RoleId).ToList(),
                    }).OrderBy(p => p.HierarchyCode).ToList();
                    userInfoCache.UpdateMenuCaches(cacheMenu);
                }

                ////Kiểm tra thông tin user trong cache, Nếu không tồn tại thì return false
                return(userInfo != null);
            }
            catch (Exception)
            {
                return(false);
            }
        }