コード例 #1
0
 public void SaveConfig(PowerAdmin config, string configPath)
 {
     lock (LockHelper)
     {
         SerializeHelper.Serialize(config, configPath);
     }
 }
コード例 #2
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (base.LoginUser == null)
            {
                filterContext.Result = new RedirectResult("/AdminLogin");
            }
            HTools.CacheObj obj = base.cacheService.Get(Constant.CacheKey.PowerConfigCacheKey);
            if (obj != null && obj.value != null)
            {
                if (obj.value is Newtonsoft.Json.Linq.JObject)
                {
                    PowerAdmin power = obj.value.ParseJSON <PowerAdmin>();
                    this.Power = power;
                }
                else
                {
                    this.Power = (PowerAdmin)obj.value;
                }
            }
            else
            {
                this.Power = null;
            }

            if (this.Power == null)
            {
                this.Power = powerConfigService.LoadConfig(Constant.PowerConfigPath);
                base.cacheService.Add(Constant.CacheKey.PowerConfigCacheKey,
                                      new HTools.CacheObj()
                {
                    value = this.Power, AbsoluteExpiration = new TimeSpan(1, 0, 0, 0)
                });
            }
            if (this.pathConfig == null)
            {
                this.pathConfig = pathConfigService.LoadConfig(Constant.PathConfigPath);
                base.cacheService.Add(Constant.CacheKey.PathConfigCacheKey,
                                      new HTools.CacheObj()
                {
                    value = this.pathConfig, AbsoluteExpiration = new TimeSpan(1, 0, 0, 0)
                });
            }
            if (this.pathConfig != null)
            {
                ViewData["_PathConfig"] = this.pathConfig;
            }
            if (base.LoginUser != null)
            {
                ViewData["_LoginUser"] = base.LoginUser;
            }
            if (this.Power != null)
            {
                ViewData["_Power"] = this.Power;
            }
        }
