aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-04-17 22:55:32 +0800
committercrupest <crupest@outlook.com>2022-04-17 22:55:32 +0800
commit84cb99d087f62dca89fa682feae6738b3350fed4 (patch)
tree03ac3b8064f47a2229941119e0f864ee99e76e72 /BackEnd/Timeline
parent7368d0388e78228499f28b33f79891c60639fb57 (diff)
downloadtimeline-84cb99d087f62dca89fa682feae6738b3350fed4.tar.gz
timeline-84cb99d087f62dca89fa682feae6738b3350fed4.tar.bz2
timeline-84cb99d087f62dca89fa682feae6738b3350fed4.zip
...
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r--BackEnd/Timeline/Helpers/SecureRandomExtensions.cs29
-rw-r--r--BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs80
2 files changed, 109 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs b/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs
new file mode 100644
index 00000000..b4f3f7f0
--- /dev/null
+++ b/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Timeline.Helpers
+{
+ public static class SecureRandomExtensions
+ {
+ private static readonly char[] AlphaDigitString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
+
+ public static string GenerateAlphaDigitString(this RandomNumberGenerator randomNumberGenerator, int length)
+ {
+ if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
+
+ var buffer = new byte[length];
+ randomNumberGenerator.GetBytes(buffer);
+
+ StringBuilder stringBuilder = new();
+
+ foreach (byte b in buffer)
+ {
+ stringBuilder.Append(AlphaDigitString[b % AlphaDigitString.Length]);
+ }
+
+ return stringBuilder.ToString();
+ }
+ }
+}
+
diff --git a/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs
new file mode 100644
index 00000000..eb8bc37a
--- /dev/null
+++ b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Threading.Tasks;
+using Microsoft.EntityFrameworkCore;
+using Timeline.Entities;
+using Timeline.Helpers;
+
+namespace Timeline.Services.User.RegisterCode
+{
+ public class RegisterCodeService : IRegisterCodeService, IDisposable
+ {
+ private readonly DatabaseContext _databaseContext;
+ private readonly RandomNumberGenerator _randomNumberGenerator;
+
+ public RegisterCodeService(DatabaseContext databaseContext)
+ {
+ _databaseContext = databaseContext;
+ _randomNumberGenerator = RandomNumberGenerator.Create();
+ }
+
+ public async Task<string> CreateNewCode(long userId)
+ {
+ var oldEntity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
+
+ if (oldEntity is not null)
+ {
+ oldEntity.Enabled = false;
+ }
+
+ var newEntity = new Entities.RegisterCode()
+ {
+ Code = _randomNumberGenerator.GenerateAlphaDigitString(6),
+ OwnerId = userId,
+ Enabled = true
+ };
+ _databaseContext.RegisterCodes.Add(newEntity);
+
+ await _databaseContext.SaveChangesAsync();
+
+ return newEntity.Code;
+ }
+
+ public Task<UserRegisterInfo> CreateRegisterInfo(long userId, long introducerId, string registerCode, DateTime registerTime)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void Dispose()
+ {
+ _randomNumberGenerator.Dispose();
+ }
+
+ public async Task<long?> GetCodeOwner(string code, bool onlyEnabled = true)
+ {
+ var entity = await _databaseContext.RegisterCodes.Where(r => r.Code == code).SingleOrDefaultAsync();
+ if (entity is null) return null;
+ if (onlyEnabled && !entity.Enabled) return null;
+ return entity.OwnerId;
+ }
+
+ public async Task<string?> GetCurrentCode(long userId)
+ {
+ var entity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
+ return entity?.Code;
+ }
+
+ public Task<UserRegisterInfo?> GetUserRegisterInfo(long userId)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducer(long introducerId)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
+