diff --git a/SharpbinV3.Client/src/lib/components/Modal.svelte b/SharpbinV3.Client/src/lib/components/Modal.svelte index 9767c5a..0d23748 100644 --- a/SharpbinV3.Client/src/lib/components/Modal.svelte +++ b/SharpbinV3.Client/src/lib/components/Modal.svelte @@ -1,10 +1,14 @@ {#if show} @@ -41,12 +53,18 @@ {displayTitle} - {#if mode !== 'confirm'} + {#if message} +

+ {message} +

+ {/if} + + {#if shouldShowInput} {/if} @@ -59,7 +77,9 @@
diff --git a/SharpbinV3.Client/src/lib/components/Navbar.svelte b/SharpbinV3.Client/src/lib/components/Navbar.svelte index 9993448..f7ea11d 100644 --- a/SharpbinV3.Client/src/lib/components/Navbar.svelte +++ b/SharpbinV3.Client/src/lib/components/Navbar.svelte @@ -13,8 +13,6 @@ export let data: PageData; - console.log(data); - let currentPath = page.url.pathname; let returnParam = currentPath !== '/' && !currentPath.startsWith('/login') && !currentPath.startsWith('/register') diff --git a/SharpbinV3.Client/src/lib/utils/auth.ts b/SharpbinV3.Client/src/lib/utils/auth.ts index d618f97..826ad49 100644 --- a/SharpbinV3.Client/src/lib/utils/auth.ts +++ b/SharpbinV3.Client/src/lib/utils/auth.ts @@ -98,3 +98,15 @@ export function clearServerTokens(cookies: Cookies) { cookies.delete('token', { path: '/' }); cookies.delete('refreshToken', { path: '/' }); } + +export function getUserUUIDFromToken() { + if (!browser) return null; + const token = getToken(); + if (!token) return null; + try { + const payload = JSON.parse(atob(token.split('.')[1])); + return payload.UUID || null; + } catch { + return null; + } +} diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 929ac1b..27e17ce 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -12,15 +12,15 @@ let error = ''; let title = ''; let content = ''; - let selectedSyntax = data.options?.syntaxes[0]; + let selectedSyntax = data.options?.syntaxes?.[0]; let expiresIn: number = 0; - let selectedVisibility = data.options?.visibilities[0].value; + let selectedVisibility = data.options?.visibilities?.[0]?.value; let password = ''; - const syntaxOptions = data.options?.syntaxes.map((lang: string) => ({ + const syntaxOptions = data.options?.syntaxes?.map((lang: string) => ({ value: lang, label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1) - })); + })) ?? []; const expiresOptions = [ { value: 0, label: 'Never Expire' }, @@ -38,12 +38,12 @@ { value: 315360000000, label: 'Expire in 10 Years' } ]; - const visibilityOptions = data.options?.visibilities.map( + const visibilityOptions = data.options?.visibilities?.map( (visibility: { value: number; displayName: string }) => ({ value: visibility.value, label: visibility.displayName }) - ); + ) ?? []; async function handleSubmit(event: Event) { event.preventDefault(); @@ -103,7 +103,7 @@ spellcheck="false" autocomplete="off" bind:value={title} - maxlength={data.options.maxTitleLength ?? 500} + maxlength={data.options?.maxTitleLength ?? 500} /> diff --git a/SharpbinV3.Client/src/routes/[id]/+page.svelte b/SharpbinV3.Client/src/routes/[id]/+page.svelte index df1ab11..cf4b787 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.svelte +++ b/SharpbinV3.Client/src/routes/[id]/+page.svelte @@ -60,10 +60,10 @@ : false; let interval: ReturnType | null = null; - const syntaxOptions = data.options?.syntaxes.map((lang: string) => ({ + const syntaxOptions = data.options?.syntaxes?.map((lang: string) => ({ value: lang, label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1) - })); + })) ?? []; const expiresOptions = [ { value: 0, label: 'Never Expire' }, { value: 600000, label: 'Expire in 10 Minutes' }, @@ -79,12 +79,12 @@ { value: 157680000000, label: 'Expire in 5 Years' }, { value: 315360000000, label: 'Expire in 10 Years' } ]; - const visibilityOptions = data.options?.visibilities.map( + const visibilityOptions = data.options?.visibilities?.map( (visibility: { value: number; displayName: string }) => ({ value: visibility.value, label: visibility.displayName }) - ); + ) ?? []; onMount(() => { interval = setInterval(() => { @@ -302,7 +302,7 @@ bind:value={editMetadata.title} class="w-full rounded border border-neutral-700 bg-neutral-800 p-2 text-white outline-none" placeholder="Untitled Paste" - maxlength={data.options.maxTitleLength ?? 500} + maxlength={data.options?.maxTitleLength ?? 500} /> {/if} @@ -398,7 +398,7 @@ {displayNames[data.paste.syntax] ?? data.paste.syntax.charAt(0).toUpperCase() + data.paste.syntax.slice(1)} - {:else if editMetadata} + {:else if editMetadata && syntaxOptions} diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte index 8629f7b..233d0b3 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte @@ -1,31 +1,45 @@
{data.user.username}
{/if} -
+
{pagination.totalCount} paste{pagination.totalCount !== 1 ? 's' : ''}
+
+ + + {data.user.uid} + +
+ {#if isOwner && data.user.lastLogin} +
+ + + Last login {dateToRelativeString(new Date(data.user.lastLogin))} + +
+ {/if}
+ {#if isOwner} +
+ + +
+ {/if}
{#if pastes && pastes.length > 0} {#each pastes.slice().sort((a, b) => b.uuid.localeCompare(a.uuid)) as paste (paste.uuid)} @@ -108,7 +209,7 @@ {/each} {:else} -

This user has not created any pastes yet.

+

{isOwner ? 'You have' : 'This user has'} not created any pastes yet.

{/if}
{#if pagination.totalPages && pagination.totalPages > 1} @@ -134,3 +235,37 @@ {/if}
+ + handleEditDisplayName(value as string)} + onCancel={() => { + showEditModal = false; + modalError = ''; + }} +/> + + handleDeleteAccount()} + onCancel={() => { + showDeleteModal = false; + modalError = ''; + }} +/> + + diff --git a/SharpbinV3.Server/Controllers/UserController.cs b/SharpbinV3.Server/Controllers/UserController.cs index db12cc4..f966bba 100644 --- a/SharpbinV3.Server/Controllers/UserController.cs +++ b/SharpbinV3.Server/Controllers/UserController.cs @@ -71,6 +71,28 @@ namespace SharpbinV3.Server.Controllers return Ok(new { message = "User updated successfully." }); } + [HttpDelete("uuid/{uuid}")] + [Authorize] + public async Task DeleteByUUID(Guid uuid) + { + var jwtUser = HttpContext.User; + var uuidClaim = jwtUser?.FindFirst("UUID")?.Value; + if (uuidClaim == null || jwtUser == null) return Forbid(); + var userRoles = jwtUser.Claims + .Where(c => c.Type == ClaimTypes.Role) + .Select(c => int.Parse(c.Value)); + + var user = await _userService.GetByUUID(uuid); + if (user == null) + return NotFound(); + if (!userRoles.Any(r => r == 255) && user.UUID != Guid.Parse(uuidClaim)) + return Forbid(); + + _db.Users.Remove(user); + await _db.SaveChangesAsync(); + return Ok(new { message = "User deleted successfully." }); + } + private async Task BuildUserResponse(User user, int page = 1) { var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString(); diff --git a/SharpbinV3.Server/DTOs/RegisterRequest.cs b/SharpbinV3.Server/DTOs/RegisterRequest.cs index 57ee0e7..1cb54b1 100644 --- a/SharpbinV3.Server/DTOs/RegisterRequest.cs +++ b/SharpbinV3.Server/DTOs/RegisterRequest.cs @@ -1,8 +1,9 @@ using System.ComponentModel.DataAnnotations; +using System.Text.RegularExpressions; namespace SharpbinV3.Server.DTOs { - public class RegisterRequest + public partial class RegisterRequest { [Required] [StringLength(50, MinimumLength = 3)] @@ -16,5 +17,30 @@ namespace SharpbinV3.Server.DTOs public string? DisplayName { get; set; } public string? Token { get; set; } + + public IEnumerable Validate() + { + var results = new List(); + if (!ValidateUsername().IsMatch(Username)) + results.Add(new ValidationResult("Username can only contain letters, numbers, underscores, and dots.", [nameof(Username)])); + + if (DisplayName != null) + { + if (DisplayName.Length < 3 || DisplayName.Length > 26) + results.Add(new ValidationResult("DisplayName must be between 3 and 26 characters.", [nameof(DisplayName)])); + if (!ValidateUsername().IsMatch(DisplayName)) + results.Add(new ValidationResult("DisplayName can only contain letters, numbers, underscores, and dots.", [nameof(DisplayName)])); + } + if (Email != null) + { + var emailAttribute = new EmailAddressAttribute(); + if (!emailAttribute.IsValid(Email)) + results.Add(new ValidationResult("Invalid email format.", [nameof(Email)])); + } + return results; + } + + [GeneratedRegex("^[A-Za-z0-9_.]+$")] + private static partial Regex ValidateUsername(); } } diff --git a/SharpbinV3.Server/DTOs/UpdateUserRequest.cs b/SharpbinV3.Server/DTOs/UpdateUserRequest.cs index ee034dc..e9348bd 100644 --- a/SharpbinV3.Server/DTOs/UpdateUserRequest.cs +++ b/SharpbinV3.Server/DTOs/UpdateUserRequest.cs @@ -1,38 +1,39 @@ using System.ComponentModel.DataAnnotations; +using System.Text.RegularExpressions; namespace SharpbinV3.Server.DTOs { - public class UpdateUserRequest + public partial class UpdateUserRequest { public string? DisplayName { get; set; } public string? Email { get; set; } public int? Visibility { get; set; } = 0; - public IEnumerable Validate(ValidationContext validationContext) + public IEnumerable Validate() { var results = new List(); - if (DisplayName != null) { + if (DisplayName != null) + { if (DisplayName.Length < 3 || DisplayName.Length > 26) - { results.Add(new ValidationResult("DisplayName must be between 3 and 26 characters.", [nameof(DisplayName)])); - } + if (!ValidateDisplayname().IsMatch(DisplayName)) + results.Add(new ValidationResult("DisplayName can only contain letters, numbers, underscores, and dots.", [nameof(DisplayName)])); } if (Email != null) { var emailAttribute = new EmailAddressAttribute(); if (!emailAttribute.IsValid(Email)) - { results.Add(new ValidationResult("Invalid email format.", [nameof(Email)])); - } } if (Visibility != null) { if (Visibility < 0 || Visibility > 2) - { results.Add(new ValidationResult("Visibility must be between 0 and 2.", [nameof(Visibility)])); - } } return results; } + + [GeneratedRegex("^[A-Za-z0-9_.]+$")] + private static partial Regex ValidateDisplayname(); } } diff --git a/SharpbinV3.Server/Data/Entities/User.cs b/SharpbinV3.Server/Data/Entities/User.cs index 4700819..95949b5 100644 --- a/SharpbinV3.Server/Data/Entities/User.cs +++ b/SharpbinV3.Server/Data/Entities/User.cs @@ -3,10 +3,11 @@ using System.ComponentModel.DataAnnotations; namespace SharpbinV3.Server.Data.Entities { + [Index(nameof(UID))] [Index(nameof(UUID), IsUnique = true)] public sealed class User { - public int UID { get; set; } + public int? UID { get; set; } [Key] public required Guid UUID { get; set; } = Guid.CreateVersion7(); // uuid v7 includes timestamp public required string Username { get; set; } diff --git a/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.Designer.cs b/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.Designer.cs new file mode 100644 index 0000000..77e2d66 --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.Designer.cs @@ -0,0 +1,190 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Server.Data; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260104211956_UserUIDAutoIncrement")] + partial class UserUIDAutoIncrement + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("IsCompressed") + .HasColumnType("INTEGER"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("Views"); + + b.HasIndex("Visibility"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.Property("UserUUID") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("JwtId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Token") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Used") + .HasColumnType("INTEGER"); + + b.HasKey("UserUUID"); + + b.ToTable("RefreshTokens"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Property("UUID") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.PrimitiveCollection("Roles") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UID") + .HasColumnType("INTEGER"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("UUID"); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("AuthorUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("RefreshTokens") + .HasForeignKey("UserUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Navigation("Pastes"); + + b.Navigation("RefreshTokens"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.cs b/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.cs new file mode 100644 index 0000000..87c5612 --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104211956_UserUIDAutoIncrement.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + /// + public partial class UserUIDAutoIncrement : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql(""" + CREATE TRIGGER Users_UID_AutoIncrement + AFTER INSERT ON Users + BEGIN + UPDATE Users + SET UID = ( + SELECT IFNULL(MAX(UID), 0) + 1 FROM Users + ) + WHERE rowid = NEW.rowid AND NEW.UID IS NULL; + END; + """, suppressTransaction: true); + + } + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql(""" + DROP TRIGGER IF EXISTS Users_UID_AutoIncrement; + """); + } + } +} diff --git a/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.Designer.cs b/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.Designer.cs new file mode 100644 index 0000000..f584552 --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.Designer.cs @@ -0,0 +1,193 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Server.Data; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260104212417_UserUIDIndex")] + partial class UserUIDIndex + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("IsCompressed") + .HasColumnType("INTEGER"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("Views"); + + b.HasIndex("Visibility"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.Property("UserUUID") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("JwtId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Token") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Used") + .HasColumnType("INTEGER"); + + b.HasKey("UserUUID"); + + b.ToTable("RefreshTokens"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Property("UUID") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.PrimitiveCollection("Roles") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UID") + .HasColumnType("INTEGER"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("UUID"); + + b.HasIndex("UID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("AuthorUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("RefreshTokens") + .HasForeignKey("UserUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Navigation("Pastes"); + + b.Navigation("RefreshTokens"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.cs b/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.cs new file mode 100644 index 0000000..84a03ba --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104212417_UserUIDIndex.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + /// + public partial class UserUIDIndex : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "IX_Users_UID", + table: "Users", + column: "UID", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Users_UID", + table: "Users"); + } + } +} diff --git a/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.Designer.cs b/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.Designer.cs new file mode 100644 index 0000000..d7a6543 --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.Designer.cs @@ -0,0 +1,192 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Server.Data; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260104212733_UserUIDNonUnique")] + partial class UserUIDNonUnique + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("IsCompressed") + .HasColumnType("INTEGER"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("Views"); + + b.HasIndex("Visibility"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.Property("UserUUID") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("JwtId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Token") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Used") + .HasColumnType("INTEGER"); + + b.HasKey("UserUUID"); + + b.ToTable("RefreshTokens"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Property("UUID") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.PrimitiveCollection("Roles") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UID") + .HasColumnType("INTEGER"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Visibility") + .HasColumnType("INTEGER"); + + b.HasKey("UUID"); + + b.HasIndex("UID"); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("AuthorUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b => + { + b.HasOne("SharpbinV3.Server.Data.Entities.User", "User") + .WithMany("RefreshTokens") + .HasForeignKey("UserUUID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b => + { + b.Navigation("Pastes"); + + b.Navigation("RefreshTokens"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.cs b/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.cs new file mode 100644 index 0000000..21b1635 --- /dev/null +++ b/SharpbinV3.Server/Migrations/20260104212733_UserUIDNonUnique.cs @@ -0,0 +1,55 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Server.Migrations +{ + /// + public partial class UserUIDNonUnique : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Users_UID", + table: "Users"); + + migrationBuilder.AlterColumn( + name: "UID", + table: "Users", + type: "INTEGER", + nullable: true, + oldClrType: typeof(int), + oldType: "INTEGER"); + + migrationBuilder.CreateIndex( + name: "IX_Users_UID", + table: "Users", + column: "UID"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Users_UID", + table: "Users"); + + migrationBuilder.AlterColumn( + name: "UID", + table: "Users", + type: "INTEGER", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "INTEGER", + oldNullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Users_UID", + table: "Users", + column: "UID", + unique: true); + } + } +} diff --git a/SharpbinV3.Server/Migrations/AppDbContextModelSnapshot.cs b/SharpbinV3.Server/Migrations/AppDbContextModelSnapshot.cs index a17b31a..040d9fa 100644 --- a/SharpbinV3.Server/Migrations/AppDbContextModelSnapshot.cs +++ b/SharpbinV3.Server/Migrations/AppDbContextModelSnapshot.cs @@ -135,7 +135,7 @@ namespace SharpbinV3.Server.Migrations .IsRequired() .HasColumnType("TEXT"); - b.Property("UID") + b.Property("UID") .HasColumnType("INTEGER"); b.Property("Username") @@ -147,6 +147,8 @@ namespace SharpbinV3.Server.Migrations b.HasKey("UUID"); + b.HasIndex("UID"); + b.HasIndex("UUID") .IsUnique(); diff --git a/SharpbinV3.Server/Program.cs b/SharpbinV3.Server/Program.cs index 0682b5e..48f1e86 100644 --- a/SharpbinV3.Server/Program.cs +++ b/SharpbinV3.Server/Program.cs @@ -111,6 +111,19 @@ namespace SharpbinV3.Server { var db = scope.ServiceProvider.GetRequiredService(); db.Database.Migrate(); + + // this is temporary since the migration doesnt seem to work properly + db.Database.ExecuteSql($""" + CREATE TRIGGER IF NOT EXISTS Users_UID_AutoIncrement + AFTER INSERT ON Users + BEGIN + UPDATE Users + SET UID = ( + SELECT IFNULL(MAX(UID), 0) + 1 FROM Users + ) + WHERE rowid = NEW.rowid AND NEW.UID IS NULL; + END; + """); } app.MapHealthChecks("/health");