9
9
using Ape . Volo . Common . Extensions ;
10
10
using Ape . Volo . Common . Global ;
11
11
using Ape . Volo . Common . Helper ;
12
+ using Ape . Volo . Common . Helper . Serilog ;
12
13
using Ape . Volo . Common . Model ;
13
14
using Ape . Volo . Common . SnowflakeIdHelper ;
14
15
using Ape . Volo . Common . WebApp ;
15
16
using Ape . Volo . Entity . Base ;
17
+ using Ape . Volo . IBusiness . Interface . Permission ;
16
18
using Microsoft . Extensions . DependencyInjection ;
17
19
using Serilog ;
18
20
using SqlSugar ;
@@ -26,6 +28,8 @@ namespace Ape.Volo.Api.Extensions;
26
28
/// </summary>
27
29
public static class SqlSugarSetup
28
30
{
31
+ private static readonly ILogger Logger = SerilogManager . GetLogger ( typeof ( SqlSugarSetup ) ) ;
32
+
29
33
public static void AddSqlSugarSetup ( this IServiceCollection services , Configs configs )
30
34
{
31
35
if ( services . IsNull ( ) )
@@ -98,7 +102,7 @@ public static void AddSqlSugarSetup(this IServiceCollection services, Configs co
98
102
MoreSettings = new ConnMoreSettings
99
103
{
100
104
IsAutoRemoveDataCache = true ,
101
- SqlServerCodeFirstNvarchar = true , //sqlserver默认使用nvarchar
105
+ SqlServerCodeFirstNvarchar = true //sqlserver默认使用nvarchar
102
106
} ,
103
107
ConfigureExternalServices = new ConfigureExternalServices
104
108
{
@@ -152,6 +156,9 @@ public static void AddSqlSugarSetup(this IServiceCollection services, Configs co
152
156
//租户
153
157
sugarScopeProvider . ConfiguringTenantFilter ( ) ;
154
158
159
+ //数据权限 这个要放在最后
160
+ sugarScopeProvider . ConfiguringUserDataScopeFilter ( ) ;
161
+
155
162
#endregion
156
163
157
164
#region 读写事件
@@ -193,6 +200,8 @@ private static void DataExecuting(object value, DataFilterModel entityInfo)
193
200
rootEntity . Id = IdHelper . GetLongId ( ) ;
194
201
}
195
202
203
+ #region BaseEntity
204
+
196
205
if ( entityInfo . EntityValue is BaseEntity baseEntity )
197
206
{
198
207
switch ( entityInfo . OperationType )
@@ -212,31 +221,88 @@ private static void DataExecuting(object value, DataFilterModel entityInfo)
212
221
}
213
222
214
223
var httpUser = AutofacHelper . GetService < IHttpUser > ( ) ;
215
- if ( httpUser . IsNull ( ) ) return ;
216
- switch ( entityInfo . OperationType )
224
+ if ( httpUser . IsNotNull ( ) && ! httpUser . Account . IsNullOrEmpty ( ) )
217
225
{
218
- case DataFilterType . InsertByObject :
226
+ switch ( entityInfo . OperationType )
219
227
{
220
- if ( baseEntity . CreateBy . IsNullOrEmpty ( ) )
228
+ case DataFilterType . InsertByObject :
221
229
{
222
- baseEntity . CreateBy = httpUser . Account ;
223
- }
230
+ if ( baseEntity . CreateBy . IsNullOrEmpty ( ) )
231
+ {
232
+ baseEntity . CreateBy = httpUser . Account ;
233
+ }
224
234
225
- if ( baseEntity is ITenantEntity tenant && httpUser . TenantId > 0 )
226
- {
227
- if ( tenant . TenantId == 0 )
235
+ var tenant = baseEntity as ITenantEntity ;
236
+ if ( tenant != null && httpUser . TenantId > 0 )
228
237
{
229
- tenant . TenantId = httpUser . TenantId ;
238
+ if ( tenant . TenantId == 0 )
239
+ {
240
+ tenant . TenantId = httpUser . TenantId ;
241
+ }
230
242
}
243
+
244
+ break ;
245
+ }
246
+ case DataFilterType . UpdateByObject :
247
+ baseEntity . UpdateBy = httpUser . Account ;
248
+ break ;
249
+ }
250
+ }
251
+ }
252
+
253
+ #endregion
254
+
255
+ #region BaseEntityNoDataScope
256
+
257
+ if ( entityInfo . EntityValue is BaseEntityNoDataScope baseEntityNoDataScope )
258
+ {
259
+ switch ( entityInfo . OperationType )
260
+ {
261
+ case DataFilterType . InsertByObject :
262
+ {
263
+ if ( baseEntityNoDataScope . CreateTime == DateTime . MinValue )
264
+ {
265
+ baseEntityNoDataScope . CreateTime = DateTime . Now ;
231
266
}
232
267
233
268
break ;
234
269
}
235
270
case DataFilterType . UpdateByObject :
236
- baseEntity . UpdateBy = httpUser . Account ;
271
+ baseEntityNoDataScope . UpdateTime = DateTime . Now ;
237
272
break ;
238
273
}
274
+
275
+ var httpUser = AutofacHelper . GetService < IHttpUser > ( ) ;
276
+ if ( httpUser . IsNotNull ( ) && ! httpUser . Account . IsNullOrEmpty ( ) )
277
+ {
278
+ switch ( entityInfo . OperationType )
279
+ {
280
+ case DataFilterType . InsertByObject :
281
+ {
282
+ if ( baseEntityNoDataScope . CreateBy . IsNullOrEmpty ( ) )
283
+ {
284
+ baseEntityNoDataScope . CreateBy = httpUser . Account ;
285
+ }
286
+
287
+ var tenant = baseEntityNoDataScope as ITenantEntity ;
288
+ if ( tenant != null && httpUser . TenantId > 0 )
289
+ {
290
+ if ( tenant . TenantId == 0 )
291
+ {
292
+ tenant . TenantId = httpUser . TenantId ;
293
+ }
294
+ }
295
+
296
+ break ;
297
+ }
298
+ case DataFilterType . UpdateByObject :
299
+ baseEntityNoDataScope . UpdateBy = httpUser . Account ;
300
+ break ;
301
+ }
302
+ }
239
303
}
304
+
305
+ #endregion
240
306
}
241
307
242
308
#endregion
@@ -339,4 +405,45 @@ private static void ConfiguringTenantFilter(this SqlSugarScopeProvider db)
339
405
db . QueryFilter . AddTableFilter < ITenantEntity > ( it => it . TenantId == httpUser . TenantId ) ;
340
406
}
341
407
}
408
+
409
+ /// <summary>
410
+ /// 配置用户数据权限
411
+ /// </summary>
412
+ /// <param name="db"></param>
413
+ private static void ConfiguringUserDataScopeFilter ( this SqlSugarScopeProvider db )
414
+ {
415
+ var httpUser = AutofacHelper . GetService < IHttpUser > ( ) ;
416
+ if ( httpUser . IsNull ( ) || httpUser . Account . IsNullOrEmpty ( ) ) return ;
417
+ var dataScopeService = AutofacHelper . GetService < IDataScopeService > ( ) ;
418
+ if ( dataScopeService == null ) return ;
419
+
420
+ try
421
+ {
422
+ var accounts = AsyncHelper . RunSync ( ( ) =>
423
+ dataScopeService . GetDataScopeAccountsAsync ( httpUser . Id ) ) ;
424
+ if ( accounts . Count > 0 )
425
+ {
426
+ if ( accounts . Count == 1 )
427
+ {
428
+ if ( ! accounts [ 0 ] . Equals ( "All" ) )
429
+ {
430
+ db . QueryFilter . AddTableFilter < ICreateByEntity > ( it => it . CreateBy == accounts [ 0 ] ) ;
431
+ }
432
+ }
433
+ else
434
+ {
435
+ db . QueryFilter . AddTableFilter < ICreateByEntity > ( it => accounts . Contains ( it . CreateBy ) ) ;
436
+ }
437
+ }
438
+ else
439
+ {
440
+ db . QueryFilter . AddTableFilter < ICreateByEntity > ( it => it . CreateBy == httpUser . Account ) ;
441
+ }
442
+ }
443
+ catch ( Exception e )
444
+ {
445
+ Logger . Fatal ( "配置用户数据权限错误:\r \n " + ExceptionHelper . GetExceptionAllMsg ( e ) ) ;
446
+ db . QueryFilter . AddTableFilter < ICreateByEntity > ( it => it . CreateBy == httpUser . Account ) ;
447
+ }
448
+ }
342
449
}
0 commit comments