AuthorizeService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using Lottomat.Application.Code;
  2. using Lottomat.Application.Entity.AuthorizeManage;
  3. using Lottomat.Application.Entity.AuthorizeManage.ViewModel;
  4. using Lottomat.Application.Entity.BaseManage;
  5. using Lottomat.Application.IService.AuthorizeManage;
  6. using Lottomat.Data;
  7. using Lottomat.Data.Repository;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data.Common;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace Lottomat.Application.Service.AuthorizeManage
  15. {
  16. /// <summary>
  17. /// 版 本
  18. /// Copyright (c) 2016-2017
  19. /// 创建人:赵轶
  20. /// 日 期:2015.12.5 22:35
  21. /// 描 述:授权认证
  22. /// </summary>
  23. public class AuthorizeService : RepositoryFactory, IAuthorizeService
  24. {
  25. /// <summary>
  26. /// 获取授权功能
  27. /// </summary>
  28. /// <param name="userId">用户Id</param>
  29. /// <returns></returns>
  30. public IEnumerable<ModuleEntity> GetModuleList(string userId)
  31. {
  32. StringBuilder strSql = new StringBuilder();
  33. strSql.Append(@"SELECT *
  34. FROM Base_Module
  35. WHERE ModuleId IN (
  36. SELECT ItemId
  37. FROM Base_Authorize
  38. WHERE ItemType = 1
  39. AND ( ObjectId IN (
  40. SELECT ObjectId
  41. FROM Base_UserRelation
  42. WHERE UserId = @UserId ) )
  43. OR ObjectId = @UserId )
  44. AND EnabledMark = 1 AND DeleteMark = 0 Order By SortCode");
  45. DbParameter[] parameter =
  46. {
  47. DbParameters.CreateDbParameter("@UserId",userId)
  48. };
  49. return this.BaseRepository().FindList<ModuleEntity>(strSql.ToString(), parameter);
  50. }
  51. /// <summary>
  52. /// 获取授权功能按钮
  53. /// </summary>
  54. /// <param name="userId">用户Id</param>
  55. /// <returns></returns>
  56. public IEnumerable<ModuleButtonEntity> GetModuleButtonList(string userId)
  57. {
  58. StringBuilder strSql = new StringBuilder();
  59. strSql.Append(@"SELECT *
  60. FROM Base_ModuleButton
  61. WHERE ModuleButtonId IN (
  62. SELECT ItemId
  63. FROM Base_Authorize
  64. WHERE ItemType = 2
  65. AND ( ObjectId IN (
  66. SELECT ObjectId
  67. FROM Base_UserRelation
  68. WHERE UserId = @UserId ) )
  69. OR ObjectId = @UserId ) Order By SortCode");
  70. DbParameter[] parameter =
  71. {
  72. DbParameters.CreateDbParameter("@UserId",userId)
  73. };
  74. return this.BaseRepository().FindList<ModuleButtonEntity>(strSql.ToString(), parameter);
  75. }
  76. /// <summary>
  77. /// 获取授权功能视图
  78. /// </summary>
  79. /// <param name="userId">用户Id</param>
  80. /// <returns></returns>
  81. public IEnumerable<ModuleColumnEntity> GetModuleColumnList(string userId)
  82. {
  83. StringBuilder strSql = new StringBuilder();
  84. strSql.Append(@"SELECT *
  85. FROM Base_ModuleColumn
  86. WHERE ModuleColumnId IN (
  87. SELECT ItemId
  88. FROM Base_Authorize
  89. WHERE ItemType = 3
  90. AND ( ObjectId IN (
  91. SELECT ObjectId
  92. FROM Base_UserRelation
  93. WHERE UserId = @UserId ) )
  94. OR ObjectId = @UserId ) Order By SortCode");
  95. DbParameter[] parameter =
  96. {
  97. DbParameters.CreateDbParameter("@UserId",userId)
  98. };
  99. return this.BaseRepository().FindList<ModuleColumnEntity>(strSql.ToString(), parameter);
  100. }
  101. /// <summary>
  102. /// 获取授权功能Url、操作Url
  103. /// </summary>
  104. /// <param name="userId">用户Id</param>
  105. /// <returns></returns>
  106. public IEnumerable<AuthorizeUrlModel> GetUrlList(string userId)
  107. {
  108. StringBuilder strSql = new StringBuilder();
  109. strSql.Append(@"SELECT ModuleId AS AuthorizeId ,
  110. ModuleId ,
  111. UrlAddress ,
  112. FullName
  113. FROM Base_Module
  114. WHERE ModuleId IN (
  115. SELECT ItemId
  116. FROM Base_Authorize
  117. WHERE ItemType = 1
  118. AND ( ObjectId IN (
  119. SELECT ObjectId
  120. FROM Base_UserRelation
  121. WHERE UserId = @UserId ) )
  122. OR ObjectId = @UserId )
  123. AND EnabledMark = 1
  124. AND DeleteMark = 0
  125. AND IsMenu = 1
  126. AND UrlAddress IS NOT NULL
  127. UNION
  128. SELECT ModuleButtonId AS AuthorizeId ,
  129. ModuleId ,
  130. ActionAddress AS UrlAddress ,
  131. FullName
  132. FROM Base_ModuleButton
  133. WHERE ModuleButtonId IN (
  134. SELECT ItemId
  135. FROM Base_Authorize
  136. WHERE ItemType = 2
  137. AND ( ObjectId IN (
  138. SELECT ObjectId
  139. FROM Base_UserRelation
  140. WHERE UserId = @UserId ) )
  141. OR ObjectId = @UserId )
  142. AND ActionAddress IS NOT NULL");
  143. DbParameter[] parameter =
  144. {
  145. DbParameters.CreateDbParameter("@UserId",userId)
  146. };
  147. return this.BaseRepository().FindList<AuthorizeUrlModel>(strSql.ToString(), parameter);
  148. }
  149. /// <summary>
  150. /// 获取关联用户关系
  151. /// </summary>
  152. /// <param name="userId">用户Id</param>
  153. /// <returns></returns>
  154. public IEnumerable<UserRelationEntity> GetUserRelationList(string userId)
  155. {
  156. return this.BaseRepository().IQueryable<UserRelationEntity>(t => t.UserId == userId);
  157. }
  158. /// <summary>
  159. /// 获得权限范围用户ID
  160. /// </summary>
  161. /// <param name="operators">当前登陆用户信息</param>
  162. /// <param name="isWrite">可写入</param>
  163. /// <returns></returns>
  164. public string GetDataAuthorUserId(Operator operators, bool isWrite = false)
  165. {
  166. string userIdList = GetDataAuthor(operators, isWrite);
  167. if (userIdList == "")
  168. {
  169. return "";
  170. }
  171. IRepository db = new RepositoryFactory().BaseRepository();
  172. string userId = operators.UserId;
  173. List<UserEntity> userList = db.FindList<UserEntity>(userIdList).ToList();
  174. StringBuilder userSb = new StringBuilder("");
  175. if (userList != null)
  176. {
  177. foreach (var item in userList)
  178. {
  179. userSb.Append(item.UserId);
  180. userSb.Append(",");
  181. }
  182. }
  183. return userSb.ToString();
  184. }
  185. /// <summary>
  186. /// 获得可读数据权限范围SQL
  187. /// </summary>
  188. /// <param name="operators">当前登陆用户信息</param>
  189. /// <param name="isWrite">可写入</param>
  190. /// <returns></returns>
  191. public string GetDataAuthor(Operator operators, bool isWrite = false)
  192. {
  193. //如果是系统管理员直接给所有数据权限
  194. if (operators.IsSystem)
  195. {
  196. return "";
  197. }
  198. IRepository db = new RepositoryFactory().BaseRepository();
  199. string userId = operators.UserId;
  200. StringBuilder whereSb = new StringBuilder(" SELECT UserId from Base_User where 1=1 ");
  201. string strAuthorData = "";
  202. if (isWrite)
  203. {
  204. strAuthorData = @" SELECT *
  205. FROM Base_AuthorizeData
  206. WHERE IsRead=0 AND
  207. ObjectId IN (
  208. SELECT ObjectId
  209. FROM Base_UserRelation
  210. WHERE UserId =@UserId)";
  211. }
  212. else
  213. {
  214. strAuthorData = @" SELECT *
  215. FROM Base_AuthorizeData
  216. WHERE
  217. ObjectId IN (
  218. SELECT ObjectId
  219. FROM Base_UserRelation
  220. WHERE UserId =@UserId)";
  221. }
  222. DbParameter[] parameter =
  223. {
  224. DbParameters.CreateDbParameter("@UserId",userId),
  225. };
  226. whereSb.Append(string.Format("AND( UserId ='{0}'", userId));
  227. IEnumerable<AuthorizeDataEntity> listAuthorizeData = db.FindList<AuthorizeDataEntity>(strAuthorData, parameter);
  228. foreach (AuthorizeDataEntity item in listAuthorizeData)
  229. {
  230. switch (item.AuthorizeType)
  231. {
  232. //0代表最大权限
  233. case 0://
  234. return "";
  235. //本人及下属
  236. case -2://
  237. whereSb.Append(" OR ManagerId ='{0}'");
  238. break;
  239. case -3:
  240. whereSb.Append(@" OR DepartmentId = ( SELECT DepartmentId
  241. FROM Base_User
  242. WHERE UserId ='{0}'
  243. )");
  244. break;
  245. case -4:
  246. whereSb.Append(@" OR OrganizeId = ( SELECT OrganizeId
  247. FROM Base_User
  248. WHERE UserId ='{0}'
  249. )");
  250. break;
  251. case -5:
  252. whereSb.Append(string.Format(@" OR DepartmentId='{1}' OR OrganizeId='{1}'", userId, item.ResourceId));
  253. break;
  254. }
  255. }
  256. whereSb.Append(")");
  257. return whereSb.ToString();
  258. }
  259. }
  260. }