-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRoleInitializer.cs
More file actions
48 lines (42 loc) · 1.64 KB
/
RoleInitializer.cs
File metadata and controls
48 lines (42 loc) · 1.64 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
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using HwProj.AuthService.API.Events;
using HwProj.EventBus.Client.Interfaces;
using HwProj.Models.Roles;
using HwProj.Models.AuthService.ViewModels;
namespace HwProj.AuthService.API
{
public class RoleInitializer
{
public static async Task InitializeAsync(UserManager<UserViewModel> userManager, RoleManager<IdentityRole> roleManager, IEventBus eventBus)
{
if(await roleManager.FindByNameAsync(Roles.LecturerRole) == null)
{
await roleManager.CreateAsync(Roles.Lecturer);
}
if (await roleManager.FindByNameAsync(Roles.StudentRole) == null)
{
await roleManager.CreateAsync(Roles.Student);
}
const string email = "admin@gmail.com";
const string password = "Admin@1234";
if (await userManager.FindByEmailAsync(email) == null)
{
var admin = new UserViewModel {
Email = email,
Name = "Admin",
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);
}
}
}
}
}