/// <summary> /// Authorize a user creating a chat message for a chat group using the Signalr ChatHub /// </summary> /// <param name="context"></param> /// <param name="requirement"></param> /// <param name="resource"></param> /// <returns></returns> protected override async Task <Task> HandleRequirementAsync(AuthorizationHandlerContext context, UserCanCreateChatMessageRequirement requirement, HubInvocationContext resource) { if (context.User == null) { return(Task.CompletedTask); } Claim userEmail = resource.Context.User.FindFirst(ClaimTypes.Email); User user = await _userManager.FindByEmailAsync(userEmail.Value.ToString()); if (user == null) { return(Task.CompletedTask); } if (resource.HubMethodArguments.Count == 0) { return(Task.CompletedTask); } CreateChatMessageDTO chatMessageDTO = resource.HubMethodArguments[0] as CreateChatMessageDTO; ChatGroupUser chatGroupUser = await _chatGroupUserService.GetByUserAndChatGroupAsync( user.Id, chatMessageDTO.ChatGroupUuid); if (user != null && chatGroupUser != null && chatGroupUser.UserId.Equals(user.Id)) { //User is chat group member context.Succeed(requirement); } return(Task.CompletedTask); }
public async Task <ActionResult <ChatGroupUserResponse> > RemoveUserFromChatGroupAsync(Guid chatGroupUuid) { string userEmail = User.FindFirstValue(ClaimTypes.Email); User user = await _userManager.FindByEmailAsync(userEmail); if (user == null) { return(Forbid()); } ChatGroupUser chatGroupUser = await _chatGroupUserService.GetByUserAndChatGroupAsync( user.Id, chatGroupUuid); // Check if user is authorized to remove user from the chat group AuthorizationResult isAuthorized = await _authorizationService.AuthorizeAsync( User, chatGroupUser, Operations.Create); if (!isAuthorized.Succeeded) { return(Forbid()); } return(await _chatGroupUserService.RemoveUserFromChatGroupAsync(user, chatGroupUuid)); }
public async Task <ActionResult <IEnumerable <ChatMessageDTO> > > ListMessagesForGroup(Guid chatGroupUuid) { string userEmail = User.FindFirstValue(ClaimTypes.Email); User user = await _userManager.FindByEmailAsync(userEmail); if (user == null) { return(Forbid()); } ChatGroupUser chatGroupUser = await _chatGroupUserService.GetByUserAndChatGroupAsync( user.Id, chatGroupUuid); // Check if user is authorized to read users for the chat group AuthorizationResult isAuthorized = await _authorizationService.AuthorizeAsync( User, chatGroupUser, Operations.Create); if (!isAuthorized.Succeeded) { return(Forbid()); } IEnumerable <ChatMessageDTO> messages = await _chatMessageService.ListMessagesForGroupAsync(chatGroupUuid); return(Ok(messages)); }
public IList <ChatGroup> GetAllGroup(int userId) { ChatGroupUser chatGroupUser = null; return(this._commonDao.OpenSession().QueryOver <ChatGroup>().Left.JoinAlias(c => c.ChatGroupUsers, () => chatGroupUser) .Where(c => chatGroupUser.User.Id == userId).OrderBy(m => m.Id).Desc.List()); }
public async Task <ChatGroupUserResponse> AddUserToChatGroupAsync(User user, Guid chatGroupUuid) { try { ChatGroup chatGroup = await _chatGroupRepository.GetByUuidAsync(chatGroupUuid); if (chatGroup == null) { return(new ChatGroupUserResponse { ErrorMessage = "Chat group does not exist" }); } // Check if user is already member of chat group ChatGroupUser chatGroupUser = await _chatGroupUserRepository.GetByUserAndChatGroupAsync( user.Id, chatGroup.ChatGroupId); if (chatGroupUser != null) { return(new ChatGroupUserResponse { ErrorMessage = $"User {user.Id} is already a member of chat group {chatGroup.Uuid}" }); } ChatGroupUser saveChatGroupUser = new ChatGroupUser { UserId = user.Id, User = user, ChatGroupId = chatGroup.ChatGroupId, ChatGroup = chatGroup, }; //Save the message await _chatGroupUserRepository.AddUserToChatGroupAsync(saveChatGroupUser); await _unitOfWork.CompleteAsync(); return(new ChatGroupUserResponse { Success = true, }); } catch (Exception ex) { // TODO logging return(new ChatGroupUserResponse { ErrorMessage = $"An error occurred when saving the ChatGroupUser resource: {ex.Message}" }); } }
public async Task <ChatGroupUserResponse> RemoveUserFromChatGroupAsync(User user, Guid chatGroupUuid) { try { ChatGroup chatGroup = await _chatGroupRepository.GetByUuidAsync(chatGroupUuid); if (chatGroup == null) { return(new ChatGroupUserResponse { ErrorMessage = "Chat group does not exist" }); } // Check if user is already member of chat group ChatGroupUser chatGroupUser = await _chatGroupUserRepository.GetByUserAndChatGroupAsync( user.Id, chatGroupUuid); if (chatGroupUser == null) { return(new ChatGroupUserResponse { ErrorMessage = $"User {user.Id} is not a member of chat group {chatGroupUuid}" }); } // Delete the ChatGroupUser entry _chatGroupUserRepository.RemoveUserFromChatGroup(chatGroupUser); await _unitOfWork.CompleteAsync(); return(new ChatGroupUserResponse { Success = true, ErrorMessage = string.Empty }); } catch (Exception ex) { // TODO logging return(new ChatGroupUserResponse { ErrorMessage = $"An error occurred when deleting the ChatGroupUser resource: {ex.Message}" }); } }
public async Task <ActionResult <ChatMessageResponse> > CreateChatMessage([FromBody] CreateChatMessageDTO chatMessageDTO) { string userEmail = User.FindFirstValue(ClaimTypes.Email); User user = await _userManager.FindByEmailAsync(userEmail); if (user == null) { return(NotFound(new ChatMessageResponse { Success = false, ErrorMessage = "User does not exist" })); } ChatGroupUser chatGroupUser = await _chatGroupUserService.GetByUserAndChatGroupAsync( user.Id, chatMessageDTO.ChatGroupUuid); // Check if user is authorized to create chat messages for the chat group AuthorizationResult isAuthorized = await _authorizationService.AuthorizeAsync( User, chatGroupUser, Operations.Create); if (!isAuthorized.Succeeded) { return(Forbid()); } CreateChatMessage chatMessage = new CreateChatMessage { User = user, Message = chatMessageDTO.Message, ChatGroupUuid = chatMessageDTO.ChatGroupUuid }; ChatMessageResponse createRes = await _chatMessageService.CreateChatMessageAsync(chatMessage); // Broadcast message to only clients who are members of the chat group await _chatHubContext.Clients.Group(chatMessageDTO.ChatGroupUuid.ToString()).SendAsync("ReceiveMessage", createRes); return(createRes); }
public void RemoveUserFromChatGroup(ChatGroupUser chatGroupUser) { _context.ChatGroupUsers.Remove(chatGroupUser); }
public async Task AddUserToChatGroupAsync(ChatGroupUser chatGroupUser) { await _context.ChatGroupUsers.AddAsync(chatGroupUser); }