Implement general settings on credential page (#145)

This commit is contained in:
Leendert de Borst
2024-08-05 11:38:47 +02:00
parent eacfee78cc
commit 7d358e0c00
6 changed files with 87 additions and 57 deletions

View File

@@ -24,7 +24,10 @@
<h3 class="mb-4 text-xl font-semibold dark:text-white">Email</h3>
</div>
<div class="flex justify-end items-center space-x-2">
<div class="w-3 h-3 mr-2 rounded-full bg-primary-300 border-2 border-primary-100 animate-pulse" title="Auto-refresh enabled"></div>
@if (RefreshTimer is not null)
{
<div class="w-3 h-3 mr-2 rounded-full bg-primary-300 border-2 border-primary-100 animate-pulse" title="Auto-refresh enabled"></div>
}
<button id="recent-email-refresh" @onclick="ManualRefresh" type="button" class="text-blue-700 border border-blue-700 hover:bg-blue-700 hover:text-white focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-full text-sm p-2.5 text-center inline-flex items-center dark:border-blue-500 dark:text-blue-500 dark:hover:text-white dark:focus:ring-blue-800 dark:hover:bg-blue-500">
Refresh
</button>
@@ -114,9 +117,13 @@
}
IsSpamOk = IsSpamOkDomain(EmailAddress);
RefreshTimer = new Timer(2000);
RefreshTimer.Elapsed += async (sender, e) => await TimerRefresh();
RefreshTimer.Start();
// Only enable auto-refresh if the setting is enabled.
if (DbService.Settings.AutoEmailRefresh)
{
RefreshTimer = new Timer(2000);
RefreshTimer.Elapsed += async (sender, e) => await TimerRefresh();
RefreshTimer.Start();
}
}
/// <inheritdoc />
@@ -284,7 +291,7 @@
Error = "The current chosen email address is already in use. Please change the email address by editing this credential.";
break;
case "CLAIM_DOES_NOT_EXIST":
Error = "An error occurred while trying to load the emails. Please try to edit and" +
Error = "An error occurred while trying to load the emails. Please try to edit and " +
"save the credential entry to synchronize the database, then again.";
break;
default:

View File

@@ -240,7 +240,7 @@ else
// Create new Obj
var alias = new Credential();
alias.Alias = new Alias();
alias.Alias.Email = string.Empty;
alias.Alias.Email = "@" + GetDefaultEmailDomain();
alias.Service = new Service();
alias.Passwords = new List<Password> { new Password() };
@@ -282,23 +282,9 @@ else
Obj.Alias.AddressCountry = identity.Address.Country;
Obj.Alias.Hobbies = identity.Hobbies[0];
var defaultDomain = Config.PrivateEmailDomains[0];
if (defaultDomain == "DISABLED.TLD")
{
if (Config.PublicEmailDomains.Count == 0)
{
Obj.Alias.Email = identity.EmailPrefix + "@example.com";
}
else
{
Obj.Alias.Email = identity.EmailPrefix + "@" + Config.PublicEmailDomains[0];
}
}
else
{
Obj.Alias.Email = identity.EmailPrefix + "@" + defaultDomain;
}
// Set the email
var emailDomain = GetDefaultEmailDomain();
Obj.Alias.Email = $"{identity.EmailPrefix}@{emailDomain}";
Obj.Alias.PhoneMobile = identity.PhoneMobile;
Obj.Alias.BankAccountIBAN = identity.BankAccountIBAN;
@@ -414,4 +400,29 @@ else
return credential;
}
/// <summary>
/// Gets the default email domain based on settings and available domains.
/// </summary>
private string GetDefaultEmailDomain()
{
var defaultDomain = DbService.Settings.DefaultEmailDomain;
// Function to check if a domain is valid
bool IsValidDomain(string domain) =>
!string.IsNullOrEmpty(domain) &&
domain != "DISABLED.TLD" &&
(Config.PublicEmailDomains.Contains(domain) || Config.PrivateEmailDomains.Contains(domain));
// Get the first valid domain from private or public domains
string GetFirstValidDomain() =>
Config.PrivateEmailDomains.FirstOrDefault(IsValidDomain) ??
Config.PublicEmailDomains.FirstOrDefault() ??
"example.com";
// Use the default domain if it's valid, otherwise get the first valid domain
string domainToUse = IsValidDomain(defaultDomain) ? defaultDomain : GetFirstValidDomain();
return domainToUse;
}
}

View File

