4
4
using System . Linq ;
5
5
using System . Threading . Tasks ;
6
6
using Microsoft . AspNetCore . Identity ;
7
+ using Microsoft . EntityFrameworkCore ;
7
8
using Microsoft . Extensions . Logging ;
8
9
using Newtonsoft . Json ;
9
10
@@ -21,6 +22,7 @@ public static void Initialize(SecurityDbContext context, UserManager<Application
21
22
password , SuperAdmin ) . GetAwaiter ( )
22
23
. GetResult ( ) ;
23
24
CreateResources ( context , dbInitializerLogger ) ;
25
+ CreatePermissions ( context , dbInitializerLogger ) ;
24
26
}
25
27
26
28
private static void CreateRole ( SecurityDbContext context , ILogger < DbInitializer > logger , string role )
@@ -90,7 +92,7 @@ private static void AddRoleToApplicationUser(SecurityDbContext context, ILogger<
90
92
context . ApplicationUserRoles . FirstOrDefault ( x => x . UserId == user . Id && x . RoleId == applicationRole . Id ) ;
91
93
if ( userRole == null )
92
94
{
93
- ApplicationUserRole entity = new ApplicationUserRole ( ) { RoleId = applicationRole . Id , UserId = user . Id } ;
95
+ ApplicationUserRole entity = new ApplicationUserRole ( ) { RoleId = applicationRole . Id , UserId = user . Id } ;
94
96
context . ApplicationUserRoles . Add ( entity ) ;
95
97
var saveChanges = context . SaveChanges ( ) ;
96
98
if ( saveChanges == 0 )
@@ -129,7 +131,44 @@ private static void CreateResources(SecurityDbContext context, ILogger<DbInitial
129
131
}
130
132
}
131
133
}
132
-
134
+ }
135
+
136
+ private static void CreatePermissions ( SecurityDbContext context , ILogger < DbInitializer > logger )
137
+ {
138
+ var readAllText = File . ReadAllText ( "./Resources/permissions.json" ) ;
139
+ var permissions = JsonConvert . DeserializeObject < List < ApplicationPermission > > ( readAllText ) ;
140
+ foreach ( var permission in permissions )
141
+ {
142
+ logger . LogInformation ( $ "Create the permission for resource `{ permission . Resource . Name } ` and role `{ permission . Role . Name } ") ;
143
+
144
+ var role = context . ApplicationRoles . FirstOrDefault ( x=> x . Name == permission . Role . Name ) ;
145
+ var resource = context . Resources . FirstOrDefault ( x=> x . Name == permission . Resource . Name ) ;
146
+
147
+ var any = context . Permissions . Include ( x => x . Role ) . Include ( x => x . Resource ) . AsEnumerable ( )
148
+ . Any ( x => string . Equals ( x . RoleId , role . Id , StringComparison . CurrentCultureIgnoreCase )
149
+ && string . Equals ( x . ResourceId , resource . Id , StringComparison . CurrentCultureIgnoreCase ) ) ;
150
+ if ( ! any )
151
+ {
152
+ var appPermission = new ApplicationPermission ( ) {
153
+ IsAllowed = permission . IsAllowed ,
154
+ ResourceId = resource . Id ,
155
+ RoleId = role . Id
156
+ } ;
157
+ context . Permissions . Add ( appPermission ) ;
158
+ var i = context . SaveChanges ( ) ;
159
+ if ( i > 0 )
160
+ {
161
+ logger . LogDebug ( $ "Created the permission for resource `{ permission . Resource . Name } ` and role `{ permission . Role . Name } ") ;
162
+ }
163
+ else
164
+ {
165
+ ApplicationException exception =
166
+ new ApplicationException ( $ "Default resource `{ permission . Resource . Name } ` & role `{ permission . Role . Name } cannot be created") ;
167
+ logger . LogError ( exception , $ "Exception occurred. { exception . Message } ") ;
168
+ throw exception ;
169
+ }
170
+ }
171
+ }
133
172
}
134
173
}
135
174
}
0 commit comments