PermissionController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Central.Control.Domain;
  7. using MES.Production.Service.IService;
  8. using Ant.Service.Common;
  9. using System.Text.RegularExpressions;
  10. using Ant.Service.Common.Enums;
  11. using ChangFa.Machinery.WebPage.Controllers;
  12. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  13. {
  14. /// <summary>
  15. /// 类描述:对模块权限按钮的管理
  16. /// 创建标识:add by 季健国 2013-7-24 10:12
  17. /// </summary>
  18. public class PermissionController : BaseController
  19. {
  20. #region 声明容器
  21. ISystemManage SystemManage { get; set; }
  22. IPermissionManage PermissionManage { get; set; }
  23. IModuleManage ModuleManage { get; set; }
  24. IUserPermissionManage UserPermissionManage { get; set; }
  25. IRolePermissionManage RolePermissionManage { get; set; }
  26. ICodeManage CodeManage { get; set; }
  27. #endregion
  28. #region 基本视图
  29. /// <summary>
  30. /// 加载导航页
  31. /// </summary>
  32. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")]
  33. public ActionResult Home()
  34. {
  35. try
  36. {
  37. ViewData["system"] = this.SystemManage.LoadAll(null).OrderBy(p => p.CREATEDATE).ToList();
  38. }
  39. catch (Exception e)
  40. {
  41. WriteLog(enumOperator.Select, "对模块权限按钮的管理加载导航页:", e);
  42. }
  43. return View();
  44. }
  45. /// <summary>
  46. /// 加载主页
  47. /// </summary>
  48. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")]
  49. public ActionResult Index()
  50. {
  51. try
  52. {
  53. var moduleId = Request.QueryString["moduleId"] ?? (Request["moduleId"] ?? "");
  54. if (!string.IsNullOrEmpty(moduleId))
  55. {
  56. int newmoduleid = int.Parse(moduleId);
  57. //模块信息
  58. var module = this.ModuleManage.Get(p => p.ID == newmoduleid);
  59. //绑定列表
  60. var query = this.PermissionManage.LoadAll(p => p.MODULEID == module.ID);
  61. if (!string.IsNullOrEmpty(keywords))
  62. {
  63. query = query.Where(p => p.NAME.Contains(keywords));
  64. }
  65. var result = query.OrderBy(p => p.SHOWORDER).ToList();
  66. ViewBag.Search = base.keywords;
  67. ViewBag.Module = module;
  68. return View(result);
  69. }
  70. return View();
  71. }
  72. catch(Exception e)
  73. {
  74. WriteLog(enumOperator.Select, "对模块权限按钮的管理加载主页:", e);
  75. throw e.InnerException;
  76. }
  77. }
  78. /// <summary>
  79. /// 加载详情
  80. /// </summary>
  81. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Detail")]
  82. public ActionResult Detail(int? id)
  83. {
  84. try
  85. {
  86. var _entity = this.PermissionManage.Get(p => p.ID == id) ?? new SYS_PERMISSION();
  87. var moduleId = Request.QueryString["moduleId"];
  88. if (!string.IsNullOrEmpty(moduleId))
  89. {
  90. int newmoduleid = int.Parse(moduleId);
  91. _entity.MODULEID = newmoduleid;
  92. }
  93. ViewData["pervalue"] = this.CodeManage.GetCode("ROLEVALUE");
  94. return View(_entity);
  95. }
  96. catch (Exception e)
  97. {
  98. WriteLog(enumOperator.Select, "对模块权限按钮的管理加载详情:", e);
  99. throw e.InnerException;
  100. }
  101. }
  102. /// <summary>
  103. /// 保存权限
  104. /// </summary>
  105. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Add,Edit")]
  106. public ActionResult Save(SYS_PERMISSION entity)
  107. {
  108. bool isEdit = false;
  109. JsonHelper json = new JsonHelper() { Msg = "保存权限成功", Status = "n" };
  110. try
  111. {
  112. if (entity != null)
  113. {
  114. if (System.Text.Encoding.GetEncoding("gb2312").GetBytes(entity.NAME.Trim()).Length > 50)
  115. {
  116. json.Msg = "权限的名称长度不能超过50个字符";
  117. return Json(json);
  118. }
  119. var _entity = new SYS_PERMISSION();
  120. entity.ICON = Request.Form["ICON"];
  121. var nextpervalue = Request.Form["NEXTPERVALUE"];
  122. if (!string.IsNullOrEmpty(nextpervalue))
  123. {
  124. if (!Regex.IsMatch(nextpervalue, @"^[A-Za-z0-9]{1,20}$"))
  125. {
  126. json.Msg = "权限值只能以英文数字组成,长度不能超过20个字符";
  127. return Json(json);
  128. }
  129. entity.PERVALUE = nextpervalue;
  130. }
  131. //添加
  132. if (entity.ID <= 0)
  133. {
  134. _entity = entity;
  135. _entity.CREATEDATE = DateTime.Now;
  136. _entity.UPDATEDATE = DateTime.Now;
  137. _entity.UPDATEUSER = this.CurrentUser.Name;
  138. _entity.CREATEUSER = this.CurrentUser.Name;
  139. }
  140. else //编辑
  141. {
  142. _entity = this.PermissionManage.Get(p => p.ID == entity.ID);
  143. entity.CREATEUSER = _entity.CREATEUSER;
  144. entity.CREATEDATE = _entity.CREATEDATE;
  145. entity.UPDATEUSER = this.CurrentUser.Name;
  146. entity.UPDATEDATE = DateTime.Now;
  147. _entity = entity;
  148. isEdit = true;
  149. }
  150. if (!this.PermissionManage.IsExist(p => p.NAME.Equals(_entity.NAME) && p.ID != _entity.ID && p.MODULEID == _entity.MODULEID))
  151. {
  152. if (PermissionManage.SaveOrUpdate(_entity, isEdit))
  153. {
  154. json.Status = "y";
  155. }
  156. else
  157. {
  158. json.Msg = "保存失败";
  159. }
  160. }
  161. else
  162. {
  163. json.Msg = "权限" + _entity.NAME + "同一模块下已存在,不能重复添加";
  164. }
  165. }
  166. else
  167. {
  168. json.Msg = "未找到要保存的权限记录";
  169. }
  170. if (isEdit)
  171. {
  172. WriteLog(enumOperator.Edit, "修改权限,结果:" + json.Msg, enumLog4net.INFO);
  173. }
  174. else
  175. {
  176. WriteLog(enumOperator.Add, "添加权限,结果:" + json.Msg, enumLog4net.INFO);
  177. }
  178. }
  179. catch(Exception e)
  180. {
  181. json.Msg = "保存权限发生内部错误!";
  182. WriteLog(enumOperator.None, "对模块权限按钮的管理保存权限:", e);
  183. }
  184. return Json(json);
  185. }
  186. /// <summary>
  187. /// 删除权限
  188. /// </summary>
  189. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Remove")]
  190. public ActionResult Delete(string idList)
  191. {
  192. var json = new JsonHelper() { Msg = "删除权限成功", Status = "n" };
  193. try
  194. {
  195. if (!string.IsNullOrEmpty(idList))
  196. {
  197. var idList1 = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
  198. //判断查找角色是否调用
  199. if (!this.RolePermissionManage.IsExist(p => idList1.Any(e => e == p.PERMISSIONID)))
  200. {
  201. //判断查找用户是否调用
  202. if (!this.UserPermissionManage.IsExist(p => idList1.Any(e => e == p.FK_PERMISSIONID)))
  203. {
  204. this.PermissionManage.Delete(p => idList1.Any(e => e == p.ID));
  205. json.Status = "y";
  206. }
  207. else
  208. {
  209. json.Msg = "有用户正在使用该权限,不能删除!";
  210. }
  211. }
  212. else
  213. {
  214. json.Msg = "有角色正在使用该权限,不能删除!";
  215. }
  216. }
  217. else
  218. {
  219. json.Msg = "未找到要删除的权限记录";
  220. }
  221. WriteLog(enumOperator.Remove, "删除权限,结果:" + json.Msg, enumLog4net.WARN);
  222. }
  223. catch (Exception e)
  224. {
  225. json.Msg = e.InnerException.Message;
  226. WriteLog(enumOperator.Remove, "对模块权限按钮的管理删除权限:", e);
  227. }
  228. return Json(json);
  229. }
  230. #endregion
  231. #region 初始化权限
  232. /// <summary>
  233. /// 初始化权限,默认增删改查详情
  234. /// <param name="id">模块ID</param>
  235. /// </summary>
  236. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Reset")]
  237. public ActionResult Reset(string id)
  238. {
  239. var json = new JsonHelper() { Status = "n", Msg = "初始化完毕" };
  240. try
  241. {
  242. if (string.IsNullOrEmpty(id) || !Regex.IsMatch(id,@"^\d+$"))
  243. {
  244. json.Msg = "模块参数错误";
  245. WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.ERROR);
  246. return Json(json);
  247. }
  248. int newid=int.Parse(id);
  249. if (this.PermissionManage.IsExist(p => p.MODULEID == newid))
  250. {
  251. json.Msg = "该模块已存在权限,无法初始化";
  252. WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.ERROR);
  253. return Json(json);
  254. }
  255. var per = new string[] { "查看,View", "列表,List", "详情,Detail", "添加,Add", "修改,Edit", "删除,Remove" };
  256. var list = new List<SYS_PERMISSION>();
  257. foreach (var item in per)
  258. {
  259. list.Add(new SYS_PERMISSION()
  260. {
  261. CREATEDATE = DateTime.Now,
  262. CREATEUSER = this.CurrentUser.Name,
  263. NAME = item.Split(',')[0],
  264. PERVALUE = item.Split(',')[1],
  265. UPDATEDATE = DateTime.Now,
  266. UPDATEUSER = this.CurrentUser.Name,
  267. MODULEID = newid,
  268. SHOWORDER = 0
  269. });
  270. }
  271. if (this.PermissionManage.SaveList(list) > 0)
  272. {
  273. json.Status = "y";
  274. }
  275. else
  276. {
  277. json.Msg = "初始化失败";
  278. }
  279. WriteLog(enumOperator.Allocation, "初始化权限,结果:" + json.Msg, enumLog4net.INFO);
  280. }
  281. catch (Exception e)
  282. {
  283. json.Msg = e.InnerException.Message;
  284. WriteLog(enumOperator.Allocation, "对模块权限按钮的管理初始化权限:", e);
  285. }
  286. return Json(json);
  287. }
  288. #endregion
  289. #region 其他模块调用
  290. /// <summary>
  291. /// 角色分配权限
  292. /// </summary>
  293. [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Allocation")]
  294. public ActionResult PerAllocation()
  295. {
  296. //用户或角色ID
  297. string id = Request["id"];
  298. //权限类型,user/role
  299. string tp = Request["tp"];
  300. if (string.IsNullOrEmpty(tp))
  301. {
  302. return Content("<script>alert('未接收到需要分配权限的类型')</script>");
  303. }
  304. if (string.IsNullOrEmpty(id))
  305. {
  306. return Content("<script>alert('未接收到需要分配权限的对象')</script>");
  307. }
  308. string sys = Request["System"];
  309. string search = Request["Search"];
  310. ViewData["PermissionType"] = tp;
  311. ViewData["objId"] = id;
  312. ViewData["Systemlist"] = this.SystemManage.LoadSystemInfo();
  313. ViewData["System"] = sys;
  314. ViewData["Search"] = search;
  315. if (string.IsNullOrEmpty(sys)) { sys = siteId; }
  316. //获取模块
  317. var moduleList = this.ModuleManage.RecursiveModule(this.ModuleManage.LoadAll(p => p.FK_BELONGSYSTEM == sys).ToList());
  318. if (!string.IsNullOrEmpty(search))
  319. {
  320. moduleList = moduleList.Where(p => p.NAME.Contains(search)).ToList();
  321. }
  322. ViewData["ModuleList"] = JsonConverter.JsonClass(moduleList.Select(p => new { p.ID, MODULENAME = GetModuleName(p.NAME, p.LEVELS), p.ICON, p.PARENTID,p.LEVELS }));
  323. //获取权限
  324. var moduleId = moduleList.Select(p => p.ID).ToList();
  325. ViewData["PermissionList"] = this.PermissionManage.LoadAll(p => moduleId.Any(e => e == p.MODULEID)).ToList();
  326. //根据类型获取用户/角色已选中的权限
  327. var selectper = new List<string>();
  328. int newid = int.Parse(id);
  329. if (tp == "user")
  330. {
  331. selectper =
  332. this.UserPermissionManage.LoadAll(p => p.FK_USERID == newid)
  333. .Select(p => p.FK_PERMISSIONID)
  334. .Cast<string>()
  335. .ToList();
  336. }
  337. else if (tp == "role")
  338. {
  339. selectper =
  340. this.RolePermissionManage.LoadAll(p => p.ROLEID == newid)
  341. .Select(p => p.PERMISSIONID)
  342. .Cast<string>()
  343. .ToList();
  344. }
  345. ViewData["selectper"] = selectper;
  346. return View();
  347. }
  348. /// <summary>
  349. /// 设置角色权限
  350. /// </summary>
  351. public ActionResult SaveAllocation()
  352. {
  353. var json = new JsonHelper()
  354. {
  355. Msg = "分配权限完毕",
  356. Status = "n"
  357. };
  358. //类型
  359. string tp = Request.Form["tp"];
  360. //对象ID
  361. string id = Request.Form["id"];
  362. //系统ID
  363. string sys = Request.Form["system"];
  364. //权限ID集合
  365. string perid = Request.Form["perid"];
  366. if (string.IsNullOrEmpty(id))
  367. {
  368. json.Msg = "未要分配权限的对象";
  369. WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR);
  370. return Json(json);
  371. }
  372. if (string.IsNullOrEmpty(tp))
  373. {
  374. json.Msg = "未要分配权限的类型";
  375. WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR);
  376. return Json(json);
  377. }
  378. perid = perid.Trim(',');
  379. try
  380. {
  381. if (tp == "user")
  382. {
  383. if (!this.UserPermissionManage.SetUserPermission(int.Parse(id), perid, sys)) { json.Msg = "保存失败"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); }
  384. }
  385. else if (tp == "role")
  386. {
  387. if (!this.RolePermissionManage.SetRolePermission(int.Parse(id), perid, sys)) { json.Msg = "保存失败"; WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.ERROR); return Json(json); }
  388. }
  389. json.Status = "y";
  390. WriteLog(enumOperator.Allocation, "设置角色权限,结果:" + json.Msg, enumLog4net.INFO);
  391. }
  392. catch (Exception e)
  393. {
  394. json.Msg = "设置角色权限发生内部错误!";
  395. WriteLog(enumOperator.Allocation, "设置角色权限:", e);
  396. }
  397. return Json(json);
  398. }
  399. /// <summary>
  400. /// 获取模块与权限导航树
  401. /// </summary>
  402. public ActionResult GetTree()
  403. {
  404. string perIds = Request.Form["perIds"];
  405. var json = new JsonHelper() { Status = "y", Msg = "Success" };
  406. //所有可显示的模块
  407. var module = this.ModuleManage.LoadAll(p => p.ISSHOW == 1).ToList();
  408. if (module.Count > 0)
  409. {
  410. #region 基本模块权限
  411. var moduleId = module.Select(p => p.ID).ToList();
  412. //通过模块获取模块的所有权限
  413. var permission = this.PermissionManage.LoadAll(p => moduleId.Any(e => e == p.MODULEID)).ToList();
  414. //构造模块与权限的集合
  415. var result = new List<object>();
  416. result.AddRange(module.Select(p => new
  417. {
  418. id = p.ID,
  419. name = p.NAME,
  420. pId = p.PARENTID,
  421. open = true
  422. }));
  423. if (!string.IsNullOrEmpty(perIds))
  424. {
  425. result.AddRange(permission.Select(p => new
  426. {
  427. id = p.ID,
  428. name = p.NAME,
  429. pId = p.MODULEID,
  430. checkeds = perIds.Split(',').Any(t => t == p.ID.ToString())
  431. }));
  432. }
  433. else
  434. {
  435. result.AddRange(permission.Select(p => new
  436. {
  437. id = p.ID,
  438. name = p.NAME,
  439. pId = p.MODULEID,
  440. }));
  441. }
  442. #endregion
  443. json.Data = JsonConverter.Serialize(result).Replace("checkeds", "checked");
  444. }
  445. return Json(json);
  446. }
  447. /// <summary>
  448. /// 显示错层方法
  449. /// </summary>
  450. private object GetModuleName(string name, decimal? level)
  451. {
  452. if (level > 0)
  453. {
  454. string nbsp = "&nbsp;&nbsp;";
  455. for (int i = 0; i < level; i++)
  456. {
  457. nbsp += "&nbsp;&nbsp;";
  458. }
  459. name = nbsp + " |--" + name;
  460. }
  461. return name;
  462. }
  463. #endregion
  464. }
  465. }