예제 #1
0
        public static bool HasAccess(string controllerName, string actionName, string customName)
        {
            var userService = new UserService();
            var user        = userService.Get();

            var userAuthorizationService = new UserRoleService();
            var moduleService            = new ModuleService();

            int moduleId = 0;

            var actions = actionName.Split(',');

            foreach (var action in actions)
            {
                var module = moduleService.Get($"{controllerName}.{action}.{customName}");

                if (module != null)
                {
                    moduleId = module.Id;
                    break;
                }
            }

            if (moduleId == 0)
            {
                return(false);
            }

            if (userAuthorizationService.GetUserRole(moduleId, user.Id))
            {
                return(true);
            }

            return(false);
        }
예제 #2
0
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            try
            {
                if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
                {
                    return;
                }

                var routingValues     = filterContext.RouteData.Values;
                var currentController = string.IsNullOrEmpty(RootController) ?  ((string)routingValues["controller"] ?? string.Empty) : RootController;
                //var currentAction = (string)routingValues["action"] ?? string.Empty;

                var contextUser = filterContext.RequestContext.HttpContext.User.Identity.Name;

                if (!int.TryParse(contextUser, out var userId))
                {
                    var userService = new UserService();
                    var user        = userService.GetByUsername(contextUser);
                    userId = user.Id;
                }

                var userAuthorizationService = new UserRoleService();
                var moduleService            = new ModuleService();

                var isAllowed = false;
                var url       = filterContext.HttpContext.Request.RawUrl;

                var actions = Action.Split(',');
                foreach (var action in actions)
                {
                    var module = moduleService.Get($"{currentController}.{action}");
                    isAllowed = isAllowed || userAuthorizationService.GetUserRole(module.Id, userId);
                }

                if (isAllowed)
                {
                    base.OnAuthorization(filterContext);
                }
                else
                {
                    filterContext.Result = new RedirectResult("/kullanici/oturum-ac?auth=0&ReturnUrl=" + url);
                }
            }
            catch
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            // Initializer initializer = new Initializer();
            //initializer.Seed();
            AutoMapperConfig.Config();
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "Excle\\新建工作表.xlsx";
            List <PointTxnDetailImport> excelList             = ImportExcelHelper.GetListFromExcel <PointTxnDetailImport>(filePath);
            PointTxnDetailService       pointTxnDetailService = new PointTxnDetailService();
            var list1 = Mapper.Map <List <PointTxnDetailImport>, List <PointTxnDetail> >(excelList);

            list1.ForEach(p =>
            {
                p.CreateUser  = "******";
                p.CredateTime = DateTime.Now;
            });
            pointTxnDetailService.Add(list1);


            UserService        userService        = new UserService();
            SysFunctionService sysFunctionService = new SysFunctionService();
            RoleService        roleService        = new RoleService();
            UserRightsService  userRightsService  = new UserRightsService();
            SysButtonService   sysButtonService   = new SysButtonService();
            SysMenuService     sysMenuService     = new SysMenuService();
            UserRoleService    userRoleService    = new UserRoleService();
            SystemService      systemService      = new SystemService();
            StoreReportService storeReportService = new StoreReportService();



            storeReportService.test();
            var             user = userService.Find(1);
            List <UserRole> list = userRoleService.GetUserRoles();

            foreach (var item in list)
            {
                Console.WriteLine("角色名称:" + item.Role.RoleName);
            }
            try
            {
                string jsonString = list.ToJsonString();
                Console.WriteLine("Json数据:" + jsonString);
                var userRoles = userRoleService.GetUserRole();

                string jsonString2 = userRoles.ToJsonString();
                Console.WriteLine("Json2数据:" + jsonString2);
            }
            catch (Exception ex)
            {
                string errorMsg = ex.Message;
            }

            List <UserRights> userRights = userRightsService.GetUserRights(user.Id);

//<<<<<<< .mine
//            //List<SysMenu> SysMenuList = new List<SysMenu>();
//            //try
//            //{
//            //    SysMenuList = userRights.Where(p => p.SysFunction.SysButtonId == 3).Select(p => p.SysFunction.SysMenu).ToList();
//            //}
//            //catch (Exception ex)
//            //{
//            //    string msg = ex.Message;
//            //}
//            //List<SysButton> SysButtonList = userRights.Select(p => p.SysFunction.SysButton).ToList();
//            //foreach (var item in SysMenuList)
//            //{
//            //    Console.WriteLine("菜单名称:" + item.MenuName);
//            //}
//            //foreach (var item in SysButtonList)
//            //{
//            //    Console.WriteLine("按钮名称:" + item.ButtonName);
//            //}
//||||||| .r59
//            List<SysMenu> SysMenuList = new List<SysMenu>();
//            try
//            {
//                SysMenuList = userRights.Where(p => p.SysFunction.SysButtonId == 3).Select(p => p.SysFunction.SysMenu).ToList();
//            }
//            catch (Exception ex)
//            {
//                string msg = ex.Message;
//            }
//            List<SysButton> SysButtonList = userRights.Select(p => p.SysFunction.SysButton).ToList();
//            foreach (var item in SysMenuList)
//            {
//                Console.WriteLine("菜单名称:" + item.MenuName);
//            }
//            foreach (var item in SysButtonList)
//            {
//                Console.WriteLine("按钮名称:" + item.ButtonName);
//            }
//=======
//            List<SysMenu> SysMenuList = new List<SysMenu>();
//            //try
//            //{
//            //    SysMenuList = userRights.Where(p => p.SysFunction.SysButtonId == 3).Select(p => p.SysFunction.SysMenu).ToList();
//            //}
//            //catch (Exception ex)
//            //{
//            //    string msg = ex.Message;
//            //}
//            //List<SysButton> SysButtonList = userRights.Select(p => p.SysFunction.SysButton).ToList();
//            //foreach (var item in SysMenuList)
//            //{
//            //    Console.WriteLine("菜单名称:" + item.MenuName);
//            //}
//            //foreach (var item in SysButtonList)
//            //{
//            //    Console.WriteLine("按钮名称:" + item.ButtonName);
//            //}
//>>>>>>> .r71
            Console.WriteLine("按钮名称:");
            Retry.Execute(Test, new TimeSpan(2000));
        }