-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRoleInitializer.cs
More file actions
58 lines (50 loc) · 2.08 KB
/
RoleInitializer.cs
File metadata and controls
58 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using HwProj.EventBus.Client.Interfaces;
using HwProj.Models.Roles;
using HwProj.NotificationService.Events.AuthService;
using User = HwProj.AuthService.API.Models.User;
namespace HwProj.AuthService.API
{
public class RoleInitializer
{
private static IdentityRole _lecturer = new IdentityRole(Roles.LecturerRole);
private static IdentityRole _student = new IdentityRole(Roles.StudentRole);
private static IdentityRole _expert = new IdentityRole(Roles.ExpertRole);
public static async Task InitializeAsync(UserManager<User> userManager, RoleManager<IdentityRole> roleManager, IEventBus eventBus)
{
if(await roleManager.FindByNameAsync(Roles.LecturerRole) == null)
{
await roleManager.CreateAsync(_lecturer);
}
if (await roleManager.FindByNameAsync(Roles.StudentRole) == null)
{
await roleManager.CreateAsync(_student);
}
if (await roleManager.FindByNameAsync(Roles.ExpertRole) == null)
{
await roleManager.CreateAsync(_expert);
}
const string email = "admin@gmail.com";
const string password = "Admin@1234";
if (await userManager.FindByEmailAsync(email) == null)
{
var admin = new User {
Email = email,
Name = "Admin",
Surname = "Adminov",
UserName = "Admin"
};
var result = await userManager.CreateAsync(admin, password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(admin, Roles.LecturerRole); //TODO: dangerous
admin.EmailConfirmed = true;
await userManager.UpdateAsync(admin);
var @event = new AdminRegisterEvent(admin.Id, admin.Email, admin.Name);
eventBus.Publish(@event);
}
}
}
}
}