@@ -16,53 +16,67 @@
<div class="mb-4">
<label for="defaultEmailDomain" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Default Email Domain</label>
<select @bind="DefaultEmailDomain" id="defaultEmailDomain" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<select @bind="DefaultEmailDomain" @bind:after="UpdateDefaultEmailDomain" id="defaultEmailDomain" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
@if (ShowPrivateDomains)
{
<optgroup label="Private Domains">
@foreach (var domain in PrivateDomains)
{
<option value="@domain">@domain</option>
}
</optgroup>
<optgroup label="Private Domains">
@foreach (var domain in PrivateDomains)
{
<option value="@domain">@domain</option>
}
</optgroup>
}
<optgroup label="Public Domains">
@foreach (var domain in PublicDomains)
{
<option value="@domain">@domain</option>
<option value="@domain">@domain</option>
}
</optgroup>
</select>
<span class="block text-sm font-normal text-gray-500 truncate dark:text-gray-400">
Set the default email domain that will be used when creating new credentials.
</span>
</div>
<div class="flex items-center mb-4">
<input @bind="AutoEmailRefresh" id="autoEmailRefresh" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<input @bind="AutoEmailRefresh" @bind:after="UpdateAutoEmailRefresh" id="autoEmailRefresh" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="autoEmailRefresh" class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">Auto email refresh on credential page</label>
</div>
</div>
@code {
private List<string> PrivateDomains => Config.PrivateEmailDomains;
private List<string> PublicDomains => Config.PublicEmailDomains;
private bool ShowPrivateDomains => PrivateDomains.Count > 0 && !(PrivateDomains.Count == 1 && PrivateDomains[0] == "DISABLED.TLD");
private string DefaultEmailDomain
{
get => DbService.Settings.DefaultEmailDomain;
set => DbService.Settings.SetDefaultEmailDomain(value).Wait();
}
private bool AutoEmailRefresh
{
get => DbService.Settings.AutoEmailRefresh;
set => DbService.Settings.SetAutoEmailRefreshAsync(value).Wait();
}
private string DefaultEmailDomain { get; set; } = string.Empty;
private bool AutoEmailRefresh { get; set; }
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Vault settings" });
DefaultEmailDomain = DbService.Settings.DefaultEmailDomain;
AutoEmailRefresh = DbService.Settings.AutoEmailRefresh;
}
/// <summary>
/// Updates the default email domain.
/// </summary>
private async Task UpdateDefaultEmailDomain()
{
await DbService.Settings.SetDefaultEmailDomain(DefaultEmailDomain);
StateHasChanged();
}
/// <summary>
/// Updates the auto email refresh setting.
/// </summary>
private async Task UpdateAutoEmailRefresh()
{
await DbService.Settings.SetAutoEmailRefresh(AutoEmailRefresh);
StateHasChanged();
}
}

View File

@@ -467,23 +467,11 @@ public class DbService : IDisposable
.Select(email => email!)
.ToListAsync();
Console.WriteLine("Before filtering email addresses:");
foreach (var email in emailAddresses)
{
Console.WriteLine(email);
}
// Filter the list of email addresses to only include those that are in the allowed domains.
emailAddresses = emailAddresses
.Where(email => _config.PrivateEmailDomains.Exists(domain => email.EndsWith(domain)))
.ToList();
Console.WriteLine("After filtering email addresses:");
foreach (var email in emailAddresses)
{
Console.WriteLine(email);
}
var databaseVersion = await GetCurrentDatabaseVersionAsync();
var vaultObject = new Vault(encryptedDatabase, databaseVersion, publicEncryptionKey, emailAddresses, DateTime.Now, DateTime.Now);

View File

@@ -50,7 +50,7 @@ public class SettingsService
/// </summary>
/// <param name="value">The new value.</param>
/// <returns>Task.</returns>
public Task SetAutoEmailRefreshAsync(bool value) => SetSettingAsync<bool>("AutoEmailRefresh", value);
public Task SetAutoEmailRefresh(bool value) => SetSettingAsync<bool>("AutoEmailRefresh", value);
/// <summary>
/// Initializes the settings service asynchronously.
@@ -157,6 +157,10 @@ public class SettingsService
db.Settings.Update(setting);
}
// Also update the setting in the local dictionary so the new value
// is returned by subsequent local reads.
_settings[key] = value;
await _dbService.SaveDatabaseAsync();
}

View File

@@ -1145,6 +1145,12 @@ video {
overscroll-behavior-y: auto;
}
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.whitespace-nowrap {
white-space: nowrap;
}