コード例 #3
0
ファイル: PowerAttribute.cs プロジェクト: pilgrimzh/Cactus
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 权限判断
            var             token      = CookieHelper.GetCookieValue("Admin");
            string          _GroupName = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString();
            HTools.CacheObj obj        = cacheService.Get(Constant.CacheKey.LoginAdminInfoCacheKey + "_" + token);
            if (obj != null && obj.value != null)
            {
                if (obj.value is Newtonsoft.Json.Linq.JObject)
                {
                    User user = obj.value.ParseJSON <User>();
                    this.LoginUser = user;
                }
                else
                {
                    this.LoginUser = (User)obj.value;
                }
            }
            else
            {
                this.LoginUser = null;
            }
            bool b = false;
            if (!this.LoginUser.IsSuperUser)
            {
                string[] acts = LoginUser.Role.ActionIds.Split(',');
                obj = cacheService.Get(Constant.CacheKey.PowerConfigCacheKey);
                if (obj != null && obj.value != null)
                {
                    if (obj.value is Newtonsoft.Json.Linq.JObject)
                    {
                        PowerAdmin powerAdmin = obj.value.ParseJSON <PowerAdmin>();
                        this.Power = powerAdmin;
                    }
                    else
                    {
                        this.Power = (PowerAdmin)obj.value;
                    }
                }
                else
                {
                    this.Power = null;
                }
                if (this.Power == null)
                {
                    this.Power = powerConfigService.LoadConfig(Constant.PowerConfigPath);
                    cacheService.Add(Constant.CacheKey.PowerConfigCacheKey, new CacheObj()
                    {
                        value = this.Power, AbsoluteExpiration = new TimeSpan(1, 0, 0, 0)
                    });
                }
                try
                {
                    if (this.Power != null)
                    {
                        var list = this.Power.list;
                        foreach (var li in list)
                        {
                            var p = li.module.FirstOrDefault(t => t.Name == ModuleName);
                            if (p != null)
                            {
                                string action_type = _GroupName + "|" + p.Name + "|" + actionEnum.ToString();//格式 模块名|操作
                                if (acts.Contains(action_type))
                                {
                                    b = true;//存在权限
                                    break;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    b = false;
                }
            }
            else
            {
                b = true;
            }
            #endregion

            #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
        }
コード例 #4
0
ファイル: SysController.cs プロジェクト: pilgrimzh/Cactus
        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";
                //
                PowerAdmin _powerConf = new PowerAdmin();
                if (_powerConf.list == null)
                {
                    _powerConf.list = new List <PowerGroup>();
                }
                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", "");
                        string _controller = className.Remove(className.IndexOf("Controller"), ("Controller").Length);
                        foreach (object obj in att_class)
                        {
                            Type   _t     = obj.GetType();
                            string t_name = _t.ToString();
                            if (t_name == _g)
                            {
                                _group      = new PowerGroup();
                                _group.Name = _controller;
                                PropertyInfo p_Title = _t.GetProperty("Title");
                                object       _ss     = p_Title.GetValue(obj, null);
                                if (_ss != null)
                                {
                                    _group.Title = _ss.ToString();
                                }
                                PropertyInfo p_Des = _t.GetProperty("Des");
                                object       _dd   = p_Des.GetValue(obj, null);
                                if (_dd != null)
                                {
                                    _group.Des = _dd.ToString();
                                }
                                PropertyInfo p_IsShow = _t.GetProperty("IsShow");
                                object       _cc      = p_IsShow.GetValue(obj, null);
                                if (_cc != null)
                                {
                                    _group.IsShow = Convert.ToBoolean(_cc.ToString());
                                }
                                PropertyInfo p_Icon = _t.GetProperty("Icon");
                                object       _oo    = p_Icon.GetValue(obj, null);
                                if (_oo != null)
                                {
                                    _group.Icon = _oo.ToString();
                                }
                                break;
                            }
                        }
                        if (_group != null && _group.module == null)
                        {
                            _group.module = new List <PowerModule>();
                            List <PowerModule> modules = new List <PowerModule>();
                            //获取方法信息
                            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)
                                    {
                                        PowerModule  _power = new PowerModule();
                                        PropertyInfo p_Name = _t.GetProperty("ModuleName");
                                        object       _pp    = p_Name.GetValue(obj, null);
                                        if (_pp != null)
                                        {
                                            _power.Name = _pp.ToString();
                                        }
                                        PropertyInfo p_actionEnum = _t.GetProperty("actionEnum");
                                        object       _aa          = p_actionEnum.GetValue(obj, null);
                                        if (_aa != null)
                                        {
                                            _power.Action_Type = _aa.ToString();
                                        }
                                        PropertyInfo p_IsShow = _t.GetProperty("IsShow");
                                        object       _cc      = p_IsShow.GetValue(obj, null);
                                        if (_cc != null)
                                        {
                                            _power.IsShow = Convert.ToBoolean(_cc.ToString());
                                        }
                                        PropertyInfo p_Title = _t.GetProperty("Title");
                                        object       _ss     = p_Title.GetValue(obj, null);
                                        if (_ss != null)
                                        {
                                            _power.Title = _ss.ToString();
                                        }
                                        PropertyInfo p_Icon = _t.GetProperty("Icon");
                                        object       _oo    = p_Icon.GetValue(obj, null);
                                        if (_oo != null)
                                        {
                                            _power.Icon = _oo.ToString();
                                        }
                                        _power.ParamStr = areStr + "/" + _controller + "/" + methodName;
                                        modules.Add(_power);
                                    }
                                }
                            }

                            //去重后的模块
                            Dictionary <string, PowerModule> module_dic_temp = new Dictionary <string, PowerModule>();
                            //去重后的模块Action
                            Dictionary <string, Dictionary <string, string> > moduleaction_dic = new Dictionary <string, Dictionary <string, string> >();
                            //过滤重复模块名
                            foreach (var module in modules)
                            {
                                if (module.IsShow)
                                {
                                    if (!module_dic_temp.ContainsKey(module.Name))
                                    {
                                        module_dic_temp.Add(module.Name, module);
                                    }
                                    else
                                    {
                                        module_dic_temp[module.Name] = module;
                                    }
                                }
                                else
                                {
                                    if (!module_dic_temp.ContainsKey(module.Name))
                                    {
                                        module_dic_temp.Add(module.Name, module);
                                    }
                                }

                                if (!moduleaction_dic.ContainsKey(module.Name))
                                {
                                    Dictionary <string, string> dic = new Dictionary <string, string>();
                                    dic.Add(module.Action_Type, module.Action_Type);
                                    moduleaction_dic.Add(module.Name, dic);
                                }
                                else
                                {
                                    Dictionary <string, string> dic = moduleaction_dic[module.Name];
                                    if (!dic.ContainsKey(module.Action_Type))
                                    {
                                        dic.Add(module.Action_Type, module.Action_Type);
                                    }
                                    moduleaction_dic[module.Name] = dic;
                                }
                            }
                            Dictionary <string, PowerModule> module_dic = new Dictionary <string, PowerModule>();
                            //合并操作标示
                            foreach (var dic in module_dic_temp)
                            {
                                if (moduleaction_dic.ContainsKey(dic.Key))
                                {
                                    string Action_Type = "";
                                    foreach (var m_dic in moduleaction_dic[dic.Key])
                                    {
                                        if (string.IsNullOrEmpty(Action_Type))
                                        {
                                            Action_Type += m_dic.Value;
                                        }
                                        else
                                        {
                                            Action_Type += "," + m_dic.Value;
                                        }
                                    }
                                    dic.Value.Action_Type = Action_Type;
                                    module_dic.Add(dic.Key, dic.Value);
                                }
                            }
                            //
                            _group.module = module_dic.Values.ToList();
                            _powerConf.list.Add(_group);
                        }
                    }
                }
                SerializeHelper.Serialize(_powerConf, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration", "PowerConfig.config"));

                if (Constant.CacheKey.List[Cactus.Model.Sys.Enums.Constant.CacheKey.PowerConfigCacheKey].Count() > 0)
                {
                    base.cacheService.Remove(Cactus.Model.Sys.Enums.Constant.CacheKey.PowerConfigCacheKey);
                }
                return(Json(new ResultModel
                {
                    pass = true,
                    msg = "初始化成功",
                    append = _powerConf
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ResultModel {
                    pass = false, msg = e.Message
                }));
            }
        }