Skip to content

Commit c27e06b

Browse files
author
stone1985
committed
- sqlite-based auth
1 parent 2048cc2 commit c27e06b

File tree

7 files changed

+485
-17
lines changed

7 files changed

+485
-17
lines changed

Botticelli.Auth.Data.Postgres/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,26 @@ namespace Botticelli.Auth.Data.Postgres;
99
public static class ServiceCollectionExtensions
1010
{
1111
/// <summary>
12-
/// Adds a basic authorization implementation
12+
/// Adds a basic authorization implementation
1313
/// </summary>
1414
/// <param name="services"></param>
1515
/// <param name="config"></param>
1616
/// <param name="dbParameters"></param>
1717
/// <returns></returns>
18-
public static IServiceCollection AddPostgresBasicBotUserAuth(this IServiceCollection services, IConfiguration config, Action<DbContextOptionsBuilder>? dbParameters = null)
18+
public static IServiceCollection AddPostgresBasicBotUserAuth(this IServiceCollection services,
19+
IConfiguration config, Action<DbContextOptionsBuilder>? dbParameters = null)
1920
{
2021
services.AddBasicBotUserAuth();
21-
22+
2223
var settings = config.GetSection(nameof(AuthSettings))
23-
.Get<AuthSettings>();
24-
24+
.Get<AuthSettings>();
25+
2526
return services.AddDbContext<AuthDefaultDbContext>(opt =>
26-
{
27-
dbParameters?.Invoke(opt);
28-
opt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
29-
.UseNpgsql(settings!.ConnectionString);
30-
},
31-
ServiceLifetime.Singleton);
27+
{
28+
dbParameters?.Invoke(opt);
29+
opt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
30+
.UseNpgsql(settings!.ConnectionString, b => b.MigrationsAssembly("Botticelli.Auth.Data.Postgres"));
31+
},
32+
ServiceLifetime.Singleton);
3233
}
3334
}

Botticelli.Auth.Data.Sqlite/Migrations/20250308203827_auth.Designer.cs

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
7+
8+
namespace Botticelli.Auth.Data.Sqlite.Migrations
9+
{
10+
/// <inheritdoc />
11+
public partial class auth : Migration
12+
{
13+
/// <inheritdoc />
14+
protected override void Up(MigrationBuilder migrationBuilder)
15+
{
16+
migrationBuilder.EnsureSchema(
17+
name: "Botticelli.Auth.Sample.Telegram");
18+
19+
migrationBuilder.CreateTable(
20+
name: "BotUserRoles",
21+
schema: "Botticelli.Auth.Sample.Telegram",
22+
columns: table => new
23+
{
24+
Id = table.Column<Guid>(type: "TEXT", nullable: false),
25+
RoleName = table.Column<string>(type: "TEXT", maxLength: 16, nullable: false),
26+
Description = table.Column<string>(type: "TEXT", maxLength: 1024, nullable: false),
27+
IsSuperUser = table.Column<bool>(type: "INTEGER", nullable: false)
28+
},
29+
constraints: table =>
30+
{
31+
table.PrimaryKey("PK_BotUserRoles", x => x.Id);
32+
});
33+
34+
migrationBuilder.CreateTable(
35+
name: "BotUsers",
36+
schema: "Botticelli.Auth.Sample.Telegram",
37+
columns: table => new
38+
{
39+
UserId = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
40+
UserName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
41+
NickName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
42+
Email = table.Column<string>(type: "TEXT", maxLength: 254, nullable: true),
43+
FirstName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
44+
LastName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
45+
Phone = table.Column<string>(type: "TEXT", maxLength: 16, nullable: true),
46+
RoleId = table.Column<Guid>(type: "TEXT", nullable: false),
47+
IsActive = table.Column<bool>(type: "INTEGER", nullable: false)
48+
},
49+
constraints: table =>
50+
{
51+
table.PrimaryKey("PK_BotUsers", x => x.UserId);
52+
table.ForeignKey(
53+
name: "FK_BotUsers_BotUserRoles_RoleId",
54+
column: x => x.RoleId,
55+
principalSchema: "Botticelli.Auth.Sample.Telegram",
56+
principalTable: "BotUserRoles",
57+
principalColumn: "Id",
58+
onDelete: ReferentialAction.Cascade);
59+
});
60+
61+
migrationBuilder.CreateTable(
62+
name: "AccessHistory",
63+
schema: "Botticelli.Auth.Sample.Telegram",
64+
columns: table => new
65+
{
66+
Id = table.Column<Guid>(type: "TEXT", nullable: false),
67+
EntityUserId = table.Column<string>(type: "TEXT", nullable: true),
68+
TimestampUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
69+
IsSuccess = table.Column<bool>(type: "INTEGER", nullable: false),
70+
ErrorMessage = table.Column<string>(type: "TEXT", maxLength: 2048, nullable: true)
71+
},
72+
constraints: table =>
73+
{
74+
table.PrimaryKey("PK_AccessHistory", x => x.Id);
75+
table.ForeignKey(
76+
name: "FK_AccessHistory_BotUsers_EntityUserId",
77+
column: x => x.EntityUserId,
78+
principalSchema: "Botticelli.Auth.Sample.Telegram",
79+
principalTable: "BotUsers",
80+
principalColumn: "UserId");
81+
});
82+
83+
migrationBuilder.InsertData(
84+
schema: "Botticelli.Auth.Sample.Telegram",
85+
table: "BotUserRoles",
86+
columns: new[] { "Id", "Description", "IsSuperUser", "RoleName" },
87+
values: new object[,]
88+
{
89+
{ new Guid("94854345-5355-4343-3447-1122244f55a8"), "A default authorized user role", false, "User" },
90+
{ new Guid("9947e363-4255-408d-b277-33402b9f07a1"), "A default user for guest", false, "Guest" },
91+
{ new Guid("d9887829-61a7-4947-9eb6-7faa66363f08"), "Bot users administrator", true, "Admin" }
92+
});
93+
94+
migrationBuilder.CreateIndex(
95+
name: "IX_AccessHistory_EntityUserId",
96+
schema: "Botticelli.Auth.Sample.Telegram",
97+
table: "AccessHistory",
98+
column: "EntityUserId");
99+
100+
migrationBuilder.CreateIndex(
101+
name: "IX_BotUserRoles_RoleName",
102+
schema: "Botticelli.Auth.Sample.Telegram",
103+
table: "BotUserRoles",
104+
column: "RoleName",
105+
unique: true);
106+
107+
migrationBuilder.CreateIndex(
108+
name: "IX_BotUsers_RoleId",
109+
schema: "Botticelli.Auth.Sample.Telegram",
110+
table: "BotUsers",
111+
column: "RoleId");
112+
}
113+
114+
/// <inheritdoc />
115+
protected override void Down(MigrationBuilder migrationBuilder)
116+
{
117+
migrationBuilder.DropTable(
118+
name: "AccessHistory",
119+
schema: "Botticelli.Auth.Sample.Telegram");
120+
121+
migrationBuilder.DropTable(
122+
name: "BotUsers",
123+
schema: "Botticelli.Auth.Sample.Telegram");
124+
125+
migrationBuilder.DropTable(
126+
name: "BotUserRoles",
127+
schema: "Botticelli.Auth.Sample.Telegram");
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)