AuthorizeService.T.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using LeaRun.Application.Code;
  2. using LeaRun.Application.Entity.AuthorizeManage;
  3. using LeaRun.Application.Entity.BaseManage;
  4. using LeaRun.Application.IService.AuthorizeManage;
  5. using LeaRun.Data;
  6. using LeaRun.Data.Repository;
  7. using LeaRun.Util.Extension;
  8. using LeaRun.Util.WebControl;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Data.Common;
  13. using System.Linq;
  14. using System.Linq.Expressions;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace LeaRun.Application.Service.AuthorizeManage
  19. {
  20. /// <summary>
  21. /// 版 本
  22. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  23. /// 创建人:刘晓雷
  24. /// 日 期:2016.03.29 22:35
  25. /// 描 述:授权认证
  26. /// </summary>
  27. public class AuthorizeService<T> : RepositoryFactory<T>, IAuthorizeService<T> where T : class,new()
  28. {
  29. private IRepository db = new RepositoryFactory().BaseRepository();
  30. private AuthorizeService authorizeService = new AuthorizeService();
  31. #region 带权限的数据源查询
  32. public IQueryable<T> IQueryable()
  33. {
  34. if (GetReadUserId() == "")
  35. {
  36. return this.BaseRepository().IQueryable();
  37. }
  38. else
  39. {
  40. var parameter = Expression.Parameter(typeof(T), "t");
  41. var authorConditon = Expression.Constant(GetReadUserId()).Call("Contains", parameter.Property("CreateUserId"));
  42. var lambda = authorConditon.ToLambda<Func<T, bool>>(parameter);
  43. return this.BaseRepository().IQueryable(lambda);
  44. }
  45. }
  46. public IQueryable<T> IQueryable(Expression<Func<T, bool>> condition)
  47. {
  48. if (GetReadUserId() != "")
  49. {
  50. var parameter = Expression.Parameter(typeof(T), "t");
  51. var authorConditon = Expression.Constant(GetReadUserId()).Call("Contains", parameter.Property("CreateUserId"));
  52. var lambda = authorConditon.ToLambda<Func<T, bool>>(parameter);
  53. condition = condition.And(lambda);
  54. }
  55. return db.IQueryable<T>(condition);
  56. }
  57. public IEnumerable<T> FindList(Pagination pagination)
  58. {
  59. if (GetReadUserId() == "")
  60. {
  61. return this.BaseRepository().FindList(pagination);
  62. }
  63. else
  64. {
  65. var parameter = Expression.Parameter(typeof(T), "t");
  66. var authorConditon = Expression.Constant(GetReadUserId()).Call("Contains", parameter.Property("CreateUserId"));
  67. var lambda = authorConditon.ToLambda<Func<T, bool>>(parameter);
  68. return this.BaseRepository().FindList(lambda, pagination);
  69. }
  70. }
  71. public IEnumerable<T> FindList(Expression<Func<T, bool>> condition, Pagination pagination)
  72. {
  73. if (GetReadUserId() != "")
  74. {
  75. var parameter = Expression.Parameter(typeof(T), "t");
  76. var authorConditon = Expression.Constant(GetReadUserId()).Call("Contains", parameter.Property("CreateUserId"));
  77. var lambda = authorConditon.ToLambda<Func<T, bool>>(parameter);
  78. condition = condition.And(lambda);
  79. }
  80. return this.BaseRepository().FindList(condition, pagination);
  81. }
  82. public IEnumerable<T> FindList(string strSql)
  83. {
  84. strSql = strSql + (GetReadSql() == "" ? "" : string.Format("and CreateUserId in({0})", GetReadSql()));
  85. return this.BaseRepository().FindList(strSql);
  86. }
  87. public IEnumerable<T> FindList(string strSql, DbParameter[] dbParameter)
  88. {
  89. strSql = strSql + (GetReadSql() == "" ? "" : string.Format("and CreateUserId in({0})", GetReadSql()));
  90. return this.BaseRepository().FindList(strSql, dbParameter);
  91. }
  92. public IEnumerable<T> FindList(string strSql, Pagination pagination)
  93. {
  94. strSql = strSql + (GetReadSql() == "" ? "" : string.Format("and CreateUserId in({0})", GetReadSql()));
  95. return this.BaseRepository().FindList(strSql, pagination);
  96. }
  97. public IEnumerable<T> FindList(string strSql, DbParameter[] dbParameter, Pagination pagination)
  98. {
  99. strSql = strSql + (GetReadSql() == "" ? "" : string.Format("and CreateUserId in({0})", GetReadSql()));
  100. return this.BaseRepository().FindList(strSql, dbParameter, pagination);
  101. }
  102. #endregion
  103. #region 取数据权限用户
  104. private string GetReadUserId()
  105. {
  106. if (OperatorProvider.Provider.Current().IsSystem)
  107. {
  108. return "";
  109. }
  110. return OperatorProvider.Provider.Current().DataAuthorize.ReadAutorizeUserId;
  111. }
  112. private string GetReadSql()
  113. {
  114. return OperatorProvider.Provider.Current().DataAuthorize.ReadAutorize;
  115. }
  116. #endregion
  117. }
  118. }