public async Task ChangeUserRole(int chatId, string userId, string caller, ChatRole newRole)
        {
            var callerRole = await rolesRepository.GetByIdAsync(chatId, caller);

            if (callerRole == null)
            {
                throw new UnauthorizedAccessException("Only creator can change user's roles.");
            }

            if (callerRole.RoleId != ChatRole.Creator)
            {
                throw new UnauthorizedAccessException("Only creator can change user's roles.");
            }

            if (userId == caller)
            {
                throw new UnauthorizedAccessException("Can't change role of creator.");
            }

            var targetUserRole = await rolesRepository.GetByIdAsync(chatId, userId);

            var staticRole = staticRolesRepo.Get(newRole);

            targetUserRole.Role = staticRole;

            await rolesRepository.UpdateAsync(targetUserRole);

            await unitOfWork.Commit();
        }
 public static ChatRoleDataModel Create(int chatId, string userId, ChatRole role)
 {
     return(new ChatRoleDataModel
     {
         ChatId = chatId,
         UserId = userId,
         RoleId = role
     });
 }
Exemple #3
0
        public async Task <bool> ChangeUserRole(string userId, int chatId, ChatRole newRole)
        {
            var whoSentId = userProvider.GetUserId(Context);
            await userService.MakeUserOnline(whoSentId, Context.ConnectionId);

            try
            {
                await chatsService.ChangeUserRole(chatId, userId, whoSentId, newRole);
                await SendUserRoleChanged(chatId, userId, newRole);

                return(true);
            }
            catch (Exception ex)
            {
                await SendError(Context.ConnectionId, ex.Message);

                logger.LogError(ex.Message);
                return(false);
            }
        }
 protected Task SendUserRoleChanged(int chatId, string userId, ChatRole newRole)
 {
     return(Clients.Group(chatId.ToString()).SendAsync("UserRoleChanged", userId, chatId, newRole));
 }
 public static ChatRoleDataModel Create(ConversationDataModel chat, string userId, ChatRole role)
 {
     return(new ChatRoleDataModel
     {
         Chat = chat,
         UserId = userId,
         RoleId = role
     });
 }
 public ChatUser(AuthUser user, Chat chat, ChatRole role)
 {
     User = user ?? throw new ArgumentNullException(nameof(user));
     Chat = chat ?? throw new ArgumentNullException(nameof(chat));
     Role = role;
 }
 public void MakeUserAdmin() => Role = ChatRole.Admin;
Exemple #8
0
 public UserRole(string?username, ChatRole role)
 {
     Username     = username;
     SelectUserBy = SelectUserBy.Username;
     Role         = role;
 }
Exemple #9
0
 public UserRole(long userId, ChatRole role)
 {
     UserId       = userId;
     SelectUserBy = SelectUserBy.UserId;
     Role         = role;
 }
Exemple #10
0
 public RoleDataModel Get(ChatRole role)
 {
     return(dbContext.Roles.Find(role));
 }
Exemple #11
0
 public Chatter(string username, ChatRole chatRole)
 {
     Username = username;
     ChatRole = chatRole;
 }