Exemplo n.º 1
0
 public ModeratorPermissions?GetUserPermissionsForSub(User user, Guid subId)
 {
     return(_moderationService.GetUserPermissionsForSub(user, subId));
 }
Exemplo n.º 2
0
        public InviteModToSubResponse Handle(InviteModToSub command)
        {
            var response = new InviteModToSubResponse();

            try
            {
                var requestingUser = _membershipService.GetUserById(command.RequestingUserId);
                if (requestingUser == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var sub = command.SubId.HasValue
                    ? _subService.GetSubById(command.SubId.Value)
                    : _subService.GetSubByName(command.SubName);
                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                var permission = _permissionService.GetUserPermissionsForSub(requestingUser, sub.Id);

                if (!permission.HasValue || !permission.Value.HasFlag(ModeratorPermissions.All))
                {
                    response.Error = "You are not permitted to invite users to mod a sub.";
                    return(response);
                }

                var user = command.UserId.HasValue
                   ? _membershipService.GetUserById(command.UserId.Value)
                   : _membershipService.GetUserByUserName(command.UserName);
                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var existingModPermissions = _moderationService.GetUserPermissionsForSub(user, sub.Id);
                if (existingModPermissions.HasValue)
                {
                    response.Error = "The user is already a moderator.";
                    return(response);
                }

                var userInviteInfo = _moderationInviteService.GetModeratorInviteInfo(user.Id, sub.Id);
                if (userInviteInfo != null)
                {
                    response.Error = "The user is already invited.";
                    return(response);
                }

                _moderationInviteService.AddInvite(user.Id, sub.Id, requestingUser.Id, command.Permissions);
            }
            catch (Exception ex)
            {
                response.Error = "An unknown error occured.";
                _logger.Error("An error occured inviting a mod to sub.", ex);
            }

            return(response);
        }