Beispiel #1
0
        public RemoveModFromSubResponse Handle(RemoveModFromSub command)
        {
            var response = new RemoveModFromSubResponse();

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

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

                Sub sub = null;

                if (command.SubId.HasValue)
                {
                    sub = _subService.GetSubById(command.SubId.Value);
                }
                else if (!string.IsNullOrEmpty(command.SubName))
                {
                    sub = _subService.GetSubByName(command.SubName);
                }

                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                var userToRemoveModInfo = _moderationService.GetModeratorInfoForUserInSub(userToRemove.Id, sub.Id);
                if (userToRemoveModInfo == null)
                {
                    response.Error = "The user is not a mod of this sub.";
                    return(response);
                }

                if (requestingUser.IsAdmin)
                {
                    _moderationService.RemoveModFromSub(userToRemove.Id, sub.Id);
                }
                else
                {
                    var requestingUserModInfo = _moderationService.GetModeratorInfoForUserInSub(requestingUser.Id, sub.Id);
                    if (requestingUserModInfo == null)
                    {
                        response.Error = "You are not a mod of this sub.";
                        return(response);
                    }

                    if (
                        // if the user is removing himself (doable)
                        requestingUser.Id == userToRemove.Id ||
                        // or the requesting user is an admin
                        requestingUser.IsAdmin ||
                        // or the user has has "full" access and the user we are changing has is a newby.
                        (requestingUserModInfo.AddedOn <=
                         userToRemoveModInfo.AddedOn &&
                         requestingUserModInfo.Permissions.HasFlag(
                             ModeratorPermissions.All))
                        )
                    {
                        _moderationService.RemoveModFromSub(userToRemove.Id, sub.Id);
                    }
                    else
                    {
                        response.Error = "You are not permitted to remove the mod from this sub.";
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Error = "An unknown error occured.";
                _logger.Error("An error occured removing a user from sub.", ex);
            }

            return(response);
        }