-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
154 lines (137 loc) · 5.7 KB
/
Utils.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Linq.Expressions;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Auth.Constants;
using Auth.Database.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Internal;
using Newtonsoft.Json;
namespace Auth
{
public static class Utils
{
public static IActionResult NORIP()
{
return new RedirectResult("https://magicalmirai.com/");
}
public static string SHA512(string payload)
{
SHA512 sha = System.Security.Cryptography.SHA512.Create();
byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(payload));
StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in hash)
{
stringBuilder.AppendFormat("{0:x2}", b);
}
return stringBuilder.ToString();
}
const int minJpnCharCode = 0x4e00;
const int maxJpnCharCode = 0x9FBF;
public static string GenerateRandomSalt(int length=5)
{
StringBuilder builder = new (length);
Random random = new ();
for (int i = 0; i < length; i++)
{
builder.Append((char)random.Next(minJpnCharCode, maxJpnCharCode));
}
return builder.ToString();
}
public static EntityEntry<T>? AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class, new()
{
bool exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(x => x == entity);
return !exists ? dbSet.Add(entity) : null;
}
public static EntityEntry<T>? RemoveIfExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class, new()
{
bool exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(x => x == entity);
return exists ? dbSet.Remove(entity) : null;
}
public static async Task<int> GetLastUserNumber(uint cardinal)
{
try
{
string ident = "gbsw" + cardinal;
AuthDbContext db = new();
User? last = db.Users.Where(x => x.Cardinal == cardinal && x.Userid.StartsWith(ident)).ToList().OrderByDescending(x=> x.Userid).FirstOrDefault();
if (last == null) return 0;
return GetNumberFromUserId(last.Userid.Remove(0, ident.Length));
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public static async Task<uint> GetLastRoleNumber()
{
try
{
AuthDbContext db = new();
Role? role = await db.Roles.OrderByDescending(x => x.Roleid).FirstOrDefaultAsync();
if (role == null) return 0;
return role.Roleid;
}
catch (Exception)
{
return 0;
}
}
public static int GetNumberFromUserId(string userId)
{
return int.Parse(new Regex(@"\d+").Match(userId).Value);
}
public static User GetUserFromContext(this HttpContext ctx) => (User)ctx.Items["user"];
public static IEnumerable<string> GetPermissions(this User user)
{
AuthDbContext db = new();
IEnumerable<string> permissions = (from r in db.Roles.Where(x => x.Userid == user.Userid).ToList()
let p = from p1 in db.Permissions.Where(x => x.Roleid == r.Roleid)
select p1.Label
select p).SelectMany(x => x).Distinct();
return permissions;
}
public static IEnumerable<string> GetPermissions(string userId)
{
AuthDbContext db = new();
User? user = db.Users.SingleOrDefault(x => x.Userid == userId);
if (user == null) return new string[] { };
IEnumerable<string> permissions = (from r in db.Roles.Where(x => x.Userid == user.Userid).ToList()
let p = from p1 in db.Permissions.Where(x => x.Roleid == r.Roleid)
select p1.Label
select p).SelectMany(x => x).Distinct();
return permissions;
}
public static bool IsAboveThanMe(this User me, User dest)
{
IEnumerable<string> mePerms = me.GetPermissions();
IEnumerable<string> destPerms = dest.GetPermissions();
if (mePerms.Contains(Permissions.ADMINISTRATOR) && destPerms.Contains(Permissions.ADMINISTRATOR)) return true;
if (mePerms.Contains(Permissions.ADMINISTRATOR)) return false;
if (destPerms.Contains(Permissions.ADMINISTRATOR)) return true;
if (mePerms.Contains(Permissions.TEACHER) && destPerms.Contains(Permissions.TEACHER)) return true;
if (mePerms.Contains(Permissions.TEACHER)) return false;
if (destPerms.Contains(Permissions.TEACHER)) return true;
return true;
}
public static IQueryable<T> Pagination<T>(this DbSet<T> dbSet, int page = 1, int limit = 10) where T : class
{
if (page < 1)
{
return dbSet.Take(limit);
}
return dbSet.Skip(limit * (page - 1)).Take(limit);
}
public static IQueryable<T> Pagination<T>(this IQueryable<T> dbSet, int page = 1, int limit = 10) where T : class
{
if (page < 1)
{
return dbSet.Take(limit);
}
return dbSet.Skip(limit * (page - 1)).Take(limit);
}
}
}