Esempio n. 1
0
 public void SaveConfig(PowerConfig config, string configPath)
 {
     lock (LockHelper)
     {
         SerializeHelper.Serialize(config, configPath);
     }
 }
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (base.LoginUser == null)
            {
                filterContext.Result = new RedirectResult("/AdminLogin");
            }

            this.Power = CacheHelper.GetCache(Constant.CacheKey.PowerConfigCacheKey) as PowerConfig;
            if (this.Power == null)
            {
                this.Power = powerConfigService.LoadConfig(Constant.PowerConfigPath);
                CacheHelper.SetCache(Constant.CacheKey.PowerConfigCacheKey, this.Power);
            }
        }
Esempio n. 3
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var token = CookieHelper.GetCookieValue("Admin");

            this.LoginUser = CacheHelper.GetCache(Constant.CacheKey.LoginAdminInfoCacheKey + "_" + token) as User;
            bool b = false;

            if (this.IsSuper == false)
            {
                //非超级管理员专属操作

                //权限id集合
                string[] acts = LoginUser.Role.ActionIds.Split(',');

                this.Power = CacheHelper.GetCache(Constant.CacheKey.PowerConfigCacheKey) as PowerConfig;

                if (this.Power == null)
                {
                    this.Power = powerConfigService.LoadConfig(Constant.PowerConfigPath);
                    CacheHelper.SetCache(Constant.CacheKey.PowerConfigCacheKey, this.Power);
                }
                try
                {
                    if (this.Power != null)
                    {
                        //Power.PowerGroupList.Where(p=>p.PowerList.FirstOrDefault(t => t.Id == PowerId)==null);
                        foreach (var li in this.Power.PowerGroupList)
                        {
                            var p = li.PowerList.FirstOrDefault(t => t.NoPowerId == PowerId);
                            if (p != null)
                            {
                                if (acts.Contains(p.NoPowerId))
                                {
                                    //存在权限
                                    b = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    b = false;
                }
            }
            //超级管理员都可以使用
            if (this.LoginUser.IsSuperUser)
            {
                b = true;
            }

            #region 无权限执行
            if (b == false)
            { //无权限执行
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult()
                    {
                        ContentEncoding = Encoding.UTF8,
                        Data            = new ResultModel
                        {
                            pass = false,
                            msg  = "无权访问"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Controller.ViewData["ErrorMessage"] = "无权访问"; //filterContext.Exception.Message + " 亲!您犯错了哦!";//得到报错的内容
                    filterContext.Result = new ViewResult()                     //new一个url为Error视图
                    {
                        ViewName = "Error",                                     /*在Shard文件夹下*/
                        ViewData = filterContext.Controller.ViewData            //view视图的属性中的viewdata被赋值
                    };
                }
            }
            #endregion
        }
Esempio n. 4
0
        public ActionResult SetPower(string param)
        {
            try
            {
                string   path     = AppDomain.CurrentDomain.BaseDirectory;
                string   dir      = Path.Combine(path, "bin");
                string   filePath = dir + Path.DirectorySeparatorChar + param;
                byte[]   filedata = System.IO.File.ReadAllBytes(filePath);
                Assembly assembly = Assembly.Load(filedata);
                Type[]   t_arr    = assembly.GetExportedTypes();

                Type   _typePower = typeof(PowerAttribute);
                Type   _typeGroup = typeof(GroupAttribute);
                string _p         = _typePower.FullName;
                string _g         = _typeGroup.FullName;
                //
                string areStr = "/Admin";
                //
                PowerConfig _powerConf = new PowerConfig();
                foreach (Type t in t_arr)//针对每个类型获取详细信息
                {
                    string className = t.Name;
                    if (className.EndsWith("Controller"))//是否控制器
                    {
                        //获取class的Attribute
                        object[]   att_class   = t.GetCustomAttributes(false);
                        PowerGroup _group      = null;
                        string     _controller = className.Replace("Controller", "");

                        foreach (object obj in att_class)
                        {
                            Type   _t     = obj.GetType();
                            string t_name = _t.ToString();
                            if (t_name == _g)
                            {
                                _group = new PowerGroup();
                                PropertyInfo p_NoGroupId = _t.GetProperty("NoGroupId");
                                object       _cc         = p_NoGroupId.GetValue(obj, null);
                                if (_cc != null)
                                {
                                    string _NoGroupId = _cc.ToString();
                                    _group.NoGroupId = _NoGroupId;
                                }
                                PropertyInfo p_GroupName = _t.GetProperty("GroupName");
                                object       _tt         = p_GroupName.GetValue(obj, null);
                                if (_tt != null)
                                {
                                    string _GroupName = _tt.ToString();
                                    _group.GroupName = _GroupName;
                                }
                                PropertyInfo p_IsShow = _t.GetProperty("IsShow");
                                object       _ss      = p_IsShow.GetValue(obj, null);
                                if (_ss != null)
                                {
                                    string _IsShow = _ss.ToString();
                                    _group.IsShow = Convert.ToBoolean(_IsShow);
                                }
                                PropertyInfo p_Icon = _t.GetProperty("Icon");
                                object       _oo    = p_Icon.GetValue(obj, null);
                                if (_oo != null)
                                {
                                    string _Icon = _oo.ToString();
                                    _group.Icon = _Icon;
                                }
                                break;
                            }
                        }
                        //获取方法信息
                        MethodInfo[] MethodInfo_arr = t.GetMethods();
                        foreach (MethodInfo m in MethodInfo_arr)
                        {
                            string   methodName = m.Name;
                            object[] att_obj    = m.GetCustomAttributes(false);
                            foreach (object obj in att_obj)
                            {
                                Type   _t     = obj.GetType();
                                string t_name = _t.ToString();
                                if (t_name == _p)
                                {
                                    Power        _power    = new Model.Sys.Power();
                                    PropertyInfo p_PowerId = _t.GetProperty("PowerId");
                                    object       _tt       = p_PowerId.GetValue(obj, null);
                                    if (_tt != null)
                                    {
                                        string _PowerId = _tt.ToString();
                                        _power.NoPowerId = _PowerId;
                                    }
                                    PropertyInfo p_PowerName = _t.GetProperty("PowerName");
                                    object       _pp         = p_PowerName.GetValue(obj, null);
                                    if (_pp != null)
                                    {
                                        string _PowerName = _pp.ToString();
                                        _power.PowerName = _PowerName;
                                    }
                                    PropertyInfo p_PowerDes = _t.GetProperty("PowerDes");
                                    object       _ss        = p_PowerDes.GetValue(obj, null);
                                    if (_ss != null)
                                    {
                                        string _PowerDes = _ss.ToString();
                                        _power.PowerDes = _PowerDes;
                                    }
                                    PropertyInfo p_IsShow = _t.GetProperty("IsShow");
                                    object       _cc      = p_IsShow.GetValue(obj, null);
                                    if (_cc != null)
                                    {
                                        string _IsShow = _cc.ToString();
                                        _power.IsShow = Convert.ToBoolean(_IsShow);
                                    }
                                    PropertyInfo p_Icon = _t.GetProperty("Icon");
                                    object       _oo    = p_Icon.GetValue(obj, null);
                                    if (_oo != null)
                                    {
                                        string _Icon = _oo.ToString();
                                        _power.Icon = _Icon;
                                    }
                                    _power.NoGroupId = _group.NoGroupId;
                                    _power.ParamStr  = areStr + "/" + _controller + "/" + methodName;
                                    if (_group.PowerList == null)
                                    {
                                        _group.PowerList = new List <Power>();
                                    }
                                    if (_group.PowerList == null)
                                    {
                                        _group.PowerList = new List <Power>();
                                    }
                                    if (_group.PowerList.Count == 0 || _group.PowerList.All(p => p.NoPowerId != _power.NoPowerId))
                                    {
                                        _group.PowerList.Add(_power);
                                    }
                                }
                            }
                        }
                        if (_group != null)
                        {
                            if (_powerConf.PowerGroupList == null)
                            {
                                _powerConf.PowerGroupList = new List <PowerGroup>();
                            }
                            if (_powerConf.PowerGroupList.Count == 0 ||
                                _powerConf.PowerGroupList.All(g => g.NoGroupId != _group.NoGroupId))
                            {
                                _powerConf.PowerGroupList.Add(_group);
                            }
                        }
                    }
                }
                SerializeHelper.Serialize(_powerConf, AppDomain.CurrentDomain.BaseDirectory + "Configuration" + Path.AltDirectorySeparatorChar + "PowerConfig.config");
                return(Json(new ResultModel
                {
                    pass = true,
                    msg = "初始化成功",
                    append = _powerConf
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ResultModel {
                    pass = false, msg = e.Message
                }));
            }
        }