Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add admin restore user vault to previous version (#200)

Leendert de Borst7977cd7394dc
diff --git a/src/AliasVault.Admin/Main/Pages/Users/View.razor b/src/AliasVault.Admin/Main/Pages/Users/View.razor
index 8a7c5953c..503e78b42 100644
--- a/src/AliasVault.Admin/Main/Pages/Users/View.razor
+++ b/src/AliasVault.Admin/Main/Pages/Users/View.razor
@@ -5,7 +5,7 @@
@if (IsLoading || User == null)
{
- <LoadingIndicator />
+ <LoadingIndicator/>
}
else
{
@@ -19,7 +19,7 @@ else
<a href="/users/@Id/delete" class="mr-2 px-4 py-2 text-white bg-red-600 rounded-lg hover:bg-red-700 focus:ring-4 focus:ring-red-300 dark:bg-red-500 dark:hover:bg-red-600 dark:focus:ring-red-800">
Delete user
</a>
- <RefreshButton OnRefresh="LoadEntryAsync" ButtonText="Refresh" />
+ <RefreshButton OnClick="LoadEntryAsync" ButtonText="Refresh"/>
</div>
</div>
</div>
@@ -36,7 +36,7 @@ else
</div>
<div class="flex items-center space-x-2">
<span class="text-sm font-medium text-gray-900 dark:text-white">2FA Status:</span>
- <StatusPill Enabled="@User.TwoFactorEnabled" />
+ <StatusPill Enabled="@User.TwoFactorEnabled"/>
<span>Authenticator key(s) active: @TwoFactorKeysCount</span>
@if (User.TwoFactorEnabled)
{
@@ -84,7 +84,9 @@ else
<td class="px-4 py-3">@entry.DeviceIdentifier</td>
<td class="px-4 py-3">@entry.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
<td class="px-4 py-3">@entry.ExpireDate.ToString("yyyy-MM-dd HH:mm")</td>
- <td class="px-4 py-3"><button @onclick="() => RevokeRefreshToken(entry)" class="px-3 py-2 text-xs font-medium text-center text-white bg-red-700 rounded-lg hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">Revoke</button></td>
+ <td class="px-4 py-3">
+ <button @onclick="() => RevokeRefreshToken(entry)" class="px-3 py-2 text-xs font-medium text-center text-white bg-red-700 rounded-lg hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">Revoke</button>
+ </td>
</tr>
}
</tbody>
@@ -103,19 +105,44 @@ else
<tr>
<th scope="col" class="px-4 py-3">ID</th>
<th scope="col" class="px-4 py-3">Created</th>
+ <th scope="col" class="px-4 py-3">Updated</th>
<th scope="col" class="px-4 py-3">Filesize</th>
<th scope="col" class="px-4 py-3">DB version</th>
+ <th scope="col" class="px-4 py-3">Status</th>
+ <th scope="col" class="px-4 py-3">Actions</th>
</tr>
</thead>
<tbody>
- @foreach (var entry in VaultList)
+ @{
+ Vault? previousEntry = null;
+ }
+ @foreach (var entry in VaultList.OrderBy(e => e.UpdatedAt))
{
<tr class="bg-white border-b hover:bg-gray-50">
<td class="px-4 py-3 font-medium text-gray-900">@entry.Id</td>
<td class="px-4 py-3">@entry.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
+ <td class="px-4 py-3">@entry.UpdatedAt.ToString("yyyy-MM-dd HH:mm")</td>
<td class="px-4 py-3">@Math.Round((double)entry.FileSize / 1024, 1) MB</td>
<td class="px-4 py-3">@entry.Version</td>
+ <td class="px-4 py-3">
+ @if (entry == LatestVault)
+ {
+ <span class="bg-blue-100 text-blue-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Current</span>
+ }
+ @if (previousEntry != null && HasPasswordChanged(entry, previousEntry))
+ {
+ <span class="bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Password Changed</span>
+ }
+ </td>
+ <td class="px-4 py-3">
+ @if (entry != LatestVault)
+ {
+ <Button OnClick="() => MakeCurrentAsync(entry)">Make Current</Button>
+ }
+ </td>
</tr>
+
+ previousEntry = entry;
}
</tbody>
</table>
@@ -155,6 +182,7 @@ else
}
@code {
+
/// <summary>
/// Gets or sets the user ID.
/// </summary>
@@ -208,25 +236,28 @@ else
// Load all refresh tokens for this user to show which devices are logged in.
RefreshTokenList = await DbContext.AliasVaultUserRefreshTokens.Where(x => x.UserId == User.Id).Select(x => new AliasVaultUserRefreshToken()
- {
- Id = x.Id,
- DeviceIdentifier = x.DeviceIdentifier,
- ExpireDate = x.ExpireDate,
- CreatedAt = x.CreatedAt,
- })
- .OrderBy(x => x.CreatedAt)
- .ToListAsync();
+ {
+ Id = x.Id,
+ DeviceIdentifier = x.DeviceIdentifier,
+ ExpireDate = x.ExpireDate,
+ CreatedAt = x.CreatedAt,
+ })
+ .OrderBy(x => x.CreatedAt)
+ .ToListAsync();
// Load all vaults for this user (do not load the actual file content for performance reasons).
VaultList = await DbContext.Vaults.Where(x => x.UserId == User.Id).Select(x => new Vault
- {
- Id = x.Id,
- Version = x.Version,
- FileSize = x.FileSize,
- CreatedAt = x.CreatedAt,
- })
- .OrderBy(x => x.CreatedAt)
- .ToListAsync();
+ {
+ Id = x.Id,
+ Version = x.Version,
+ FileSize = x.FileSize,
+ CreatedAt = x.CreatedAt,
+ UpdatedAt = x.UpdatedAt,
+ Salt = x.Salt,
+ Verifier = x.Verifier,
+ })
+ .OrderBy(x => x.UpdatedAt)
+ .ToListAsync();
// Load all email claims for this user.
EmailClaimList = await DbContext.UserEmailClaims.Where(x => x.UserId == User.Id)
@@ -305,4 +336,54 @@ else
await LoadEntryAsync();
}
}
+
+ /// <summary>
+ /// Determines if the password has changed between two vault entries.
+ /// </summary>
+ /// <param name="current">The current vault entry.</param>
+ /// <param name="previous">The previous vault entry to compare against.</param>
+ /// <returns>True if the password has changed, false otherwise.</returns>
+ private bool HasPasswordChanged(Vault current, Vault previous)
+ {
+ return current.Salt != previous.Salt || current.Verifier != previous.Verifier;
+ }
+
+ /// <summary>
+ /// Gets the latest vault entry from the list.
+ /// </summary>
+ private Vault? LatestVault => VaultList.MaxBy(v => v.UpdatedAt);
+
+ /// <summary>
+ /// Handles the action of making a vault the current one.
+ /// </summary>
+ /// <param name="vault">The vault to make current.</param>
+ private async Task MakeCurrentAsync(Vault vault)
+ {
+ if (await ConfirmModalService.ShowConfirmation(
+ title: "Confirm Vault Restoration",
+ message: $@"Are you sure you want to restore this earlier vault and make it the active one?
+
+Important notes:
+• The next time the user logs in, they will load this previous vault version.
+• If the user has changed their password recently, it will be reverted to the one stored in this vault.
+
+Do you want to proceed with the restoration?")) {
+ // Load vault
+ var currentVault = await DbContext.Vaults.FindAsync(vault.Id);
+
+ if (currentVault == null)
+ {
+ return;
+ }
+
+ // Update time to make this the current vault.
+ currentVault.UpdatedAt = DateTime.UtcNow;
+
+ // Save it.
+ await DbContext.SaveChangesAsync();
+
+ // Reload the page.
+ await LoadEntryAsync();
+ }
+ }
}
diff --git a/src/AliasVault.Admin/wwwroot/css/tailwind.css b/src/AliasVault.Admin/wwwroot/css/tailwind.css
index fd8021ec2..e5ecff4a2 100644
--- a/src/AliasVault.Admin/wwwroot/css/tailwind.css
+++ b/src/AliasVault.Admin/wwwroot/css/tailwind.css
@@ -1300,6 +1300,11 @@ video {
background-color: rgb(234 179 8 / var(--tw-bg-opacity));
}
+.bg-blue-100 {
+ --tw-bg-opacity: 1;
+ background-color: rgb(219 234 254 / var(--tw-bg-opacity));
+}
+
.bg-opacity-50 {
--tw-bg-opacity: 0.5;
}
@@ -1398,6 +1403,21 @@ video {
padding-bottom: 2rem;
}
+.px-2\.5 {
+ padding-left: 0.625rem;
+ padding-right: 0.625rem;
+}
+
+.py-0 {
+ padding-top: 0px;
+ padding-bottom: 0px;
+}
+
+.py-0\.5 {
+ padding-top: 0.125rem;
+ padding-bottom: 0.125rem;
+}
+
.pl-2 {
padding-left: 0.5rem;
}
@@ -1582,6 +1602,11 @@ video {
color: rgb(133 77 14 / var(--tw-text-opacity));
}
+.text-blue-800 {
+ --tw-text-opacity: 1;
+ color: rgb(30 64 175 / var(--tw-text-opacity));
+}
+
.underline {
text-decoration-line: underline;
}
@@ -1704,6 +1729,11 @@ video {
background-color: rgb(153 27 27 / var(--tw-bg-opacity));
}
+.hover\:bg-blue-700:hover {
+ --tw-bg-opacity: 1;
+ background-color: rgb(29 78 216 / var(--tw-bg-opacity));
+}
+
.hover\:text-gray-900:hover {
--tw-text-opacity: 1;
color: rgb(17 24 39 / var(--tw-text-opacity));
diff --git a/src/Utilities/AliasVault.RazorComponents/ConfirmModal.razor b/src/Utilities/AliasVault.RazorComponents/ConfirmModal.razor
index 8a8d57929..eaa4ad40a 100644
--- a/src/Utilities/AliasVault.RazorComponents/ConfirmModal.razor
+++ b/src/Utilities/AliasVault.RazorComponents/ConfirmModal.razor
@@ -10,7 +10,7 @@
<h3 class="text-lg leading-6 font-medium text-gray-900">@ModalService.Title</h3>
<div class="mt-2 px-7 py-3">
<p class="text-sm text-gray-500">
- @ModalService.Message
+ @ConvertNewlinesToHtml(ModalService.Message)
</p>
</div>
<div class="items-center px-4 py-3">
@@ -36,4 +36,14 @@
{
ModalService.OnChange -= StateHasChanged;
}
+
+ /// <summary>
+ /// Converts newline characters to HTML line breaks.
+ /// </summary>
+ /// <param name="text">The input text.</param>
+ /// <returns>The text with newlines replaced by <br /> tags.</returns>
+ private MarkupString ConvertNewlinesToHtml(string text)
+ {
+ return (MarkupString)text.Replace(Environment.NewLine, "<br />").Replace("\n", "<br />");
+ }
}