'Handle workflow sharing.'

@web.expose
@web.require_login('Share    or    export    Galaxy    workflows')
def sharing(self, trans, id, **kwargs):

    session = trans.sa_session
    if ('unshare_me' in kwargs):
        stored = self.get_stored_workflow(trans, id, False, True)
        association = session.query(model.StoredWorkflowUserShareAssociation).filter_by(user=trans.user, stored_workflow=stored).one()
        session.delete(association)
        session.flush()
        return self.list(trans)
    else:
        stored = self.get_stored_workflow(trans, id)
        session.add(stored)
        if ('make_accessible_via_link' in kwargs):
            self._make_item_accessible(trans.sa_session, stored)
        elif ('make_accessible_and_publish' in kwargs):
            self._make_item_accessible(trans.sa_session, stored)
            stored.published = True
        elif ('publish' in kwargs):
            stored.published = True
        elif ('disable_link_access' in kwargs):
            stored.importable = False
        elif ('unpublish' in kwargs):
            stored.published = False
        elif ('disable_link_access_and_unpublish' in kwargs):
            stored.importable = stored.published = False
        elif ('unshare_user' in kwargs):
            user = session.query(model.User).get(trans.security.decode_id(kwargs['unshare_user']))
            if (not user):
                error('User    not    found    for    provided    id')
            association = session.query(model.StoredWorkflowUserShareAssociation).filter_by(user=user, stored_workflow=stored).one()
            session.delete(association)
        if (stored.importable and (not stored.slug)):
            self._make_item_accessible(trans.sa_session, stored)
        session.flush()
        return trans.fill_template('/workflow/sharing.mako', use_panels=True, item=stored)
