Created
August 17, 2017 12:23
-
-
Save Ciwan1859/09abf7b26ca0d29f6f9e0f01ee28eddc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PermissionsService : IPermissionsService | |
{ | |
private string _userId; | |
private bool _isAdministrator; | |
private HttpContext _httpContext; | |
private readonly UserManager<BbUser> _userManager; | |
public PermissionsService(UserManager<BbUser> userManager) | |
{ | |
_userManager = userManager; | |
} | |
public async Task<Permissions> GetPermissions(HttpContext httpContext) | |
{ | |
_httpContext = httpContext; | |
PopulateUserIdAndIsAdminFlag(); | |
var permissions = new Permissions | |
{ | |
UserId = _userId, | |
IsAdministrator = _isAdministrator | |
}; | |
return await Task.Run(() => permissions); | |
} | |
private async void PopulateUserIdAndIsAdminFlag() | |
{ | |
if (!IsAuthenticated()) return; | |
var username = _httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); | |
var user = await _userManager.FindByNameAsync(username); | |
var roles = await _userManager.GetRolesAsync(user); | |
_userId = user.Id; | |
_isAdministrator = roles.Contains("Admin"); | |
} | |
#region Helper | |
private bool IsAuthenticated() | |
{ | |
return _httpContext.User.Identity.IsAuthenticated; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment