diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Common/GettingUserPropertiesTest.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Common/GettingUserPropertiesTest.cs new file mode 100644 index 0000000000..25f90b387f --- /dev/null +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Common/GettingUserPropertiesTest.cs @@ -0,0 +1,94 @@ +using System.Security.Authentication; +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Moq; +using NUnit.Framework; +using OutOfSchool.Common; +using OutOfSchool.WebApi.Common; + +namespace OutOfSchool.WebApi.Tests.Common +{ + [TestFixture] + public class GettingUserPropertiesTest + { + private readonly Claim userIdClaim = new Claim(IdentityResourceClaimsTypes.Sub, "38776161-734b-4aec-96eb-4a1f87a2e5f3"); + private readonly Claim userRoleClaim = new Claim(IdentityResourceClaimsTypes.Role, "Parent"); + private readonly Claim userSubroleClaim = new Claim(IdentityResourceClaimsTypes.Subrole, "None"); + private Mock httpContextMoq; + + [SetUp] + public void Setup() + { + httpContextMoq = new Mock(); + httpContextMoq.Setup(x => x.User.FindFirst("sub")).Returns(userIdClaim); + httpContextMoq.Setup(x => x.User.FindFirst("role")).Returns(userRoleClaim); + httpContextMoq.Setup(x => x.User.FindFirst("subrole")).Returns(userSubroleClaim); + } + + [Test] + public void GetUserId_ByHttpContext_ReturnsClaim() + { + // Assert + Assert.AreEqual(userIdClaim.Value, GettingUserProperties.GetUserId(httpContextMoq.Object)); + } + + [Test] + public void GetUserId_ByHttpContext_ThrowsAuthenticationException() + { + // Assert + Assert.Throws( + () => GettingUserProperties.GetUserId((HttpContext)null)); + } + + [Test] + public void GetUserId_ByClaimsPrincipal_ReturnNull() + { + // Assert + Assert.IsNull(GettingUserProperties.GetUserId((ClaimsPrincipal)null)); + } + + [Test] + public void GetUserRole_ByHttpContext_ReturnsClaim() + { + // Assert + Assert.AreEqual(userRoleClaim.Value, GettingUserProperties.GetUserRole(httpContextMoq.Object).ToString()); + } + + [Test] + public void GetUserRole_ByHttpContext_ThrowsAuthenticationException() + { + // Assert + Assert.Throws( + () => GettingUserProperties.GetUserRole((HttpContext)null)); + } + + [Test] + public void GetUserRole_ByClaimsPrincipal_ReturnNull() + { + // Assert + Assert.IsNull(GettingUserProperties.GetUserRole((ClaimsPrincipal)null)); + } + + [Test] + public void GetUserSubrole_ByHttpContext_ReturnsClaim() + { + // Assert + Assert.AreEqual(userSubroleClaim.Value, GettingUserProperties.GetUserSubrole(httpContextMoq.Object).ToString()); + } + + [Test] + public void GetUserSubrole_ByHttpContext_ThrowsAuthenticationException() + { + // Assert + Assert.Throws( + () => GettingUserProperties.GetUserSubrole((HttpContext)null)); + } + + [Test] + public void GetUserSubrole_ByClaimsPrincipal_ReturnNull() + { + // Assert + Assert.IsNull(GettingUserProperties.GetUserSubrole((ClaimsPrincipal)null)); + } + } +} diff --git a/OutOfSchool/OutOfSchool.WebApi/Common/GettingUserProperties.cs b/OutOfSchool/OutOfSchool.WebApi/Common/GettingUserProperties.cs new file mode 100644 index 0000000000..11e8847be1 --- /dev/null +++ b/OutOfSchool/OutOfSchool.WebApi/Common/GettingUserProperties.cs @@ -0,0 +1,71 @@ +using System; +using System.Security.Authentication; +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using OutOfSchool.Common; +using OutOfSchool.Common.Extensions; +using OutOfSchool.Services.Enums; + +namespace OutOfSchool.WebApi.Common +{ + public static class GettingUserProperties + { + public static string GetUserId(HttpContext httpContext) + { + var userId = GetUserId(httpContext?.User); + + if (userId is null) + { + ThrowAuthenticationException(nameof(IdentityResourceClaimsTypes.Sub)); + } + + return userId; + } + + public static string GetUserId(ClaimsPrincipal user) + { + return user?.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + } + + public static Role GetUserRole(HttpContext httpContext) + { + var userRoleName = GetUserRole(httpContext?.User); + + if (userRoleName is null) + { + ThrowAuthenticationException(nameof(IdentityResourceClaimsTypes.Role)); + } + + Role userRole = (Role)Enum.Parse(typeof(Role), userRoleName, true); + + return userRole; + } + + public static string GetUserRole(ClaimsPrincipal user) + { + return user?.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Role); + } + + public static Subrole GetUserSubrole(HttpContext httpContext) + { + var userSubroleName = GetUserSubrole(httpContext?.User); + + if (userSubroleName is null) + { + ThrowAuthenticationException(nameof(IdentityResourceClaimsTypes.Subrole)); + } + + Subrole userSubrole = (Subrole)Enum.Parse(typeof(Subrole), userSubroleName, true); + + return userSubrole; + } + + public static string GetUserSubrole(ClaimsPrincipal user) + { + return user?.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole); + } + + private static void ThrowAuthenticationException(string claimType) + => throw new AuthenticationException($"Can not get user's claim {claimType} from Context."); + } +} diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/BlockedProviderParentController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/BlockedProviderParentController.cs index 886fcac7f1..a2b1cd0dc8 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/BlockedProviderParentController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/BlockedProviderParentController.cs @@ -1,12 +1,10 @@ using System; -using System.Security.Authentication; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using OutOfSchool.Common; -using OutOfSchool.Common.Extensions; using OutOfSchool.Common.PermissionsModule; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models.BlockedProviderParent; using OutOfSchool.WebApi.Services; @@ -46,7 +44,7 @@ public BlockedProviderParentController( [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task Block(BlockedProviderParentBlockDto blockedProviderParentBlockDto) { - var userId = GetUserId(); + var userId = GettingUserProperties.GetUserId(HttpContext); var result = await blockedProviderParentService.Block(blockedProviderParentBlockDto, userId).ConfigureAwait(false); if (!result.Succeeded) @@ -79,7 +77,7 @@ public async Task Block(BlockedProviderParentBlockDto blockedProv [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task UnBlock(BlockedProviderParentUnblockDto blockedProviderParentUnblockDto) { - var userId = GetUserId(); + var userId = GettingUserProperties.GetUserId(HttpContext); var result = await blockedProviderParentService.Unblock(blockedProviderParentUnblockDto, userId).ConfigureAwait(false); if (!result.Succeeded) @@ -112,13 +110,5 @@ public async Task GetBlock(Guid parentId, Guid providerId) { return Ok(await blockedProviderParentService.GetBlock(parentId, providerId).ConfigureAwait(false)); } - - private string GetUserId() - { - var userId = HttpContext.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub) - ?? throw new AuthenticationException($"Can not get user's claim {nameof(IdentityResourceClaimsTypes.Sub)} from HttpContext."); - - return userId; - } } } diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ChatWorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ChatWorkshopController.cs index 232f423d4d..e4579bddb5 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ChatWorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ChatWorkshopController.cs @@ -11,6 +11,7 @@ using OutOfSchool.Common; using OutOfSchool.Common.Extensions; using OutOfSchool.Services.Enums; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models; using OutOfSchool.WebApi.Models.ChatWorkshop; using OutOfSchool.WebApi.Services; @@ -155,7 +156,7 @@ public Task GetProvidersRoomsAsync() private async Task IsParentAChatRoomParticipantAsync(ChatRoomWorkshopDto chatRoom) { - var userId = this.GetUserId(); + var userId = GettingUserProperties.GetUserId(HttpContext); var result = await validationService.UserIsParentOwnerAsync(userId, chatRoom.ParentId).ConfigureAwait(false); @@ -169,8 +170,8 @@ private async Task IsParentAChatRoomParticipantAsync(ChatRoomWorkshopDto c private async Task IsProviderAChatRoomParticipantAsync(ChatRoomWorkshopDto chatRoom) { - var userId = this.GetUserId(); - var userSubrole = this.GetUserSubrole(); + var userId = GettingUserProperties.GetUserId(HttpContext); + var userSubrole = GettingUserProperties.GetUserSubrole(HttpContext); var result = await validationService.UserIsWorkshopOwnerAsync(userId, chatRoom.WorkshopId, userSubrole).ConfigureAwait(false); @@ -182,34 +183,6 @@ private async Task IsProviderAChatRoomParticipantAsync(ChatRoomWorkshopDto return result; } - private string GetUserId() - { - var userId = HttpContext.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub) - ?? throw new AuthenticationException($"Can not get user's claim {nameof(IdentityResourceClaimsTypes.Sub)} from HttpContext."); - - return userId; - } - - private Role GetUserRole() - { - var userRoleName = HttpContext.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Role) - ?? throw new AuthenticationException($"Can not get user's claim {nameof(IdentityResourceClaimsTypes.Role)} from HttpContext."); - - Role userRole = (Role)Enum.Parse(typeof(Role), userRoleName, true); - - return userRole; - } - - private Subrole GetUserSubrole() - { - var userSubroleName = HttpContext.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole) - ?? throw new AuthenticationException($"Can not get user's claim {nameof(IdentityResourceClaimsTypes.Subrole)} from HttpContext."); - - Subrole userSubrole = (Subrole)Enum.Parse(typeof(Subrole), userSubroleName, true); - - return userSubrole; - } - private void LogWarningAboutUsersTryingToGetNotOwnChatRoom(Guid chatRoomId, string userId) { var messageToLog = $"User with {nameof(userId)}:{userId} is trying to get not his own chat room: {nameof(chatRoomId)}={chatRoomId}."; @@ -230,7 +203,7 @@ private async Task GetRoomByIdAsync(Guid chatRoomId, Func GetMessagesByRoomIdAsync(Guid chatRoomId, Offs if (chatRoom is null) { - var messageToLog = $"User with userId:{this.GetUserId()} is trying to get messages from not existing chat room: {nameof(chatRoomId)}={chatRoomId}."; + var messageToLog = $"User with userId:{GettingUserProperties.GetUserId(HttpContext)} is trying to get messages from not existing chat room: {nameof(chatRoomId)}={chatRoomId}."; logger.LogInformation(messageToLog); return NoContent(); @@ -276,7 +249,7 @@ private async Task GetMessagesByRoomIdAsync(Guid chatRoomId, Offs if (isChatRoomValid) { - var messages = await messageService.GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync(chatRoomId, offsetFilter, this.GetUserRole()).ConfigureAwait(false); + var messages = await messageService.GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync(chatRoomId, offsetFilter, GettingUserProperties.GetUserRole(HttpContext)).ConfigureAwait(false); if (messages.Any()) { @@ -306,9 +279,9 @@ private async Task GetUsersRoomsAsync(Func GetByParentIdForAdmin(Guid parentId, [FromQuery [HttpGet] public async Task GetUsersChildren([FromQuery] OffsetFilter offsetFilter) { - string userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string userId = GettingUserProperties.GetUserId(User); return Ok(await service.GetByUserId(userId, offsetFilter).ConfigureAwait(false)); } @@ -99,7 +96,7 @@ public async Task GetUsersChildren([FromQuery] OffsetFilter offse [HttpGet("{id}")] public async Task GetUsersChildById(Guid id) { - string userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string userId = GettingUserProperties.GetUserId(User); return Ok(await service.GetByIdAndUserId(id, userId).ConfigureAwait(false)); } @@ -118,7 +115,7 @@ public async Task GetUsersChildById(Guid id) [HttpPost] public async Task Create(ChildDto childDto) { - string userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string userId = GettingUserProperties.GetUserId(User); var child = await service.CreateChildForUser(childDto, userId).ConfigureAwait(false); @@ -142,7 +139,7 @@ public async Task Create(ChildDto childDto) [HttpPut] public async Task Update(ChildDto dto) { - string userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string userId = GettingUserProperties.GetUserId(User); return Ok(await service.UpdateChildCheckingItsUserIdProperty(dto, userId).ConfigureAwait(false)); } @@ -161,7 +158,7 @@ public async Task Update(ChildDto dto) [HttpDelete("{id}")] public async Task Delete(Guid id) { - string userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string userId = GettingUserProperties.GetUserId(User); await service.DeleteChildCheckingItsUserIdProperty(id, userId).ConfigureAwait(false); diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/NotificationController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/NotificationController.cs index 4ac15c79ce..a9dfa70ebc 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/NotificationController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/NotificationController.cs @@ -4,9 +4,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using OutOfSchool.Common; -using OutOfSchool.Common.Extensions; using OutOfSchool.Services.Enums; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models.Notifications; using OutOfSchool.WebApi.Services; @@ -113,7 +112,7 @@ public async Task Read(Guid id) [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task ReadUsersNotificationsByType(NotificationType notificationType) { - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(User); await notificationService.ReadUsersNotificationsByType(userId, notificationType).ConfigureAwait(false); return Ok(); } @@ -152,7 +151,7 @@ public async Task GetById(Guid id) [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllUsersNotificationsGroupedAsync() { - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(User); var allNofitications = await notificationService.GetAllUsersNotificationsGroupedAsync(userId).ConfigureAwait(false); @@ -174,7 +173,7 @@ public async Task GetAllUsersNotificationsGroupedAsync() [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllUsersNotifications(NotificationType? notificationType) { - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(User); var allNofitications = await notificationService.GetAllUsersNotificationsByFilterAsync(userId, notificationType).ConfigureAwait(false); @@ -195,7 +194,7 @@ public async Task GetAllUsersNotifications(NotificationType? noti [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAmountOfNewUsersNotifications() { - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(User); var amount = await notificationService.GetAmountOfNewUsersNotificationsAsync(userId).ConfigureAwait(false); diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderAdminController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderAdminController.cs index 194db19e7d..399fa121c1 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderAdminController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderAdminController.cs @@ -4,16 +4,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; -using OutOfSchool.Common; -using OutOfSchool.Common.Extensions; using OutOfSchool.Common.Models; using OutOfSchool.Common.PermissionsModule; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models; using OutOfSchool.WebApi.Services; @@ -41,7 +39,7 @@ public ProviderAdminController( public override void OnActionExecuting(ActionExecutingContext context) { path = $"{context.HttpContext.Request.Path.Value}[{context.HttpContext.Request.Method}]"; - userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + userId = GettingUserProperties.GetUserId(User); } /// diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderController.cs index 5efe9bfe12..98b6ecd605 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ProviderController.cs @@ -13,7 +13,7 @@ using OutOfSchool.Common; using OutOfSchool.Common.Extensions; using OutOfSchool.Common.PermissionsModule; -using OutOfSchool.WebApi.Extensions; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models; using OutOfSchool.WebApi.Services; @@ -96,9 +96,9 @@ public async Task GetById(Guid providerId) public async Task GetProfile() { // TODO: localize messages from the conrollers. - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); - var isDeputyOrAdmin = !string.IsNullOrEmpty(User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole)) && - User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole) != "None"; + var userId = GettingUserProperties.GetUserId(User); + var isDeputyOrAdmin = !string.IsNullOrEmpty(GettingUserProperties.GetUserSubrole(User)) && + GettingUserProperties.GetUserSubrole(User) != "None"; if (userId == null) { BadRequest("Invalid user information."); @@ -144,7 +144,7 @@ public async Task Create(ProviderDto providerModel) } // TODO: find out if we need this field in the model - providerModel.UserId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + providerModel.UserId = GettingUserProperties.GetUserId(User); try { @@ -185,7 +185,7 @@ public async Task Update(ProviderDto providerModel) try { - var userId = User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(User); var provider = await providerService.Update(providerModel, userId).ConfigureAwait(false); if (provider == null) diff --git a/OutOfSchool/OutOfSchool.WebApi/Hubs/ChatWorkshopHub.cs b/OutOfSchool/OutOfSchool.WebApi/Hubs/ChatWorkshopHub.cs index dababe71d5..f30d32003c 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Hubs/ChatWorkshopHub.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Hubs/ChatWorkshopHub.cs @@ -10,10 +10,9 @@ using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Newtonsoft.Json; -using OutOfSchool.Common; -using OutOfSchool.Common.Extensions; using OutOfSchool.Services.Enums; using OutOfSchool.Services.Repository; +using OutOfSchool.WebApi.Common; using OutOfSchool.WebApi.Models.ChatWorkshop; using OutOfSchool.WebApi.Services; @@ -71,13 +70,13 @@ public ChatWorkshopHub( public override async Task OnConnectedAsync() { - var userId = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userId, nameof(userId)); - var userRoleName = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Role); + var userRoleName = GettingUserProperties.GetUserRole(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userRoleName, nameof(userRoleName)); - var userSubroleName = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole); + var userSubroleName = GettingUserProperties.GetUserSubrole(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userSubroleName, nameof(userSubroleName)); Role userRole = (Role)Enum.Parse(typeof(Role), userRoleName, true); @@ -119,7 +118,7 @@ public override async Task OnConnectedAsync() public override async Task OnDisconnectedAsync(Exception exception) { - var userId = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userId, nameof(userId)); logger.LogDebug($"UserId: {userId} connection:{Context.ConnectionId} disconnected."); @@ -145,7 +144,7 @@ public async Task SendMessageToOthersInGroupAsync(string chatNewMessage) if (!userHasRights) { - var messageToLog = $"{Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Role)} with UserId:{Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub)} is trying to send message with one of not his own parameters: {nameof(chatMessageWorkshopCreateDto.WorkshopId)} {chatMessageWorkshopCreateDto.WorkshopId}, {nameof(chatMessageWorkshopCreateDto.ParentId)} {chatMessageWorkshopCreateDto.ParentId}"; + var messageToLog = $"{GettingUserProperties.GetUserRole(Context.User)} with UserId:{GettingUserProperties.GetUserId(Context.User)} is trying to send message with one of not his own parameters: {nameof(chatMessageWorkshopCreateDto.WorkshopId)} {chatMessageWorkshopCreateDto.WorkshopId}, {nameof(chatMessageWorkshopCreateDto.ParentId)} {chatMessageWorkshopCreateDto.ParentId}"; logger.LogWarning(messageToLog); var messageForUser = localizer["Some of the message parameters were wrong. Please check your message and try again."]; @@ -241,17 +240,17 @@ private async Task AddConnectionsToGroupAsync(string userId, string chatRoomUniq private Task UserHasRigtsForChatRoomAsync(Guid workshopId, Guid parentId) { - var userId = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + var userId = GettingUserProperties.GetUserId(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userId, nameof(userId)); - var userRole = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Role); + var userRole = GettingUserProperties.GetUserRole(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userRole, nameof(userRole)); bool userRoleIsProvider = userRole.Equals(Role.Provider.ToString(), StringComparison.OrdinalIgnoreCase); if (userRoleIsProvider) { - var userSubroleName = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Subrole); + var userSubroleName = GettingUserProperties.GetUserSubrole(Context.User); LogErrorThrowExceptionIfPropertyIsNull(userSubroleName, nameof(userSubroleName)); Subrole userSubrole = (Subrole)Enum.Parse(typeof(Subrole), userSubroleName, true); diff --git a/OutOfSchool/OutOfSchool.WebApi/Hubs/NotificationHub.cs b/OutOfSchool/OutOfSchool.WebApi/Hubs/NotificationHub.cs index 516cce4419..f74ecf888b 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Hubs/NotificationHub.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Hubs/NotificationHub.cs @@ -1,8 +1,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; -using OutOfSchool.Common; -using OutOfSchool.Common.Extensions; +using OutOfSchool.WebApi.Common; namespace OutOfSchool.WebApi.Hubs { @@ -11,7 +10,7 @@ public class NotificationHub : Hub { public override async Task OnConnectedAsync() { - string name = Context.User.GetUserPropertyByClaimType(IdentityResourceClaimsTypes.Sub); + string name = GettingUserProperties.GetUserId(Context.User); await Groups.AddToGroupAsync(Context.ConnectionId, name).ConfigureAwait(false);