Exemple #1
0
        public ChangeModPermissionsForSubResponse Handle(ChangeModPermissionsForSub command)
        {
            var response = new ChangeModPermissionsForSubResponse();

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

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

                // you can't change your own permissions
                if (requestingUser.Id == userToChange.Id)
                {
                    response.Error = "You can't change your own permissions.";
                    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);
                }

                if (requestingUser.IsAdmin)
                {
                    // the user is an admin, or the user has has "full" access and the user we are changing has is a newby.
                    _moderationService.UpdateUserModPermissionForSub(userToChange.Id, sub.Id, command.Permissions);
                }
                else
                {
                    var requestingUserModInfo = _moderationService.GetModeratorInfoForUserInSub(requestingUser.Id, sub.Id);
                    if (requestingUserModInfo == null)
                    {
                        response.Error = "You are not a mod of the sub.";
                        return(response);
                    }

                    var userToChangeModInfo = _moderationService.GetModeratorInfoForUserInSub(userToChange.Id, sub.Id);
                    if (userToChangeModInfo == null)
                    {
                        response.Error = "The user that you are attempting change permissions for is not a mod.";
                        return(response);
                    }

                    if (requestingUser.IsAdmin ||
                        (requestingUserModInfo.AddedOn <= userToChangeModInfo.AddedOn &&
                         requestingUserModInfo.Permissions.HasFlag(ModeratorPermissions.All)))
                    {
                        // the user is an admin, or the user has has "full" access and the user we are changing has is a newby.
                        _moderationService.UpdateUserModPermissionForSub(userToChange.Id, sub.Id, command.Permissions);
                    }
                    else
                    {
                        response.Error = "You are not authorized to change this mod's permissions.";
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Error = "An unknown error occured.";
                _logger.Error("An error occured changing a mods permissions.", ex);
            }

            return(response);
        }