Files
Server/OwnChar.Server/Extensions/GeneralExtensions.cs
2024-08-23 11:18:46 +02:00

49 lines
1.7 KiB
C#

using OwnChar.Api.Packets;
using OwnChar.Data;
using OwnChar.Data.Model.Base;
using OwnChar.Server.Api;
using OwnChar.Server.Data.Model;
using Pilz.Net.Api;
using System.Diagnostics.CodeAnalysis;
namespace OwnChar.Server.Extensions;
public static class GeneralExtensions
{
public static bool CheckLogin(this IOwnCharServer server, ApiRequestInfo request, UserType userType)
{
if (server.Data is null
|| !request.IsAuthenticated
|| request.AuthKey.Split(":") is not string[] authKey
|| authKey.ElementAtOrDefault(0) is not string username
|| authKey.ElementAtOrDefault(1) is not string secret
|| string.IsNullOrWhiteSpace(username)
|| string.IsNullOrWhiteSpace(secret)
|| !server.IsLoggedIn(secret)
|| server.GetUser(secret) is not UserAccountBase usr
|| usr.Type < userType)
return false;
return true;
}
public static bool CheckLogin(this IOwnCharServer server, ApiRequestInfo request, UserType userType, [NotNullWhen(true)] out UserAccountDb? user)
{
if (server.Data is null
|| !request.IsAuthenticated
|| request.AuthKey.Split(":") is not string[] authKey
|| authKey.ElementAtOrDefault(0) is not string username
|| authKey.ElementAtOrDefault(1) is not string secret
|| string.IsNullOrWhiteSpace(username)
|| string.IsNullOrWhiteSpace(secret)
|| !server.IsLoggedIn(secret)
|| server.GetUser(secret) is not UserAccountDb usr
|| usr.Type < userType)
{
user = null;
return false;
}
user = usr;
return true;
}
}