123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- 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
- {
- /// <summary>
- /// 类描述:对模块权限按钮的管理
- /// 创建标识:add by 季健国 2013-7-24 10:12
- /// </summary>
- 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 基本视图
- /// <summary>
- /// 加载导航页
- /// </summary>
- [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();
- }
- /// <summary>
- /// 加载主页
- /// </summary>
- [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;
- }
- }
- /// <summary>
- /// 加载详情
- /// </summary>
- [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;
- }
- }
- /// <summary>
- /// 保存权限
- /// </summary>
- [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);
- }
- /// <summary>
- /// 删除权限
- /// </summary>
- [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 初始化权限
- /// <summary>
- /// 初始化权限,默认增删改查详情
- /// <param name="id">模块ID</param>
- /// </summary>
- [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<SYS_PERMISSION>();
- 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 其他模块调用
- /// <summary>
- /// 角色分配权限
- /// </summary>
- [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("<script>alert('未接收到需要分配权限的类型')</script>");
- }
- if (string.IsNullOrEmpty(id))
- {
- return Content("<script>alert('未接收到需要分配权限的对象')</script>");
- }
- 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<string>();
- int newid = int.Parse(id);
- if (tp == "user")
- {
- selectper =
- this.UserPermissionManage.LoadAll(p => p.FK_USERID == newid)
- .Select(p => p.FK_PERMISSIONID)
- .Cast<string>()
- .ToList();
- }
- else if (tp == "role")
- {
- selectper =
- this.RolePermissionManage.LoadAll(p => p.ROLEID == newid)
- .Select(p => p.PERMISSIONID)
- .Cast<string>()
- .ToList();
- }
- ViewData["selectper"] = selectper;
- return View();
- }
- /// <summary>
- /// 设置角色权限
- /// </summary>
- 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);
- }
- /// <summary>
- /// 获取模块与权限导航树
- /// </summary>
- 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<object>();
- 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);
- }
- /// <summary>
- /// 显示错层方法
- /// </summary>
- 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
- }
- }
|