-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultUserssAndRoles.cs
More file actions
71 lines (68 loc) · 2.43 KB
/
DefaultUserssAndRoles.cs
File metadata and controls
71 lines (68 loc) · 2.43 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
59
60
61
62
63
64
65
66
67
68
69
70
71
using AcademyManager.Models;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AcademyManager
{
//This class creates the default users and roles for the app
public static class DefaultUserssAndRoles
{
public static void CreateDefaultUsersAndRoles(UserManager<AMUser> userManager, RoleManager<IdentityRole> roleManager)
{
DefaultRoles(roleManager);
DefaultUsers(userManager);
}
/*
this function checks if the default app user admin@localhost.com already exists and creates it if it is not existing already
add also adds the default user to to Administrator role
*/
private static void DefaultUsers(UserManager<AMUser> userManager)
{
if (userManager.FindByNameAsync("admin@localhost.com").Result == null)
{
var user = new AMUser
{
UserName = "admin@localhost.com",
Email = "admin@localhost.com"
};
var result = userManager.CreateAsync(user, "Elvis1$").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "Administrator");
}
}
}
/*
This function checks if the three default roles of the application exists already or creates them if they do not exist
*/
private static void DefaultRoles(RoleManager<IdentityRole> roleManager)
{
if (!roleManager.RoleExistsAsync("Administrator").Result)
{
var role = new IdentityRole
{
Name = "Administrator"
};
var result = roleManager.CreateAsync(role).Result;
}
if (!roleManager.RoleExistsAsync("Facilitator").Result)
{
var role = new IdentityRole
{
Name = "Facilitator"
};
var result = roleManager.CreateAsync(role).Result;
}
if (!roleManager.RoleExistsAsync("Trainee").Result)
{
var role = new IdentityRole
{
Name = "Trainee"
};
var result = roleManager.CreateAsync(role).Result;
}
}
}
}