diff --git a/AnalysisData/AnalysisData/Authorization.cs b/AnalysisData/AnalysisData/Authorization.cs index d6567ca..e86a715 100644 --- a/AnalysisData/AnalysisData/Authorization.cs +++ b/AnalysisData/AnalysisData/Authorization.cs @@ -1,6 +1,7 @@ using System.Text; using AnalysisData.Repository.RoleRepository.Abstraction; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; using Microsoft.IdentityModel.Tokens; namespace AnalysisData; @@ -14,7 +15,7 @@ public Authorization(IRoleRepository roleRepository) _roleRepository = roleRepository; } - public async Task ConfigureServices(IServiceCollection services) + public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => @@ -25,24 +26,25 @@ public async Task ConfigureServices(IServiceCollection services) ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, - ValidIssuer = "yourIssuer", - ValidAudience = "yourAudience", + ValidIssuer = "", + ValidAudience = "", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("yourSecretKey")) }; }); + services.AddAuthorization(); + } + + public async Task InitializeRolesAsync(IServiceProvider serviceProvider) + { var goldRoles = await _roleRepository.GetRolesByPolicyAsync("Gold"); var silverRoles = await _roleRepository.GetRolesByPolicyAsync("Silver"); var bronzeRoles = await _roleRepository.GetRolesByPolicyAsync("Bronze"); - services.AddAuthorization(options => - { - options.AddPolicy("gold", policy => - policy.RequireRole(goldRoles.ToArray())); - options.AddPolicy("silver", policy => - policy.RequireRole(silverRoles.ToArray())); - - options.AddPolicy("bronze", policy => - policy.RequireRole(bronzeRoles.ToArray())); - }); + + var authorizationOptions = serviceProvider.GetRequiredService(); + + authorizationOptions.AddPolicy("gold", policy => policy.RequireRole(goldRoles.ToArray())); + authorizationOptions.AddPolicy("silver", policy => policy.RequireRole(silverRoles.ToArray())); + authorizationOptions.AddPolicy("bronze", policy => policy.RequireRole(bronzeRoles.ToArray())); } } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.Designer.cs b/AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.Designer.cs deleted file mode 100644 index e59fe5b..0000000 --- a/AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.Designer.cs +++ /dev/null @@ -1,402 +0,0 @@ -// -using System; -using AnalysisData.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace AnalysisData.Migrations -{ - [DbContext(typeof(ApplicationDbContext))] - [Migration("20240830211432_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.7") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("AnalysisData.EAV.Model.AttributeEdge", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("AttributeEdges"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.AttributeNode", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("AttributeNodes"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.Category", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("Categories"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.EntityEdge", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("EntityIDSource") - .HasColumnType("integer"); - - b.Property("EntityIDTarget") - .HasColumnType("integer"); - - b.HasKey("Id"); - - b.ToTable("EntityEdges"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.EntityNode", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasMaxLength(100) - .HasColumnType("character varying(100)"); - - b.Property("NodeFileReferenceId") - .HasColumnType("integer"); - - b.HasKey("Id"); - - b.HasIndex("NodeFileReferenceId"); - - b.ToTable("EntityNodes"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.FileEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("CategoryId") - .HasColumnType("integer"); - - b.Property("FileName") - .IsRequired() - .HasColumnType("text"); - - b.Property("UploadDate") - .HasColumnType("timestamp with time zone"); - - b.Property("UploaderId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("CategoryId"); - - b.HasIndex("UploaderId"); - - b.ToTable("FileUploadedDb"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.UserFile", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("FileId") - .HasColumnType("integer"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("FileId"); - - b.HasIndex("UserId"); - - b.ToTable("UserFiles"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.ValueEdge", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("AttributeId") - .HasColumnType("integer"); - - b.Property("EntityId") - .HasColumnType("integer"); - - b.Property("Value") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.HasIndex("AttributeId"); - - b.HasIndex("EntityId"); - - b.ToTable("ValueEdges"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.ValueNode", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("AttributeId") - .HasColumnType("integer"); - - b.Property("EntityId") - .HasColumnType("integer"); - - b.Property("Value") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.HasIndex("AttributeId"); - - b.HasIndex("EntityId"); - - b.ToTable("ValueNodes"); - }); - - modelBuilder.Entity("AnalysisData.UserManage.Model.Role", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("RoleName") - .IsRequired() - .HasColumnType("text"); - - b.Property("RolePolicy") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("Roles"); - }); - - modelBuilder.Entity("AnalysisData.UserManage.Model.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("Email") - .IsRequired() - .HasColumnType("text"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("text"); - - b.Property("ImageURL") - .HasColumnType("text"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Password") - .IsRequired() - .HasColumnType("text"); - - b.Property("PhoneNumber") - .IsRequired() - .HasColumnType("text"); - - b.Property("RoleId") - .HasColumnType("integer"); - - b.Property("Username") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.HasIndex("RoleId"); - - b.ToTable("Users"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.EntityNode", b => - { - b.HasOne("AnalysisData.EAV.Model.FileEntity", "FileEntity") - .WithMany("EntityNodes") - .HasForeignKey("NodeFileReferenceId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("FileEntity"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.FileEntity", b => - { - b.HasOne("AnalysisData.EAV.Model.Category", "Category") - .WithMany() - .HasForeignKey("CategoryId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("AnalysisData.UserManage.Model.User", "User") - .WithMany("UploadData") - .HasForeignKey("UploaderId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Category"); - - b.Navigation("User"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.UserFile", b => - { - b.HasOne("AnalysisData.EAV.Model.FileEntity", "FileEntity") - .WithMany() - .HasForeignKey("FileId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("AnalysisData.UserManage.Model.User", "User") - .WithMany("UserFiles") - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("FileEntity"); - - b.Navigation("User"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.ValueEdge", b => - { - b.HasOne("AnalysisData.EAV.Model.AttributeEdge", "Attribute") - .WithMany() - .HasForeignKey("AttributeId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("AnalysisData.EAV.Model.EntityEdge", "Entity") - .WithMany() - .HasForeignKey("EntityId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Attribute"); - - b.Navigation("Entity"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.ValueNode", b => - { - b.HasOne("AnalysisData.EAV.Model.AttributeNode", "Attribute") - .WithMany() - .HasForeignKey("AttributeId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("AnalysisData.EAV.Model.EntityNode", "Entity") - .WithMany() - .HasForeignKey("EntityId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Attribute"); - - b.Navigation("Entity"); - }); - - modelBuilder.Entity("AnalysisData.UserManage.Model.User", b => - { - b.HasOne("AnalysisData.UserManage.Model.Role", "Role") - .WithMany() - .HasForeignKey("RoleId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Role"); - }); - - modelBuilder.Entity("AnalysisData.EAV.Model.FileEntity", b => - { - b.Navigation("EntityNodes"); - }); - - modelBuilder.Entity("AnalysisData.UserManage.Model.User", b => - { - b.Navigation("UploadData"); - - b.Navigation("UserFiles"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.cs b/AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.cs deleted file mode 100644 index c952907..0000000 --- a/AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - -namespace AnalysisData.Migrations -{ - /// - public partial class SeedData : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.InsertData( - table: "Roles", - columns: new[] { "Id", "RoleName", "RolePolicy" }, - values: new object[,] - { - { 1, "admin", "gold" }, - { 2, "Data-Analyst", "bronze" }, - { 3, "Data-Manager", "silver" } - }); - - migrationBuilder.InsertData( - table: "Users", - columns: new[] { "Id", "Email", "FirstName", "ImageURL", "LastName", "Password", "PhoneNumber", "RoleId", "Username" }, - values: new object[] { new Guid("0cbdbc08-e5c5-4faf-a8bc-f6c2bda55308"), "admin@gmail.com", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DeleteData( - table: "Roles", - keyColumn: "Id", - keyValue: 2); - - migrationBuilder.DeleteData( - table: "Roles", - keyColumn: "Id", - keyValue: 3); - - migrationBuilder.DeleteData( - table: "Users", - keyColumn: "Id", - keyValue: new Guid("0cbdbc08-e5c5-4faf-a8bc-f6c2bda55308")); - - migrationBuilder.DeleteData( - table: "Roles", - keyColumn: "Id", - keyValue: 1); - } - } -} diff --git a/AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.Designer.cs b/AnalysisData/AnalysisData/Migrations/20240902082451_initial.Designer.cs similarity index 98% rename from AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.Designer.cs rename to AnalysisData/AnalysisData/Migrations/20240902082451_initial.Designer.cs index 68a1bdc..3dce538 100644 --- a/AnalysisData/AnalysisData/Migrations/20240901184817_SeedData.Designer.cs +++ b/AnalysisData/AnalysisData/Migrations/20240902082451_initial.Designer.cs @@ -12,8 +12,8 @@ namespace AnalysisData.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20240901184817_SeedData")] - partial class SeedData + [Migration("20240902082451_initial")] + partial class initial { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -309,7 +309,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasData( new { - Id = new Guid("0cbdbc08-e5c5-4faf-a8bc-f6c2bda55308"), + Id = new Guid("88d6055e-bb66-4577-9584-63617a465e05"), Email = "admin@gmail.com", FirstName = "admin", LastName = "admin", diff --git a/AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.cs b/AnalysisData/AnalysisData/Migrations/20240902082451_initial.cs similarity index 93% rename from AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.cs rename to AnalysisData/AnalysisData/Migrations/20240902082451_initial.cs index 26bc969..a70599b 100644 --- a/AnalysisData/AnalysisData/Migrations/20240830211432_InitialCreate.cs +++ b/AnalysisData/AnalysisData/Migrations/20240902082451_initial.cs @@ -4,10 +4,12 @@ #nullable disable +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + namespace AnalysisData.Migrations { /// - public partial class InitialCreate : Migration + public partial class initial : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -231,6 +233,21 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Cascade); }); + migrationBuilder.InsertData( + table: "Roles", + columns: new[] { "Id", "RoleName", "RolePolicy" }, + values: new object[,] + { + { 1, "admin", "gold" }, + { 2, "Data-Analyst", "bronze" }, + { 3, "Data-Manager", "silver" } + }); + + migrationBuilder.InsertData( + table: "Users", + columns: new[] { "Id", "Email", "FirstName", "ImageURL", "LastName", "Password", "PhoneNumber", "RoleId", "Username" }, + values: new object[] { new Guid("88d6055e-bb66-4577-9584-63617a465e05"), "admin@gmail.com", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" }); + migrationBuilder.CreateIndex( name: "IX_EntityNodes_NodeFileReferenceId", table: "EntityNodes", diff --git a/AnalysisData/AnalysisData/Migrations/ApplicationDbContextModelSnapshot.cs b/AnalysisData/AnalysisData/Migrations/ApplicationDbContextModelSnapshot.cs index aab61c4..aa7025f 100644 --- a/AnalysisData/AnalysisData/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/AnalysisData/AnalysisData/Migrations/ApplicationDbContextModelSnapshot.cs @@ -306,7 +306,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasData( new { - Id = new Guid("0cbdbc08-e5c5-4faf-a8bc-f6c2bda55308"), + Id = new Guid("88d6055e-bb66-4577-9584-63617a465e05"), Email = "admin@gmail.com", FirstName = "admin", LastName = "admin", diff --git a/AnalysisData/AnalysisData/Program.cs b/AnalysisData/AnalysisData/Program.cs index 8026d51..f5a4193 100644 --- a/AnalysisData/AnalysisData/Program.cs +++ b/AnalysisData/AnalysisData/Program.cs @@ -16,13 +16,12 @@ var connectionString = builder.Configuration["CONNECTION_STRING"]; builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); -var services = builder.Services.BuildServiceProvider(); -var authorization = services.GetRequiredService(); -await authorization.ConfigureServices(builder.Services); +var services = builder.Services.BuildServiceProvider(); var app = builder.Build(); - +var authorization = app.Services.GetRequiredService(); +await authorization.InitializeRolesAsync(app.Services); if (app.Environment.IsDevelopment())