赖素文
authored
|
1
2
3
4
5
6
7
8
9
|
// ***********************************************************************
// <summary>
// 用户权限策略工厂
//</summary>
// ***********************************************************************
using Hh.Mes.POJO.Entity;
using Hh.Mes.Service.Repository;
|
唐召明
authored
|
10
11
12
|
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Text.Json;
|
赖素文
authored
|
13
14
15
16
17
18
19
20
21
22
23
|
namespace Hh.Mes.Service.SystemAuth
{
/// <summary>
/// 加载用户所有可访问的资源/机构/模块
/// </summary>
public class AuthContextFactory : RepositorySqlSugar<SysUser>
{
private SystemAuthStrategy _systemAuth;
private NormalAuthStrategy _normalAuthStrategy;
|
唐召明
authored
|
24
|
private readonly IDistributedCache _cache;
|
赖素文
authored
|
25
|
|
唐召明
authored
|
26
|
public AuthContextFactory(SystemAuthStrategy sysStrategy, NormalAuthStrategy normalAuthStrategy, IDistributedCache cache)
|
赖素文
authored
|
27
28
29
|
{
_systemAuth = sysStrategy;
_normalAuthStrategy = normalAuthStrategy;
|
唐召明
authored
|
30
|
_cache = cache;
|
赖素文
authored
|
31
32
33
34
35
36
37
38
39
40
41
42
|
}
/// <summary>
/// 生成授权信息
/// System给开发者用的,普通管理员只能看到System授权过的模块,普通管理员即使能新增模块,保存后也看不到。
/// 想了很久才明白,普通管理员是用于授权给其他用户,模块的增加、修改、删除只能由System来进行。
/// 组织管理,这个确实架构有天生的bug,组织本来就该由管理员设置,居然设置后不能看到。
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public AuthStrategyContext GetAuthStrategyContext(string username)
{
|
唐召明
authored
|
43
44
45
46
47
|
//var cl = new RedisBase();
var authStrategyBytes = _cache.Get(username);
if (authStrategyBytes != null)
|
赖素文
authored
|
48
|
{
|
唐召明
authored
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
return JsonSerializer.Deserialize<AuthStrategyContext>(authStrategyBytes);
}
var authStrategy = new AuthStrategyContext();
var user = Context.Queryable<SysUser>().First(t => t.Account == username);
if (user != null)
{
if (username == "System")
{
_systemAuth.User = user;
authStrategy.Modules = _systemAuth.Modules;
authStrategy.Roles = _systemAuth.Roles;
authStrategy.Orgs = _systemAuth.Orgs;
authStrategy.User = _systemAuth.User;
}
else
|
赖素文
authored
|
65
|
{
|
唐召明
authored
|
66
67
68
69
70
|
_normalAuthStrategy.User = user;
authStrategy.Modules = _normalAuthStrategy.Modules;
authStrategy.Roles = _normalAuthStrategy.Roles;
authStrategy.Orgs = _normalAuthStrategy.Orgs;
authStrategy.User = _normalAuthStrategy.User;
|
赖素文
authored
|
71
|
}
|
唐召明
authored
|
72
73
|
var cacheOption = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(1));
_cache.Set(username, JsonSerializer.SerializeToUtf8Bytes(authStrategy), cacheOption);
|
赖素文
authored
|
74
75
76
77
78
|
}
return authStrategy;
}
}
}
|