Fixes administration startup

This commit is contained in:
2018-12-13 15:32:37 +00:00
parent 18eadad805
commit c270005d5b

View File

@ -30,7 +30,7 @@ namespace Yavsc.Controllers
this.context = context; this.context = context;
} }
private async Task<bool> CreateRoles () { private async Task<bool> EnsureRoleList () {
// ensure all roles existence // ensure all roles existence
foreach (string roleName in new string[] { foreach (string roleName in new string[] {
Constants.AdminGroupName, Constants.AdminGroupName,
@ -67,20 +67,25 @@ namespace Yavsc.Controllers
var admins = await _userManager.GetUsersInRoleAsync(Constants.AdminGroupName); var admins = await _userManager.GetUsersInRoleAsync(Constants.AdminGroupName);
if (admins != null && admins.Count > 0) if (admins != null && admins.Count > 0)
{ {
// All is ok, nothing to do here.
if (User.IsInRole(Constants.AdminGroupName)) if (User.IsInRole(Constants.AdminGroupName))
{ {
// check all user groups exist
if (!await CreateRoles()) return Ok(new { message = "you already got it." });
return new BadRequestObjectResult(ModelState);
return Ok(new { message = "you checked the role list." });
} }
return HttpNotFound(); return HttpNotFound();
} }
var user = await _userManager.FindByIdAsync(User.GetUserId()); var user = await _userManager.FindByIdAsync(User.GetUserId());
// check all user groups exist
if (!await EnsureRoleList()) {
ModelState.AddModelError(null, "Could not ensure role list existence. aborting.");
return new BadRequestObjectResult(ModelState);
}
IdentityRole adminRole; IdentityRole adminRole;
adminRole = await _roleManager.FindByNameAsync(Constants.AdminGroupName); adminRole = await _roleManager.FindByNameAsync(Constants.AdminGroupName);
var addToRoleResult = await _userManager.AddToRoleAsync(user, Constants.AdminGroupName); var addToRoleResult = await _userManager.AddToRoleAsync(user, Constants.AdminGroupName);
if (!addToRoleResult.Succeeded) if (!addToRoleResult.Succeeded)
{ {