1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . IO ;
3
4
using System . Linq ;
4
5
using System . Threading . Tasks ;
5
6
using Microsoft . AspNetCore . Identity ;
6
7
using Microsoft . Extensions . Logging ;
8
+ using Newtonsoft . Json ;
7
9
8
10
namespace AuthWebApplication . Models . Db
9
11
{
10
12
public class DbInitializer
11
13
{
12
14
private const string SuperAdmin = "SuperAdmin" ;
13
15
16
+ public static void Initialize ( SecurityDbContext context , UserManager < ApplicationUser > userManager ,
17
+ string password , ILogger < DbInitializer > dbInitializerLogger )
18
+ {
19
+ CreateRole ( context , dbInitializerLogger , SuperAdmin ) ;
20
+ CreateDefaultUser ( context , userManager , dbInitializerLogger , "Super Admin" , "foyzulkarim@gmail.com" ,
21
+ password , SuperAdmin ) . GetAwaiter ( )
22
+ . GetResult ( ) ;
23
+ CreateResources ( context , dbInitializerLogger ) ;
24
+ }
25
+
14
26
private static void CreateRole ( SecurityDbContext context , ILogger < DbInitializer > logger , string role )
15
27
{
16
28
logger . LogInformation ( $ "Create the role `{ role } ` for application") ;
17
- var any = context . ApplicationRoles . AsEnumerable ( ) . Any ( x => string . Equals ( x . Name , role , StringComparison . CurrentCultureIgnoreCase ) ) ;
29
+ var any = context . ApplicationRoles . AsEnumerable ( )
30
+ . Any ( x => string . Equals ( x . Name , role , StringComparison . CurrentCultureIgnoreCase ) ) ;
18
31
if ( ! any )
19
32
{
20
33
var appRole = new ApplicationRole ( role ) ;
@@ -26,14 +39,17 @@ private static void CreateRole(SecurityDbContext context, ILogger<DbInitializer>
26
39
}
27
40
else
28
41
{
29
- ApplicationException exception = new ApplicationException ( $ "Default role `{ role } ` cannot be created") ;
42
+ ApplicationException exception =
43
+ new ApplicationException ( $ "Default role `{ role } ` cannot be created") ;
30
44
logger . LogError ( exception , $ "Exception occurred. { exception . Message } ") ;
31
45
throw exception ;
32
46
}
33
47
}
34
48
}
35
49
36
- private static async Task < ApplicationUser > CreateDefaultUser ( SecurityDbContext context , UserManager < ApplicationUser > userManager , ILogger < DbInitializer > logger , string displayName , string email , string password , string role )
50
+ private static async Task < ApplicationUser > CreateDefaultUser ( SecurityDbContext context ,
51
+ UserManager < ApplicationUser > userManager , ILogger < DbInitializer > logger , string displayName , string email ,
52
+ string password , string role )
37
53
{
38
54
logger . LogInformation ( $ "Create default user with email `{ email } ` for application") ;
39
55
ApplicationUser user = await userManager . FindByEmailAsync ( email ) ;
@@ -55,7 +71,8 @@ private static async Task<ApplicationUser> CreateDefaultUser(SecurityDbContext c
55
71
}
56
72
else
57
73
{
58
- ApplicationException exception = new ApplicationException ( $ "Default user `{ email } ` cannot be created") ;
74
+ ApplicationException exception =
75
+ new ApplicationException ( $ "Default user `{ email } ` cannot be created") ;
59
76
logger . LogError ( exception , $ "Exception occurred. { exception . Message } ") ;
60
77
throw exception ;
61
78
}
@@ -65,29 +82,54 @@ private static async Task<ApplicationUser> CreateDefaultUser(SecurityDbContext c
65
82
return user ;
66
83
}
67
84
68
- private static void AddRoleToApplicationUser ( SecurityDbContext context , ILogger < DbInitializer > logger , ApplicationUser user , string role )
85
+ private static void AddRoleToApplicationUser ( SecurityDbContext context , ILogger < DbInitializer > logger ,
86
+ ApplicationUser user , string role )
69
87
{
70
88
var applicationRole = context . ApplicationRoles . First ( x => x . Name == role ) ;
71
- var userRole = context . ApplicationUserRoles . FirstOrDefault ( x => x . UserId == user . Id && x . RoleId == applicationRole . Id ) ;
89
+ var userRole =
90
+ context . ApplicationUserRoles . FirstOrDefault ( x => x . UserId == user . Id && x . RoleId == applicationRole . Id ) ;
72
91
if ( userRole == null )
73
92
{
74
- ApplicationUserRole entity = new ApplicationUserRole ( ) { RoleId = applicationRole . Id , UserId = user . Id } ;
93
+ ApplicationUserRole entity = new ApplicationUserRole ( ) { RoleId = applicationRole . Id , UserId = user . Id } ;
75
94
context . ApplicationUserRoles . Add ( entity ) ;
76
95
var saveChanges = context . SaveChanges ( ) ;
77
96
if ( saveChanges == 0 )
78
97
{
79
- ApplicationException exception = new ApplicationException ( $ "Adding role to user `{ user . Email } ` cannot be done") ;
98
+ ApplicationException exception =
99
+ new ApplicationException ( $ "Adding role to user `{ user . Email } ` cannot be done") ;
80
100
logger . LogError ( exception , $ "Exception occurred. { exception . Message } ") ;
81
101
throw exception ;
82
102
}
83
103
}
84
104
}
85
-
86
- public static void Initialize ( SecurityDbContext context , UserManager < ApplicationUser > userManager , string password , ILogger < DbInitializer > dbInitializerLogger )
105
+
106
+ private static void CreateResources ( SecurityDbContext context , ILogger < DbInitializer > logger )
87
107
{
88
- CreateRole ( context , dbInitializerLogger , SuperAdmin ) ;
89
- CreateDefaultUser ( context , userManager , dbInitializerLogger , "Super Admin" , "foyzulkarim@gmail.com" , password , SuperAdmin ) . GetAwaiter ( )
90
- . GetResult ( ) ;
108
+ var readAllText = File . ReadAllText ( "./Resources/resources.json" ) ;
109
+ var resources = JsonConvert . DeserializeObject < List < ApplicationResource > > ( readAllText ) ;
110
+ foreach ( var resource in resources )
111
+ {
112
+ logger . LogInformation ( $ "Create the resource `{ resource . Name } ` for application") ;
113
+ var any = context . Resources . AsEnumerable ( )
114
+ . Any ( x => string . Equals ( x . Name , resource . Name , StringComparison . CurrentCultureIgnoreCase ) ) ;
115
+ if ( ! any )
116
+ {
117
+ context . Resources . Add ( resource ) ;
118
+ var i = context . SaveChanges ( ) ;
119
+ if ( i > 0 )
120
+ {
121
+ logger . LogDebug ( $ "Created the resource `{ resource . Name } ` successfully") ;
122
+ }
123
+ else
124
+ {
125
+ ApplicationException exception =
126
+ new ApplicationException ( $ "Default resource `{ resource . Name } ` cannot be created") ;
127
+ logger . LogError ( exception , $ "Exception occurred. { exception . Message } ") ;
128
+ throw exception ;
129
+ }
130
+ }
131
+ }
132
+
91
133
}
92
134
}
93
- }
135
+ }
0 commit comments