AuthorizeService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using LeaRun.Application.Code;
  2. using LeaRun.Application.Entity.AuthorizeManage;
  3. using LeaRun.Application.Entity.AuthorizeManage.ViewModel;
  4. using LeaRun.Application.Entity.BaseManage;
  5. using LeaRun.Application.IService.AuthorizeManage;
  6. using LeaRun.Data;
  7. using LeaRun.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 LeaRun.Application.Service.AuthorizeManage
  15. {
  16. /// <summary>
  17. /// 版 本
  18. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  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. if (userId == "62bd9a97-075a-401b-a453-a7e58b13d18f")
  34. {
  35. strSql.Append(@"SELECT *
  36. FROM Base_Module
  37. WHERE EnabledMark = 1 AND DeleteMark = 0 and IsClient =0 Order By SortCode");
  38. }
  39. else
  40. {
  41. strSql.Append(@"SELECT *
  42. FROM Base_Module
  43. WHERE ModuleId IN (
  44. SELECT ItemId
  45. FROM Base_Authorize
  46. WHERE ItemType = 1
  47. AND ( ObjectId IN (
  48. SELECT ObjectId
  49. FROM Base_UserRelation
  50. WHERE UserId = @UserId ) )
  51. OR (ItemType = 1 and ObjectId = @UserId) )
  52. AND EnabledMark = 1 AND DeleteMark = 0 and IsClient =0 Order By SortCode");
  53. }
  54. DbParameter[] parameter =
  55. {
  56. DbParameters.CreateDbParameter("@UserId",userId)
  57. };
  58. return this.BaseRepository().FindList<ModuleEntity>(strSql.ToString(), parameter);
  59. }
  60. /// <summary>
  61. /// 获取授权功能菜单(重载)
  62. /// </summary>
  63. /// <param name="userId">用户Id</param>
  64. /// <returns></returns>
  65. public IEnumerable<ModuleEntity> GetModuleListN(string userId)
  66. {
  67. StringBuilder strSql = new StringBuilder();
  68. if (userId == "62bd9a97-075a-401b-a453-a7e58b13d18f")
  69. {
  70. strSql.Append(@"SELECT *
  71. FROM Base_Module
  72. WHERE EnabledMark = 1 AND DeleteMark = 0 Order By SortCode");
  73. }
  74. else
  75. {
  76. strSql.Append(@"SELECT *
  77. FROM Base_Module
  78. WHERE ModuleId IN (
  79. SELECT ItemId
  80. FROM Base_Authorize
  81. WHERE ItemType = 1
  82. AND ( ObjectId IN (
  83. SELECT ObjectId
  84. FROM Base_UserRelation
  85. WHERE UserId = @UserId ) )
  86. OR (ItemType = 1 and ObjectId = @UserId) )
  87. AND EnabledMark = 1 AND DeleteMark = 0 Order By SortCode");
  88. }
  89. DbParameter[] parameter =
  90. {
  91. DbParameters.CreateDbParameter("@UserId",userId)
  92. };
  93. return this.BaseRepository().FindList<ModuleEntity>(strSql.ToString(), parameter);
  94. }
  95. /// <summary>
  96. /// 获取授权功能按钮
  97. /// </summary>
  98. /// <param name="userId">用户Id</param>
  99. /// <returns></returns>
  100. public IEnumerable<ModuleButtonEntity> GetModuleButtonList(string userId)
  101. {
  102. StringBuilder strSql = new StringBuilder();
  103. strSql.Append(@"SELECT *
  104. FROM Base_ModuleButton
  105. WHERE ModuleButtonId IN (
  106. SELECT ItemId
  107. FROM Base_Authorize
  108. WHERE ItemType = 2
  109. AND ( ObjectId IN (
  110. SELECT ObjectId
  111. FROM Base_UserRelation
  112. WHERE UserId = @UserId ) )
  113. OR (ItemType = 2 and ObjectId = @UserId) ) Order By SortCode");
  114. DbParameter[] parameter =
  115. {
  116. DbParameters.CreateDbParameter("@UserId",userId)
  117. };
  118. return this.BaseRepository().FindList<ModuleButtonEntity>(strSql.ToString(), parameter);
  119. }
  120. /// <summary>
  121. /// 获取授权功能视图
  122. /// </summary>
  123. /// <param name="userId">用户Id</param>
  124. /// <returns></returns>
  125. public IEnumerable<ModuleColumnEntity> GetModuleColumnList(string userId)
  126. {
  127. StringBuilder strSql = new StringBuilder();
  128. strSql.Append(@"SELECT *
  129. FROM Base_ModuleColumn
  130. WHERE ModuleColumnId IN (
  131. SELECT ItemId
  132. FROM Base_Authorize
  133. WHERE ItemType = 3
  134. AND ( ObjectId IN (
  135. SELECT ObjectId
  136. FROM Base_UserRelation
  137. WHERE UserId = @UserId ) )
  138. OR (ItemType = 3 and ObjectId = @UserId) ) Order By SortCode");
  139. DbParameter[] parameter =
  140. {
  141. DbParameters.CreateDbParameter("@UserId",userId)
  142. };
  143. return this.BaseRepository().FindList<ModuleColumnEntity>(strSql.ToString(), parameter);
  144. }
  145. /// <summary>
  146. /// 获取授权功能Url、操作Url
  147. /// </summary>
  148. /// <param name="userId">用户Id</param>
  149. /// <returns></returns>
  150. public IEnumerable<AuthorizeUrlModel> GetUrlList(string userId)
  151. {
  152. StringBuilder strSql = new StringBuilder();
  153. strSql.Append(@"SELECT ModuleId AS AuthorizeId ,
  154. ModuleId ,
  155. UrlAddress ,
  156. FullName
  157. FROM Base_Module
  158. WHERE ModuleId IN (
  159. SELECT ItemId
  160. FROM Base_Authorize
  161. WHERE ItemType = 1
  162. AND ( ObjectId IN (
  163. SELECT ObjectId
  164. FROM Base_UserRelation
  165. WHERE UserId = @UserId ) )
  166. OR (ItemType = 1 and ObjectId = @UserId) )
  167. AND EnabledMark = 1
  168. AND DeleteMark = 0
  169. AND IsMenu = 1
  170. AND IsClient =0
  171. AND UrlAddress IS NOT NULL
  172. UNION
  173. SELECT ModuleButtonId AS AuthorizeId ,
  174. ModuleId ,
  175. ActionAddress AS UrlAddress ,
  176. FullName
  177. FROM Base_ModuleButton
  178. WHERE ModuleButtonId IN (
  179. SELECT ItemId
  180. FROM Base_Authorize
  181. WHERE ItemType = 2
  182. AND ( ObjectId IN (
  183. SELECT ObjectId
  184. FROM Base_UserRelation
  185. WHERE UserId = @UserId ) )
  186. OR (ItemType = 2 and ObjectId = @UserId) )
  187. AND ActionAddress IS NOT NULL");
  188. DbParameter[] parameter =
  189. {
  190. DbParameters.CreateDbParameter("@UserId",userId)
  191. };
  192. return this.BaseRepository().FindList<AuthorizeUrlModel>(strSql.ToString(), parameter);
  193. }
  194. /// <summary>
  195. /// 获取关联用户关系
  196. /// </summary>
  197. /// <param name="userId">用户Id</param>
  198. /// <returns></returns>
  199. public IEnumerable<UserRelationEntity> GetUserRelationList(string userId)
  200. {
  201. return this.BaseRepository().IQueryable<UserRelationEntity>(t => t.UserId == userId);
  202. }
  203. /// <summary>
  204. /// 获得权限范围用户ID
  205. /// </summary>
  206. /// <param name="operators">当前登陆用户信息</param>
  207. /// <param name="isWrite">可写入</param>
  208. /// <returns></returns>
  209. public string GetDataAuthorUserId(Operator operators, bool isWrite = false)
  210. {
  211. string userIdList = GetDataAuthor(operators, isWrite);
  212. if (userIdList == "")
  213. {
  214. return "";
  215. }
  216. IRepository db = new RepositoryFactory().BaseRepository();
  217. string userId = operators.UserId;
  218. List<UserEntity> userList = db.FindList<UserEntity>(userIdList).ToList();
  219. StringBuilder userSb = new StringBuilder("");
  220. if (userList != null)
  221. {
  222. int a = 0;
  223. foreach (var item in userList)
  224. {
  225. userSb.Append(item.UserId);
  226. if (a<userList.Count-1)
  227. {
  228. userSb.Append(",");
  229. }
  230. a++;
  231. }
  232. }
  233. return userSb.ToString();
  234. }
  235. /// <summary>
  236. /// 获得可读数据权限范围SQL
  237. /// </summary>
  238. /// <param name="operators">当前登陆用户信息</param>
  239. /// <param name="isWrite">可写入</param>
  240. /// <returns></returns>
  241. public string GetDataAuthor(Operator operators, bool isWrite = false)
  242. {
  243. //如果是系统管理员直接给所有数据权限
  244. if (operators.IsSystem)
  245. {
  246. return "";
  247. }
  248. IRepository db = new RepositoryFactory().BaseRepository();
  249. string userId = operators.UserId;
  250. StringBuilder whereSb = new StringBuilder(" select UserId from Base_user where 1=1 ");
  251. string strAuthorData = "";
  252. if (isWrite)
  253. {
  254. strAuthorData = @" SELECT *
  255. FROM Base_AuthorizeData
  256. WHERE IsRead=0 AND
  257. ObjectId IN (
  258. SELECT ObjectId
  259. FROM Base_UserRelation
  260. WHERE UserId =@UserId) or ObjectId =@UserId";
  261. }
  262. else
  263. {
  264. strAuthorData = @" SELECT *
  265. FROM Base_AuthorizeData
  266. WHERE
  267. ObjectId IN (
  268. SELECT ObjectId
  269. FROM Base_UserRelation
  270. WHERE UserId =@UserId) or ObjectId =@UserId";
  271. }
  272. DbParameter[] parameter =
  273. {
  274. DbParameters.CreateDbParameter("@UserId",userId),
  275. };
  276. whereSb.Append(string.Format("AND( UserId ='{0}'", userId));
  277. IEnumerable<AuthorizeDataEntity> listAuthorizeData = db.FindList<AuthorizeDataEntity>(strAuthorData, parameter);
  278. foreach (AuthorizeDataEntity item in listAuthorizeData)
  279. {
  280. switch (item.AuthorizeType)
  281. {
  282. //0代表最大权限
  283. case 0://
  284. return "";
  285. //case -1://本人
  286. // whereSb.Append("");
  287. // break;
  288. //本人及下属
  289. case -2://
  290. whereSb.Append(string.Format(" OR ManagerId ='{0}'", userId));
  291. break;
  292. case -3:
  293. whereSb.Append(string.Format(@" or DepartmentId = ( SELECT DepartmentId
  294. FROM Base_User
  295. WHERE UserId ='{0}'
  296. )", userId));
  297. break;
  298. case -4:
  299. whereSb.Append(string.Format(@" or OrganizeId = ( SELECT OrganizeId
  300. FROM Base_User
  301. WHERE UserId ='{0}'
  302. )", userId));
  303. break;
  304. case -5:
  305. whereSb.Append(string.Format(@" or DepartmentId='{0}'", item.ResourceId));
  306. break;
  307. }
  308. }
  309. whereSb.Append(")");
  310. return whereSb.ToString();
  311. }
  312. }
  313. }