def _modify_relationship(relationship, unlink=False, is_sub=False):
    """Return a function for relationship modification.

    Used to support friending (user-to-user), as well as moderating,
    contributor creating, and banning (user-to-subreddit).

    """
    # The API uses friend and unfriend to manage all of these relationships.
    url_key = 'unfriend' if unlink else 'friend'

    if relationship == 'friend':
        access = {'scope': None, 'login': True}
    elif relationship == 'moderator':
        access = {'scope': 'modothers'}
    elif relationship in ['banned', 'contributor', 'muted']:
        access = {'scope': 'modcontributors'}
    elif relationship in ['wikibanned', 'wikicontributor']:
        access = {'scope': ['modcontributors', 'modwiki']}
    else:
        access = {'scope': None, 'mod': True}

    @restrict_access(**access)
    def do_relationship(thing, user, **kwargs):
        data = {'name': six.text_type(user),
                'type': relationship}
        data.update(kwargs)
        if is_sub:
            data['r'] = six.text_type(thing)
        else:
            data['container'] = thing.fullname

        session = thing.reddit_session
        if relationship == 'moderator':
            session.evict(session.config['moderators'].format(
                subreddit=six.text_type(thing)))
        url = session.config[url_key]
        return session.request_json(url, data=data)
    return do_relationship