Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb49f7a

Browse files
feat: adição do arquivo MigrationBuilderExtensions.cs
1 parent def33cf commit fb49f7a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace ContainRs.Api.Identity;
4+
5+
public static class MigrationBuilderExtensions
6+
{
7+
public static void SeedIdentityData(this MigrationBuilder migrationBuilder)
8+
{
9+
string roleSuporte = "Suporte";
10+
string[] roleNames = [roleSuporte,"Cliente","Comercial","Admin"];
11+
string email = "suporte@email.org";
12+
13+
// Cria roles
14+
foreach(var roleName in roleNames)
15+
{
16+
migrationBuilder.Sql($@"
17+
IF NOT EXISTS (SELECT 1 FROM [Identity].AspNetRoles WHERE Name = '{roleName}')
18+
BEGIN
19+
INSERT INTO [Identity].AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
20+
VALUES (NEWID(), '{roleName}', UPPER('{roleName}'), NEWID())
21+
END");
22+
}
23+
24+
// Cria usuário inicial de suporte
25+
migrationBuilder.Sql($@"
26+
IF NOT EXISTS (SELECT 1 FROM [Identity].AspNetUsers WHERE Email = '{email}')
27+
BEGIN
28+
DECLARE @UserId UNIQUEIDENTIFIER = NEWID();
29+
INSERT INTO [Identity].AspNetUsers (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, PasswordHash, SecurityStamp, ConcurrencyStamp, AccessFailedCount)
30+
VALUES (@UserId, '{email}', UPPER('{email}'), '{email}', UPPER('{email}'), 1, 1, 0, 0,
31+
'AQAAAAIAAYagAAAAEPa7WUxVMskcxZNjU9sJFWCxQAn3wX4eJuALj5mxvm8wrsMkxx7wqiT/DWmKaGNGDA==',
32+
NEWID(), NEWID(), 0);
33+
34+
INSERT INTO [Identity].AspNetUserRoles (UserId, RoleId)
35+
SELECT @UserId, Id FROM [Identity].AspNetRoles WHERE Name = '{roleSuporte}'
36+
END");
37+
}
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /