diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/Admin.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/Admin.razor index 3939ea9..d3cc79e 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/Admin.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/Admin.razor @@ -7,7 +7,7 @@ @using Web_Jarmark.Data.Models @inject ApplicationDbContext Db -@attribute [Authorize] +@attribute [Authorize(Roles = "Admin")] Přehled administrace diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor index 2baaf3a..02c8f59 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor @@ -8,8 +8,7 @@ @inject UserManager UserManager @inject RoleManager RoleManager -@attribute [Authorize] - +@attribute [Authorize(Roles = "Admin")] Registrovaní uživatelé
diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor index b1ab2f1..98c27c3 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor @@ -9,7 +9,7 @@ @inject ApplicationDbContext Db @inject NavigationManager Nav -@attribute [Authorize] +@attribute [Authorize(Roles = "Admin")] @(IsNew ? "Nová kategorie" : "Upravit kategorii") diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor index 2f84bd2..4ccea73 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor @@ -7,7 +7,7 @@ @using Web_Jarmark.Data.Models @inject ApplicationDbContext Db -@attribute [Authorize] +@attribute [Authorize(Roles = "Admin")] Kategorie diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor index f8b5f50..9995e8b 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor @@ -9,7 +9,7 @@ @inject ApplicationDbContext Db @inject NavigationManager Nav -@attribute [Authorize] +@attribute [Authorize(Roles = "Admin")] @(IsNew ? "Nové zboží" : "Upravit zboží") diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor index bd3441a..c403200 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor @@ -7,7 +7,7 @@ @using Web_Jarmark.Data.Models @inject ApplicationDbContext Db -@attribute [Authorize] +@attribute [Authorize(Roles = "Admin")] Zboží diff --git a/Web-Jarmark/Web-Jarmark/Data/ApplicationUser.cs b/Web-Jarmark/Web-Jarmark/Data/ApplicationUser.cs index c634e8c..2704242 100644 --- a/Web-Jarmark/Web-Jarmark/Data/ApplicationUser.cs +++ b/Web-Jarmark/Web-Jarmark/Data/ApplicationUser.cs @@ -1,10 +1,17 @@ + using Microsoft.AspNetCore.Identity; namespace Web_Jarmark.Data { - // Add profile data for application users by adding properties to the ApplicationUser class public class ApplicationUser : IdentityUser { - } + // Jméno uživatele + public string? FirstName { get; set; } + // Příjmení uživatele + public string? LastName { get; set; } + + // Datum registrace + public DateTime? CreatedAt { get; set; } + } } diff --git a/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.Designer.cs b/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.Designer.cs new file mode 100644 index 0000000..13184a5 --- /dev/null +++ b/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.Designer.cs @@ -0,0 +1,624 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Web_Jarmark.Data; + +#nullable disable + +namespace Web_Jarmark.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20261104173931_AddCreatedAtToUsers")] + partial class AddCreatedAtToUsers + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.13"); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ClaimType") + .HasColumnType("TEXT"); + + b.Property("ClaimValue") + .HasColumnType("TEXT"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ClaimType") + .HasColumnType("TEXT"); + + b.Property("ClaimValue") + .HasColumnType("TEXT"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ProviderKey") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ProviderDisplayName") + .HasColumnType("TEXT"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserPasskey", b => + { + b.Property("CredentialId") + .HasMaxLength(1024) + .HasColumnType("BLOB"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("CredentialId"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserPasskeys", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("TEXT"); + + b.Property("RoleId") + .HasColumnType("TEXT"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("TEXT"); + + b.Property("LoginProvider") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("Name") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("Value") + .HasColumnType("TEXT"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("Web_Jarmark.Data.ApplicationUser", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AccessFailedCount") + .HasColumnType("INTEGER"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("EmailConfirmed") + .HasColumnType("INTEGER"); + + b.Property("FirstName") + .HasColumnType("TEXT"); + + b.Property("LastName") + .HasColumnType("TEXT"); + + b.Property("LockoutEnabled") + .HasColumnType("INTEGER"); + + b.Property("LockoutEnd") + .HasColumnType("TEXT"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("PhoneNumber") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("INTEGER"); + + b.Property("SecurityStamp") + .HasColumnType("TEXT"); + + b.Property("TwoFactorEnabled") + .HasColumnType("INTEGER"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Cart", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("Token") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UpdatedAt") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Token") + .IsUnique(); + + b.ToTable("Carts"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.CartItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CartId") + .HasColumnType("INTEGER"); + + b.Property("ProductId") + .HasColumnType("INTEGER"); + + b.Property("Quantity") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("CartId", "ProductId") + .IsUnique(); + + b.ToTable("CartItems"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("IsActive") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Slug") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CancelledAt") + .HasColumnType("TEXT"); + + b.Property("Code") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("CustomerName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Email") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ExpiresAt") + .HasColumnType("TEXT"); + + b.Property("IssuedAt") + .HasColumnType("TEXT"); + + b.Property("IssuedByUserId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Note") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PaidAt") + .HasColumnType("TEXT"); + + b.Property("PaymentReference") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Phone") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PreferredPayment") + .HasColumnType("INTEGER"); + + b.Property("Status") + .HasColumnType("INTEGER"); + + b.Property("TotalAmount") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Order"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.OrderItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("OrderId") + .HasColumnType("INTEGER"); + + b.Property("ProductId") + .HasColumnType("INTEGER"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Quantity") + .HasColumnType("INTEGER"); + + b.Property("UnitPrice") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.ToTable("OrderItem"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CategoryId") + .HasColumnType("INTEGER"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("IsVisible") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("TEXT"); + + b.Property("StockQuantity") + .HasColumnType("INTEGER"); + + b.Property("UpdatedAt") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Web_Jarmark.Data.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Web_Jarmark.Data.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserPasskey", b => + { + b.HasOne("Web_Jarmark.Data.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.OwnsOne("Microsoft.AspNetCore.Identity.IdentityPasskeyData", "Data", b1 => + { + b1.Property("IdentityUserPasskeyCredentialId"); + + b1.Property("AttestationObject") + .IsRequired(); + + b1.Property("ClientDataJson") + .IsRequired(); + + b1.Property("CreatedAt"); + + b1.Property("IsBackedUp"); + + b1.Property("IsBackupEligible"); + + b1.Property("IsUserVerified"); + + b1.Property("Name"); + + b1.Property("PublicKey") + .IsRequired(); + + b1.Property("SignCount"); + + b1.PrimitiveCollection("Transports"); + + b1.HasKey("IdentityUserPasskeyCredentialId"); + + b1.ToTable("AspNetUserPasskeys"); + + b1 + .ToJson("Data") + .HasColumnType("TEXT"); + + b1.WithOwner() + .HasForeignKey("IdentityUserPasskeyCredentialId"); + }); + + b.Navigation("Data") + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Web_Jarmark.Data.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Web_Jarmark.Data.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.CartItem", b => + { + b.HasOne("Web_Jarmark.Data.Models.Cart", "Cart") + .WithMany("CartItems") + .HasForeignKey("CartId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Web_Jarmark.Data.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Cart"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.OrderItem", b => + { + b.HasOne("Web_Jarmark.Data.Models.Order", "Order") + .WithMany("OrderItems") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Web_Jarmark.Data.Models.Product", "Product") + .WithMany("OrderItems") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Product", b => + { + b.HasOne("Web_Jarmark.Data.Models.Category", "Category") + .WithMany("Products") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Cart", b => + { + b.Navigation("CartItems"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Category", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Order", b => + { + b.Navigation("OrderItems"); + }); + + modelBuilder.Entity("Web_Jarmark.Data.Models.Product", b => + { + b.Navigation("OrderItems"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.cs b/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.cs new file mode 100644 index 0000000..14d0a26 --- /dev/null +++ b/Web-Jarmark/Web-Jarmark/Migrations/20261104173931_AddCreatedAtToUsers.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Web_Jarmark.Migrations +{ + /// + public partial class AddCreatedAtToUsers : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "CreatedAt", + table: "AspNetUsers", + type: "TEXT", + nullable: true); + + migrationBuilder.AddColumn( + name: "FirstName", + table: "AspNetUsers", + type: "TEXT", + nullable: true); + + migrationBuilder.AddColumn( + name: "LastName", + table: "AspNetUsers", + type: "TEXT", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "CreatedAt", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "FirstName", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "LastName", + table: "AspNetUsers"); + } + } +} diff --git a/Web-Jarmark/Web-Jarmark/Migrations/ApplicationDbContextModelSnapshot.cs b/Web-Jarmark/Web-Jarmark/Migrations/ApplicationDbContextModelSnapshot.cs index e0e1f4f..781cb21 100644 --- a/Web-Jarmark/Web-Jarmark/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Web-Jarmark/Web-Jarmark/Migrations/ApplicationDbContextModelSnapshot.cs @@ -178,6 +178,9 @@ namespace Web_Jarmark.Migrations .IsConcurrencyToken() .HasColumnType("TEXT"); + b.Property("CreatedAt") + .HasColumnType("TEXT"); + b.Property("Email") .HasMaxLength(256) .HasColumnType("TEXT"); @@ -185,6 +188,12 @@ namespace Web_Jarmark.Migrations b.Property("EmailConfirmed") .HasColumnType("INTEGER"); + b.Property("FirstName") + .HasColumnType("TEXT"); + + b.Property("LastName") + .HasColumnType("TEXT"); + b.Property("LockoutEnabled") .HasColumnType("INTEGER"); diff --git a/Web-Jarmark/Web-Jarmark/Program.cs b/Web-Jarmark/Web-Jarmark/Program.cs index 3200a5f..c6da9b7 100644 --- a/Web-Jarmark/Web-Jarmark/Program.cs +++ b/Web-Jarmark/Web-Jarmark/Program.cs @@ -1,3 +1,4 @@ + using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -7,24 +8,28 @@ using Web_Jarmark.Data; var builder = WebApplication.CreateBuilder(args); -// Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.Services.AddAuthorization(); builder.Services.AddCascadingAuthenticationState(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddAuthentication(options => - { - options.DefaultScheme = IdentityConstants.ApplicationScheme; - options.DefaultSignInScheme = IdentityConstants.ExternalScheme; - }) - .AddIdentityCookies(); +{ + options.DefaultScheme = IdentityConstants.ApplicationScheme; + options.DefaultSignInScheme = IdentityConstants.ExternalScheme; +}) +.AddIdentityCookies(); + +var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") + ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); + builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddIdentityCore(options => @@ -39,9 +44,75 @@ builder.Services.AddIdentityCore(options => builder.Services.AddSingleton, IdentityNoOpEmailSender>(); builder.Services.AddHttpContextAccessor(); -var app = builder.Build(); -// Configure the HTTP request pipeline. +var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var userManager = scope.ServiceProvider + .GetRequiredService>(); + + var roleManager = scope.ServiceProvider + .GetRequiredService>(); + + const string adminEmail = "markus@bumbus.cz"; + + // Vytvoření role Admin, pokud ještě neexistuje + if (!await roleManager.RoleExistsAsync("Admin")) + { + await roleManager.CreateAsync(new IdentityRole("Admin")); + } + + // Vyhledání administrátorského účtu + var adminUser = await userManager.FindByEmailAsync(adminEmail); + + if (adminUser != null) + { + if (!await userManager.IsInRoleAsync(adminUser, "Admin")) + { + await userManager.AddToRoleAsync(adminUser, "Admin"); + } + } + else + { + throw new Exception( + $"Administrátorský účet {adminEmail} nebyl nalezen."); + } +} +// Vytvoření rolí a nastavení administrátora +using (var scope = app.Services.CreateScope()) +{ + var roleManager = scope.ServiceProvider.GetRequiredService>(); + var userManager = scope.ServiceProvider.GetRequiredService>(); + + if (!await roleManager.RoleExistsAsync("Admin")) + { + await roleManager.CreateAsync(new IdentityRole("Admin")); + } + + if (!await roleManager.RoleExistsAsync("User")) + { + await roleManager.CreateAsync(new IdentityRole("User")); + } + + var adminEmail = app.Configuration["InitialAdmin:Email"]; + + if (!string.IsNullOrWhiteSpace(adminEmail)) + { + var admin = await userManager.FindByEmailAsync(adminEmail); + + if (admin != null && !await userManager.IsInRoleAsync(admin, "Admin")) + { + var result = await userManager.AddToRoleAsync(admin, "Admin"); + + if (!result.Succeeded) + { + throw new InvalidOperationException( + string.Join(", ", result.Errors.Select(e => e.Description))); + } + } + } +} + if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); @@ -49,12 +120,15 @@ if (app.Environment.IsDevelopment()) else { app.UseExceptionHandler("/Error", createScopeForErrors: true); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } + app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true); app.UseHttpsRedirection(); +app.UseAuthentication(); +app.UseAuthorization(); + app.Use(async (context, next) => { const string cookieName = "CartToken"; @@ -80,10 +154,10 @@ app.Use(async (context, next) => app.UseAntiforgery(); app.MapStaticAssets(); + app.MapRazorComponents() .AddInteractiveServerRenderMode(); -// Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); app.Run(); diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db b/Web-Jarmark/Web-Jarmark/jarmark.db index 470db60..ba1b386 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db and b/Web-Jarmark/Web-Jarmark/jarmark.db differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-shm b/Web-Jarmark/Web-Jarmark/jarmark.db-shm index 1944f3c..fe9ac28 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-shm and b/Web-Jarmark/Web-Jarmark/jarmark.db-shm differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-wal b/Web-Jarmark/Web-Jarmark/jarmark.db-wal index 3d60585..e69de29 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-wal and b/Web-Jarmark/Web-Jarmark/jarmark.db-wal differ