Backend login and register
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.Configuration;
|
||||
|
||||
public class RoleConfiguration : IEntityTypeConfiguration<Role>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Role> builder)
|
||||
{
|
||||
builder.ToTable("Roles", "auth");
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Name)
|
||||
.IsRequired()
|
||||
.HasMaxLength(50);
|
||||
|
||||
builder.HasMany(x => x.UserRoles)
|
||||
.WithOne(x => x.Role)
|
||||
.HasForeignKey(x => x.RoleId);
|
||||
|
||||
builder.HasData(
|
||||
new Role { Id = 1, Name = "SuperAdmin" },
|
||||
new Role { Id = 2, Name = "Admin" },
|
||||
new Role { Id = 3, Name = "User" }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.Configuration;
|
||||
|
||||
public class UserConfiguration : IEntityTypeConfiguration<User>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<User> builder)
|
||||
{
|
||||
builder.ToTable("Users", "auth");
|
||||
|
||||
builder.HasKey(k => k.Id);
|
||||
|
||||
builder.Property(x => x.Username)
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.Property(x => x.Password)
|
||||
.IsRequired()
|
||||
.HasMaxLength(250)
|
||||
.HasColumnType("varchar(250)");
|
||||
|
||||
builder.Property(x => x.Email)
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)")
|
||||
.HasAnnotation("RegularExpression", @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
|
||||
|
||||
builder.HasMany(x => x.UserRoles)
|
||||
.WithOne(x => x.User)
|
||||
.HasForeignKey(x => x.UserId);
|
||||
|
||||
builder.HasData(
|
||||
new User
|
||||
{
|
||||
Id = 1, Username = "Superadmin", Email = "superadmin@wenske-services-development.de",
|
||||
Password = "AQAAAAIAAYagAAAAEADJEu1s5qUJyP4gDUrBGyqSNtKU2IKBpZm0JqfyvOkJnqVeOHZBUrEhNr7IdQRDBQ=="
|
||||
},
|
||||
new User
|
||||
{
|
||||
Id = 2, Username = "Admin", Email = "admin@wenske-services-development.de",
|
||||
Password = "AQAAAAIAAYagAAAAEIOUiJUfMrM1Lpt4Ae3FLQOB/Bk6WHtndRAWUp132afVunMvRqkT6Hhh+27kkNW8YQ=="
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.Configuration;
|
||||
|
||||
public class UserRoleConfiguration : IEntityTypeConfiguration<UserRole>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<UserRole> builder)
|
||||
{
|
||||
builder.ToTable("UserRoles", "auth");
|
||||
builder.HasKey(x => new { x.UserId, x.RoleId });
|
||||
builder.HasOne(x => x.User)
|
||||
.WithMany(x => x.UserRoles)
|
||||
.HasForeignKey(x => x.UserId);
|
||||
|
||||
builder.HasOne(x => x.Role)
|
||||
.WithMany(x => x.UserRoles)
|
||||
.HasForeignKey(x => x.RoleId);
|
||||
|
||||
builder.HasData(
|
||||
new UserRole { UserId = 1, RoleId = 1 },
|
||||
new UserRole { UserId = 2, RoleId = 2 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Context;
|
||||
|
||||
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Role> Roles { get; set; }
|
||||
public DbSet<UserRole> UserRoles { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Infrastructure.Utilities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Infrastructure.Context;
|
||||
|
||||
public static class DbContextOptionsFactory
|
||||
{
|
||||
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
|
||||
{
|
||||
public ApplicationDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
// Build the configuration manually
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../API"))
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddUserSecrets<ApplicationDbContextFactory>()
|
||||
.Build();
|
||||
// Read PostgreSQLSettings from configuration
|
||||
var postgreSqlSettings = configuration.GetRequiredSection("PostgreSQLSettings").Get<PostgreSqlSettings>();
|
||||
var connectionString = postgreSqlSettings?.ConnectionString;
|
||||
var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
if (connectionString != null)
|
||||
optionBuilder.UseNpgsql(connectionString);
|
||||
|
||||
return new ApplicationDbContext(optionBuilder.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Domain.Interface;
|
||||
using Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Infrastructure.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
// Register repositories and unit of work
|
||||
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
|
||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<IUserRoleRepository, UserRoleRepository>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,32 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UserSecretsId>9c947c10-2373-4590-92a9-e5fe6b759c69</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Migrations\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.3.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Infrastructure.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20260206104345_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Roles", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Name = "SuperAdmin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Admin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 3,
|
||||
Name = "User"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)")
|
||||
.HasAnnotation("RegularExpression", "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
||||
|
||||
b.Property<DateTime>("LastLogin")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasMaxLength(250)
|
||||
.HasColumnType("varchar(250)");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime?>("RefreshTokenExpiryTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("ResetPasswordToken")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ResetPasswordTokenExpiryTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Email = "admin@rss.wenske-services-development.de",
|
||||
LastLogin = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Password = "AQAAAAIAAYagAAAAELBxAsYVTssn5taCQ7CMo+Mzn0i87Jt8ZXJTe7cgG5hfN3wDJzIkQaotyFhM/mQGaQ==",
|
||||
ResetPasswordTokenExpiryTime = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Username = "Superadmin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Email = "info@rss.wenske-services-development.de",
|
||||
LastLogin = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Password = "AQAAAAIAAYagAAAAEP0NWiTTz20doMf/KL0WkBR+5roc5KTouMrfiHk2MMXOQn+E+C5Q4dqWD7PnNoxUmQ==",
|
||||
ResetPasswordTokenExpiryTime = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Username = "Admin"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
UserId = 1,
|
||||
RoleId = 1
|
||||
},
|
||||
new
|
||||
{
|
||||
UserId = 2,
|
||||
RoleId = 2
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.EnsureSchema(
|
||||
name: "auth");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Roles",
|
||||
schema: "auth",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Roles", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
schema: "auth",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
Username = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
|
||||
Email = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false),
|
||||
Password = table.Column<string>(type: "varchar(250)", maxLength: 250, nullable: false),
|
||||
LastLogin = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
RefreshToken = table.Column<string>(type: "text", nullable: true),
|
||||
RefreshTokenExpiryTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
||||
ResetPasswordToken = table.Column<string>(type: "text", nullable: true),
|
||||
ResetPasswordTokenExpiryTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserRoles",
|
||||
schema: "auth",
|
||||
columns: table => new
|
||||
{
|
||||
UserId = table.Column<int>(type: "integer", nullable: false),
|
||||
RoleId = table.Column<int>(type: "integer", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId });
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRoles_Roles_RoleId",
|
||||
column: x => x.RoleId,
|
||||
principalSchema: "auth",
|
||||
principalTable: "Roles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRoles_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalSchema: "auth",
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
schema: "auth",
|
||||
table: "Roles",
|
||||
columns: new[] { "Id", "Name" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ 1, "SuperAdmin" },
|
||||
{ 2, "Admin" },
|
||||
{ 3, "User" }
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
schema: "auth",
|
||||
table: "Users",
|
||||
columns: new[] { "Id", "Email", "LastLogin", "Password", "RefreshToken", "RefreshTokenExpiryTime", "ResetPasswordToken", "ResetPasswordTokenExpiryTime", "Username" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ 1, "admin@rss.wenske-services-development.de", new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "AQAAAAIAAYagAAAAELBxAsYVTssn5taCQ7CMo+Mzn0i87Jt8ZXJTe7cgG5hfN3wDJzIkQaotyFhM/mQGaQ==", null, null, null, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Superadmin" },
|
||||
{ 2, "info@rss.wenske-services-development.de", new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "AQAAAAIAAYagAAAAEP0NWiTTz20doMf/KL0WkBR+5roc5KTouMrfiHk2MMXOQn+E+C5Q4dqWD7PnNoxUmQ==", null, null, null, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Admin" }
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
schema: "auth",
|
||||
table: "UserRoles",
|
||||
columns: new[] { "RoleId", "UserId" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ 1, 1 },
|
||||
{ 2, 2 }
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserRoles_RoleId",
|
||||
schema: "auth",
|
||||
table: "UserRoles",
|
||||
column: "RoleId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "UserRoles",
|
||||
schema: "auth");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Roles",
|
||||
schema: "auth");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users",
|
||||
schema: "auth");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Infrastructure.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Roles", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Name = "SuperAdmin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Admin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 3,
|
||||
Name = "User"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)")
|
||||
.HasAnnotation("RegularExpression", "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
||||
|
||||
b.Property<DateTime>("LastLogin")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasMaxLength(250)
|
||||
.HasColumnType("varchar(250)");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime?>("RefreshTokenExpiryTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("ResetPasswordToken")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ResetPasswordTokenExpiryTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Email = "admin@rss.wenske-services-development.de",
|
||||
LastLogin = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Password = "AQAAAAIAAYagAAAAELBxAsYVTssn5taCQ7CMo+Mzn0i87Jt8ZXJTe7cgG5hfN3wDJzIkQaotyFhM/mQGaQ==",
|
||||
ResetPasswordTokenExpiryTime = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Username = "Superadmin"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Email = "info@rss.wenske-services-development.de",
|
||||
LastLogin = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Password = "AQAAAAIAAYagAAAAEP0NWiTTz20doMf/KL0WkBR+5roc5KTouMrfiHk2MMXOQn+E+C5Q4dqWD7PnNoxUmQ==",
|
||||
ResetPasswordTokenExpiryTime = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
Username = "Admin"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles", "auth");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
UserId = 1,
|
||||
RoleId = 1
|
||||
},
|
||||
new
|
||||
{
|
||||
UserId = 2,
|
||||
RoleId = 2
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Domain.Interface;
|
||||
using Infrastructure.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Repositories;
|
||||
|
||||
public class GenericRepository<TEntity>(ApplicationDbContext applicationDbContext)
|
||||
: IGenericRepository<TEntity> where TEntity : class
|
||||
{
|
||||
protected readonly DbSet<TEntity> DbSet = applicationDbContext.Set<TEntity>();
|
||||
|
||||
public async Task<TEntity> AddAsync(TEntity entity)
|
||||
{
|
||||
var entry = await DbSet.AddAsync(entity);
|
||||
return entry.Entity;
|
||||
}
|
||||
|
||||
public void Delete(TEntity entity)
|
||||
{
|
||||
DbSet.Remove(entity);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<TEntity>> GetAllAsync()
|
||||
{
|
||||
return await DbSet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<TEntity?> GetByIdAsync(int id)
|
||||
{
|
||||
return await DbSet.FindAsync(id);
|
||||
}
|
||||
|
||||
public TEntity Update(TEntity entity)
|
||||
{
|
||||
var entry = DbSet.Update(entity);
|
||||
return entry.Entity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Domain.Interface;
|
||||
using Infrastructure.Context;
|
||||
|
||||
namespace Infrastructure.Repositories;
|
||||
|
||||
public class UnitOfWork(ApplicationDbContext context) : IUnitOfWork
|
||||
{
|
||||
public void Commit()
|
||||
{
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
public async Task CommitAsync()
|
||||
{
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Domain.Entities;
|
||||
using Domain.Interface;
|
||||
using Infrastructure.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Repositories;
|
||||
|
||||
public class UserRepository(ApplicationDbContext applicationDbContext)
|
||||
: GenericRepository<User>(applicationDbContext), IUserRepository
|
||||
{
|
||||
public async Task<User?> GetUserByEmailAsync(string email)
|
||||
{
|
||||
return await DbSet.FirstOrDefaultAsync(u => u.Email == email);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAsync(string username)
|
||||
{
|
||||
return await DbSet.FirstOrDefaultAsync(u => u.Username == username);
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetUserRolesByEmailAsync(string email)
|
||||
{
|
||||
return await DbSet
|
||||
.Where(ur => ur.Email == email)
|
||||
.SelectMany(ur => ur.UserRoles)
|
||||
.Select(ur => ur.Role!.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
{
|
||||
return await DbSet.FindAsync(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Domain.Entities;
|
||||
using Domain.Interface;
|
||||
using Infrastructure.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Repositories;
|
||||
|
||||
public class UserRoleRepository(ApplicationDbContext applicationDbContext) : IUserRoleRepository
|
||||
{
|
||||
public async Task<bool> AddRoleAsync(int userId, int roleId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userRole = new UserRole
|
||||
{
|
||||
UserId = userId,
|
||||
RoleId = roleId
|
||||
};
|
||||
await applicationDbContext.UserRoles.AddAsync(userRole);
|
||||
var result = await applicationDbContext.SaveChangesAsync() > 0;
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveRoleAsync(int userId, int roleId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userRole =
|
||||
await applicationDbContext.UserRoles.FirstOrDefaultAsync(u => u.UserId == userId && u.RoleId == roleId);
|
||||
if (userRole is null) return false;
|
||||
applicationDbContext.UserRoles.Remove(userRole);
|
||||
var result = await applicationDbContext.SaveChangesAsync() > 0;
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> HasRoleAsync(int userId, int roleId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var isUserHasRole =
|
||||
await applicationDbContext.UserRoles.AnyAsync(u => u.UserId == userId && u.RoleId == roleId);
|
||||
return isUserHasRole;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
namespace Infrastructure.Utilities;
|
||||
|
||||
public static class EmailBody
|
||||
{
|
||||
public static string EmailStringBody(string email, string emailToken)
|
||||
{
|
||||
// URL-encode parameters for security
|
||||
var encodedEmail = Uri.EscapeDataString(email);
|
||||
var encodedToken = Uri.EscapeDataString(emailToken);
|
||||
|
||||
return $@"<!DOCTYPE html>
|
||||
<html lang=""en"">
|
||||
<head>
|
||||
<meta charset=""UTF-8"">
|
||||
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
|
||||
<title>Reset Your Password</title>
|
||||
</head>
|
||||
<body style=""margin: 0; padding: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f4f4;"">
|
||||
<table width=""100%"" cellpadding=""0"" cellspacing=""0"" style=""background-color: #f4f4f4; padding: 20px 0;"">
|
||||
<tr>
|
||||
<td align=""center"">
|
||||
<table width=""600"" cellpadding=""0"" cellspacing=""0"" style=""background: linear-gradient(#4d82c3, #605d5d, #fda400) no-repeat; border-radius: 10px; overflow: hidden; max-width: 100%;"">
|
||||
<tr>
|
||||
<td style=""padding: 40px 30px; background-color: rgba(255, 255, 255, 0.95); margin: 20px;"">
|
||||
<h1 style=""color: #333; text-align: center; margin: 0 0 20px 0; font-size: 28px;"">Reset Your Password</h1>
|
||||
<hr style=""border: none; border-top: 2px solid #4d82c3; margin: 20px 0;"">
|
||||
|
||||
<p style=""color: #555; text-align: center; font-size: 16px; line-height: 1.6; margin: 20px 0;"">
|
||||
You're receiving this email because you requested a password reset for your RssReader account.
|
||||
</p>
|
||||
|
||||
<p style=""color: #555; font-size: 16px; line-height: 1.6; margin: 20px 0;"">
|
||||
Please click the button below to choose a new password:
|
||||
</p>
|
||||
|
||||
<table width=""100%"" cellpadding=""0"" cellspacing=""0"" style=""margin: 30px 0;"">
|
||||
<tr>
|
||||
<td align=""center"">
|
||||
<a href=""https://rss.wenske-services-development.de/reset?email={encodedEmail}&code={encodedToken}""
|
||||
target=""_blank""
|
||||
style=""background-color: #4d82c3;
|
||||
color: #ffffff;
|
||||
padding: 15px 40px;
|
||||
text-decoration: none;
|
||||
border-radius: 5px;
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
display: inline-block;"">
|
||||
Reset Password
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p style=""color: #999; font-size: 14px; line-height: 1.6; margin: 30px 0 0 0; border-top: 1px solid #e0e0e0; padding-top: 20px;"">
|
||||
If you didn't request a password reset, you can safely ignore this email.
|
||||
</p>
|
||||
|
||||
<p style=""color: #555; font-size: 16px; margin: 30px 0 0 0;"">
|
||||
Kind regards,<br>
|
||||
<strong>The RssReader Team</strong>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Infrastructure.Utilities;
|
||||
|
||||
public abstract class GenerateRefreshTokenHelper
|
||||
{
|
||||
public static string GenerateRefreshToken()
|
||||
{
|
||||
var randomNumber = new byte[32];
|
||||
using var rng = RandomNumberGenerator.Create();
|
||||
rng.GetBytes(randomNumber);
|
||||
return Convert.ToBase64String(randomNumber);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Infrastructure.Utilities;
|
||||
|
||||
public class PostgreSqlSettings
|
||||
{
|
||||
public string? Host { get; set; }
|
||||
public string? Database { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Password { get; set; }
|
||||
|
||||
public string ConnectionString => $"Host={Host};Database={Database};Username={Username};Password={Password};";
|
||||
}
|
||||
Reference in New Issue
Block a user