using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Central.Control.Domain; using MES.Production.Service.IService; using Ant.Service.Common; using System.Text.RegularExpressions; using Ant.Service.Common.Enums; using ChangFa.Machinery.WebPage.Controllers; namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers { /// /// 类描述:对模块权限按钮的管理 /// 创建标识:add by 季健国 2013-7-24 10:12 /// public class PermissionController : BaseController { #region 声明容器 ISystemManage SystemManage { get; set; } IPermissionManage PermissionManage { get; set; } IModuleManage ModuleManage { get; set; } IUserPermissionManage UserPermissionManage { get; set; } IRolePermissionManage RolePermissionManage { get; set; } ICodeManage CodeManage { get; set; } #endregion #region 基本视图 /// /// 加载导航页 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] public ActionResult Home() { try { ViewData["system"] = this.SystemManage.LoadAll(null).OrderBy(p => p.CREATEDATE).ToList(); } catch (Exception e) { WriteLog(enumOperator.Select, "对模块权限按钮的管理加载导航页:", e); } return View(); } /// /// 加载主页 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] public ActionResult Index() { try { var moduleId = Request.QueryString["moduleId"] ?? (Request["moduleId"] ?? ""); if (!string.IsNullOrEmpty(moduleId)) { int newmoduleid = int.Parse(moduleId); //模块信息 var module = this.ModuleManage.Get(p => p.ID == newmoduleid); //绑定列表 var query = this.PermissionManage.LoadAll(p => p.MODULEID == module.ID); if (!string.IsNullOrEmpty(keywords)) { query = query.Where(p => p.NAME.Contains(keywords)); } var result = query.OrderBy(p => p.SHOWORDER).ToList(); ViewBag.Search = base.keywords; ViewBag.Module = module; return View(result); } return View(); } catch(Exception e) { WriteLog(enumOperator.Select, "对模块权限按钮的管理加载主页:", e); throw e.InnerException; } } /// /// 加载详情 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Detail")] public ActionResult Detail(int? id) { try { var _entity = this.PermissionManage.Get(p => p.ID == id) ?? new SYS_PERMISSION(); var moduleId = Request.QueryString["moduleId"]; if (!string.IsNullOrEmpty(moduleId)) { int newmoduleid = int.Parse(moduleId); _entity.MODULEID = newmoduleid; } ViewData["pervalue"] = this.CodeManage.GetCode("ROLEVALUE"); return View(_entity); } catch (Exception e) { WriteLog(enumOperator.Select, "对模块权限按钮的管理加载详情:", e); throw e.InnerException; } } /// /// 保存权限 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Add,Edit")] public ActionResult Save(SYS_PERMISSION entity) { bool isEdit = false; JsonHelper json = new JsonHelper() { Msg = "保存权限成功", Status = "n" }; try { if (entity != null) { if (System.Text.Encoding.GetEncoding("gb2312").GetBytes(entity.NAME.Trim()).Length > 50) { json.Msg = "权限的名称长度不能超过50个字符"; return Json(json); } var _entity = new SYS_PERMISSION(); entity.ICON = Request.Form["ICON"]; var nextpervalue = Request.Form["NEXTPERVALUE"]; if (!string.IsNullOrEmpty(nextpervalue)) { if (!Regex.IsMatch(nextpervalue, @"^[A-Za-z0-9]{1,20}$")) { json.Msg = "权限值只能以英文数字组成,长度不能超过20个字符"; return Json(json); } entity.PERVALUE = nextpervalue; } //添加 if (entity.ID <= 0) { _entity = entity; _entity.CREATEDATE = DateTime.Now; _entity.UPDATEDATE = DateTime.Now; _entity.UPDATEUSER = this.CurrentUser.Name; _entity.CREATEUSER = this.CurrentUser.Name; } else //编辑 { _entity = this.PermissionManage.Get(p => p.ID == entity.ID); entity.CREATEUSER = _entity.CREATEUSER; entity.CREATEDATE = _entity.CREATEDATE; entity.UPDATEUSER = this.CurrentUser.Name; entity.UPDATEDATE = DateTime.Now; _entity = entity; isEdit = true; } if (!this.PermissionManage.IsExist(p => p.NAME.Equals(_entity.NAME) && p.ID != _entity.ID && p.MODULEID == _entity.MODULEID)) { if (PermissionManage.SaveOrUpdate(_entity, isEdit)) { json.Status = "y"; } else { json.Msg = "保存失败"; } } else { json.Msg = "权限" + _entity.NAME + "同一模块下已存在,不能重复添加"; } } else { json.Msg = "未找到要保存的权限记录"; } if (isEdit) { WriteLog(enumOperator.Edit, "修改权限,结果:" + json.Msg, enumLog4net.INFO); } else { WriteLog(enumOperator.Add, "添加权限,结果:" + json.Msg, enumLog4net.INFO); } } catch(Exception e) { json.Msg = "保存权限发生内部错误!"; WriteLog(enumOperator.None, "对模块权限按钮的管理保存权限:", e); } return Json(json); } /// /// 删除权限 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Remove")] public ActionResult Delete(string idList) { var json = new JsonHelper() { Msg = "删除权限成功", Status = "n" }; try { if (!string.IsNullOrEmpty(idList)) { var idList1 = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList(); //判断查找角色是否调用 if (!this.RolePermissionManage.IsExist(p => idList1.Any(e => e == p.PERMISSIONID))) { //判断查找用户是否调用 if (!this.UserPermissionManage.IsExist(p => idList1.Any(e => e == p.FK_PERMISSIONID))) { this.PermissionManage.Delete(p => idList1.Any(e => e == p.ID)); json.Status = "y"; } else { json.Msg = "有用户正在使用该权限,不能删除!"; } } else { json.Msg = "有角色正在使用该权限,不能删除!"; } } else { json.Msg = "未找到要删除的权限记录"; } WriteLog(enumOperator.Remove, "删除权限,结果:" + json.Msg, enumLog4net.WARN); } catch (Exception e) { json.Msg = e.InnerException.Message; WriteLog(enumOperator.Remove, "对模块权限按钮的管理删除权限:", e); } return Json(json); } #endregion #region 初始化权限 /// /// 初始化权限,默认增删改查详情 /// 模块ID /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Reset")] public ActionResult Reset(string id) { var json = new JsonHelper() { Status = "n", Msg = "初始化完毕" }; try { if (string.IsNullOrEmpty(id) || !Regex.IsMatch(id,@"^\d+$")) { json.Msg = "模块参数错误"; WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } int newid=int.Parse(id); if (this.PermissionManage.IsExist(p => p.MODULEID == newid)) { json.Msg = "该模块已存在权限,无法初始化"; WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } var per = new string[] { "查看,View", "列表,List", "详情,Detail", "添加,Add", "修改,Edit", "删除,Remove" }; var list = new List(); foreach (var item in per) { list.Add(new SYS_PERMISSION() { CREATEDATE = DateTime.Now, CREATEUSER = this.CurrentUser.Name, NAME = item.Split(',')[0], PERVALUE = item.Split(',')[1], UPDATEDATE = DateTime.Now, UPDATEUSER = this.CurrentUser.Name, MODULEID = newid, SHOWORDER = 0 }); } if (this.PermissionManage.SaveList(list) > 0) { json.Status = "y"; } else { json.Msg = "初始化失败"; } WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.INFO); } catch (Exception e) { json.Msg = e.InnerException.Message; WriteLog(enumOperator.Allocation, "对模块权限按钮的管理初始化权限:", e); } return Json(json); } #endregion #region 其他模块调用 /// /// 角色分配权限 /// [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Allocation")] public ActionResult PerAllocation() { //用户或角色ID string id = Request["id"]; //权限类型,user/role string tp = Request["tp"]; if (string.IsNullOrEmpty(tp)) { return Content(""); } if (string.IsNullOrEmpty(id)) { return Content(""); } string sys = Request["System"]; string search = Request["Search"]; ViewData["PermissionType"] = tp; ViewData["objId"] = id; ViewData["Systemlist"] = this.SystemManage.LoadSystemInfo(); ViewData["System"] = sys; ViewData["Search"] = search; if (string.IsNullOrEmpty(sys)) { sys = siteId; } //获取模块 var moduleList = this.ModuleManage.RecursiveModule(this.ModuleManage.LoadAll(p => p.FK_BELONGSYSTEM == sys).ToList()); if (!string.IsNullOrEmpty(search)) { moduleList = moduleList.Where(p => p.NAME.Contains(search)).ToList(); } ViewData["ModuleList"] = JsonConverter.JsonClass(moduleList.Select(p => new { p.ID, MODULENAME = GetModuleName(p.NAME, p.LEVELS), p.ICON, p.PARENTID,p.LEVELS })); //获取权限 var moduleId = moduleList.Select(p => p.ID).ToList(); ViewData["PermissionList"] = this.PermissionManage.LoadAll(p => moduleId.Any(e => e == p.MODULEID)).ToList(); //根据类型获取用户/角色已选中的权限 var selectper = new List(); int newid = int.Parse(id); if (tp == "user") { selectper = this.UserPermissionManage.LoadAll(p => p.FK_USERID == newid) .Select(p => p.FK_PERMISSIONID) .Cast() .ToList(); } else if (tp == "role") { selectper = this.RolePermissionManage.LoadAll(p => p.ROLEID == newid) .Select(p => p.PERMISSIONID) .Cast() .ToList(); } ViewData["selectper"] = selectper; return View(); } /// /// 设置角色权限 /// public ActionResult SaveAllocation() { var json = new JsonHelper() { Msg = "分配权限完毕", Status = "n" }; //类型 string tp = Request.Form["tp"]; //对象ID string id = Request.Form["id"]; //系统ID string sys = Request.Form["system"]; //权限ID集合 string perid = Request.Form["perid"]; if (string.IsNullOrEmpty(id)) { json.Msg = "未要分配权限的对象"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } if (string.IsNullOrEmpty(tp)) { json.Msg = "未要分配权限的类型"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } perid = perid.Trim(','); try { if (tp == "user") { if (!this.UserPermissionManage.SetUserPermission(int.Parse(id), perid, sys)) { json.Msg = "保存失败"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } } else if (tp == "role") { if (!this.RolePermissionManage.SetRolePermission(int.Parse(id), perid, sys)) { json.Msg = "保存失败"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); } } json.Status = "y"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.INFO); } catch (Exception e) { json.Msg = "设置角色权限发生内部错误!"; WriteLog(enumOperator.Allocation, "设置角色权限:", e); } return Json(json); } /// /// 获取模块与权限导航树 /// public ActionResult GetTree() { string perIds = Request.Form["perIds"]; var json = new JsonHelper() { Status = "y", Msg = "Success" }; //所有可显示的模块 var module = this.ModuleManage.LoadAll(p => p.ISSHOW == 1).ToList(); if (module.Count > 0) { #region 基本模块权限 var moduleId = module.Select(p => p.ID).ToList(); //通过模块获取模块的所有权限 var permission = this.PermissionManage.LoadAll(p => moduleId.Any(e => e == p.MODULEID)).ToList(); //构造模块与权限的集合 var result = new List(); result.AddRange(module.Select(p => new { id = p.ID, name = p.NAME, pId = p.PARENTID, open = true })); if (!string.IsNullOrEmpty(perIds)) { result.AddRange(permission.Select(p => new { id = p.ID, name = p.NAME, pId = p.MODULEID, checkeds = perIds.Split(',').Any(t => t == p.ID.ToString()) })); } else { result.AddRange(permission.Select(p => new { id = p.ID, name = p.NAME, pId = p.MODULEID, })); } #endregion json.Data = JsonConverter.Serialize(result).Replace("checkeds", "checked"); } return Json(json); } /// /// 显示错层方法 /// private object GetModuleName(string name, decimal? level) { if (level > 0) { string nbsp = "  "; for (int i = 0; i < level; i++) { nbsp += "  "; } name = nbsp + " |--" + name; } return name; } #endregion } }