Merge authentication into Questions API
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using QuestionsApi.Models;
|
||||
|
||||
namespace QuestionsApi.Services;
|
||||
|
||||
public sealed record LegalPolicyStatus(
|
||||
string TermsVersion,
|
||||
string PrivacyVersion,
|
||||
DateTime EffectiveUtc,
|
||||
string TermsUrl,
|
||||
string PrivacyUrl,
|
||||
bool RequiresLegalAcceptance);
|
||||
|
||||
public sealed class LegalPolicy(IConfiguration configuration)
|
||||
{
|
||||
public const string DefaultVersion = "2026-07-20";
|
||||
|
||||
public string TermsVersion => configuration["Legal:TermsVersion"] ?? DefaultVersion;
|
||||
public string PrivacyVersion => configuration["Legal:PrivacyVersion"] ?? DefaultVersion;
|
||||
public string TermsUrl => configuration["Legal:TermsUrl"] ?? "https://doctor.ranaze.com/terms.html";
|
||||
public string PrivacyUrl => configuration["Legal:PrivacyUrl"] ?? "https://doctor.ranaze.com/privacy.html";
|
||||
public DateTime EffectiveUtc => DateTime.TryParse(configuration["Legal:EffectiveUtc"], out var value)
|
||||
? value.ToUniversalTime()
|
||||
: new DateTime(2026, 7, 20, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
public bool IsCurrent(AuthUser user) =>
|
||||
user.TermsAcceptedVersion == TermsVersion && user.PrivacyAcceptedVersion == PrivacyVersion;
|
||||
|
||||
public LegalPolicyStatus Status(AuthUser? user = null) => new(
|
||||
TermsVersion, PrivacyVersion, EffectiveUtc, TermsUrl, PrivacyUrl,
|
||||
user is not null && !IsCurrent(user));
|
||||
|
||||
public void Accept(AuthUser user, string source, DateTime? acceptedUtc = null)
|
||||
{
|
||||
var timestamp = acceptedUtc ?? DateTime.UtcNow;
|
||||
user.LegalAcceptances ??= [];
|
||||
if (IsCurrent(user) && user.LegalAcceptances.Any(record =>
|
||||
record.TermsVersion == TermsVersion && record.PrivacyVersion == PrivacyVersion))
|
||||
return;
|
||||
user.TermsAcceptedVersion = TermsVersion;
|
||||
user.PrivacyAcceptedVersion = PrivacyVersion;
|
||||
user.LegalAcceptedUtc = timestamp;
|
||||
user.LegalAcceptances.Add(new LegalAcceptanceRecord
|
||||
{
|
||||
TermsVersion = TermsVersion,
|
||||
PrivacyVersion = PrivacyVersion,
|
||||
AcceptedUtc = timestamp,
|
||||
Source = source
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user