forked from liaozb/APIJSON.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityService.cs
More file actions
134 lines (124 loc) · 3.93 KB
/
IdentityService.cs
File metadata and controls
134 lines (124 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using APIJSON.NET.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace APIJSON.NET.Services
{
/// <summary>
///
/// </summary>
public class IdentityService : IIdentityService
{
private IHttpContextAccessor _context;
private List<Role> roles;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="_roles"></param>
public IdentityService(IHttpContextAccessor context, IOptions<List<Role>> _roles)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
roles = _roles.Value;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetUserIdentity()
{
return _context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetUserRoleName()
{
return _context.HttpContext.User.FindFirstValue(ClaimTypes.Role);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Role GetRole()
{
var role = new Role();
if (string.IsNullOrEmpty(GetUserRoleName()))//没登录默认取第一个
{
role = roles.FirstOrDefault();
}
else
{
role = roles.FirstOrDefault(it => it.Name.Equals(GetUserRoleName(), StringComparison.CurrentCultureIgnoreCase));
}
return role;
}
/// <summary>
///
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public Tuple<bool, string> GetSelectRole(string table)
{
var role = GetRole();
if (role == null || role.Select == null || role.Select.Table == null)
{
return Tuple.Create(false, $"appsettings.json权限配置不正确!");
}
string tablerole = role.Select.Table.FirstOrDefault(it => it == "*" || it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
if (string.IsNullOrEmpty(tablerole))
{
return Tuple.Create(false, $"表名{table}没权限查询!");
}
int index = Array.IndexOf(role.Select.Table, tablerole);
string selectrole = role.Select.Column[index];
return Tuple.Create(true, selectrole);
}
/// <summary>
///
/// </summary>
/// <param name="col"></param>
/// <param name="selectrole"></param>
/// <returns></returns>
public bool ColIsRole(string col, string[] selectrole)
{
if (selectrole.Contains("*"))
{
return true;
}
else
{
if (col.Contains("(") && col.Contains(")"))
{
Regex reg = new Regex(@"\(([^)]*)\)");
Match m = reg.Match(col);
if (selectrole.Contains(m.Result("$1"), StringComparer.CurrentCultureIgnoreCase))
{
return true;
}
else
{
return false;
}
}
else
{
if (selectrole.Contains(col, StringComparer.CurrentCultureIgnoreCase))
{
return true;
}
else
{
return false;
}
}
}
}
}
}