mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Berufsschule_HAM.Models;
|
|
using Berufsschule_HAM.Services;
|
|
namespace Berufsschule_HAM.Helpers;
|
|
|
|
public static partial class UsersHelper
|
|
{
|
|
public static string CreateUsername(string givenName, string surname)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(givenName) || string.IsNullOrWhiteSpace(surname))
|
|
throw new ArgumentException("Given name and surname must not be empty.");
|
|
|
|
string combined = (surname + givenName).ToLowerInvariant();
|
|
combined = combined.Replace("ä", "ae").Replace("ö", "oe").Replace("ü", "ue");
|
|
// Normalize to decompose accents (e.g., é -> e + ́)
|
|
string normalized = combined.Normalize(NormalizationForm.FormD);
|
|
|
|
// Remove diacritics
|
|
var sb = new StringBuilder();
|
|
foreach (var c in normalized)
|
|
{
|
|
var category = CharUnicodeInfo.GetUnicodeCategory(c);
|
|
if (category != UnicodeCategory.NonSpacingMark)
|
|
sb.Append(c);
|
|
}
|
|
|
|
string cleaned = sb.ToString();
|
|
|
|
// Replace German ligatures etc.
|
|
cleaned = cleaned
|
|
.Replace("ß", "ss")
|
|
.Replace("æ", "ae")
|
|
.Replace("œ", "oe")
|
|
.Replace("ø", "o");
|
|
|
|
// Remove everything not a-z
|
|
cleaned = AtoZ().Replace(cleaned, "");
|
|
|
|
return cleaned;
|
|
}
|
|
|
|
public static async Task<string> HashPassword(LdapService ldapService, string password)
|
|
{
|
|
AdminSettingsModel settings = await ldapService.GetAdminSettingsModelAsync();
|
|
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
|
|
byte[] hashedPassword = settings.hashAlgorithm?.ComputeHash(passwordBytes) ?? throw new Exception("Hash algorithm not instantiated yet");
|
|
return $"{{{settings.DefaultHashAlgorithm.ToUpperInvariant()}}}{Convert.ToBase64String(hashedPassword)}";
|
|
}
|
|
|
|
[GeneratedRegex("[^a-z]")]
|
|
private static partial Regex AtoZ();
|
|
} |