'Associates a user with an OpenID log in'

@web.expose
def openid_associate(self, trans, cntrller='user', **kwd):

    if (not trans.app.config.enable_openid):
        return trans.show_error_message('OpenID    authentication    is    not    enabled    in    this    instance    of    Galaxy')
    use_panels = util.string_as_bool(kwd.get('use_panels', False))
    message = escape(kwd.get('message', ''))
    status = kwd.get('status', 'done')
    email = kwd.get('email', '')
    username = kwd.get('username', '')
    redirect = kwd.get('redirect', '').strip()
    params = util.Params(kwd)
    is_admin = ((cntrller == 'admin') and trans.user_is_admin())
    openids = trans.galaxy_session.openids
    user = None
    if (not openids):
        return trans.show_error_message(('You    have    not    successfully    completed    an    OpenID    authentication    in    this    session.        You    can    do    so    on    the    <a    href="%s">login</a>    page.' % url_for(controller='user', action='login', use_panels=use_panels)))
    elif is_admin:
        return trans.show_error_message('Associating    OpenIDs    with    accounts    cannot    be    done    by    administrators.')
    if kwd.get('login_button', False):
        (message, status, user, success) = self.__validate_login(trans, **kwd)
        if success:
            openid_objs = []
            for openid in openids:
                openid_provider_obj = trans.app.openid_providers.get(openid.provider)
                if ((not openid_provider_obj) or (not openid_provider_obj.never_associate_with_user)):
                    openid.user = user
                    trans.sa_session.add(openid)
                    trans.log_event(('User    associated    OpenID:    %s' % openid.openid))
                if (openid_provider_obj and openid_provider_obj.has_post_authentication_actions()):
                    openid_objs.append(openid_provider_obj)
            trans.sa_session.flush()
            if (len(openid_objs) == 1):
                return trans.response.send_redirect(url_for(controller='user', action='openid_auth', openid_provider=openid_objs[0].id, redirect=redirect, auto_associate=True))
            elif openid_objs:
                message = 'You    have    authenticated    with    several    OpenID    providers,    please    click    the    following    links    to    execute    the    post    authentication    actions.    '
                message = ('%s<br/><ul>' % message)
                for openid in openid_objs:
                    message = ('%s<li><a    href="%s"    target="_blank">%s</a></li>' % (message, url_for(controller='user', action='openid_auth', openid_provider=openid.id, redirect=redirect, auto_associate=True), openid.name))
                message = ('%s</ul>' % message)
                return trans.response.send_redirect(url_for(controller='user', action='openid_manage', use_panels=use_panels, redirect=redirect, message=message, status='info'))
            if redirect:
                return trans.response.send_redirect(redirect)
            return trans.response.send_redirect(url_for(controller='user', action='openid_manage', use_panels=use_panels, redirect=redirect, message=message, status='info'))
    if kwd.get('create_user_button', False):
        password = kwd.get('password', '')
        confirm = kwd.get('confirm', '')
        subscribe = params.get('subscribe', '')
        subscribe_checked = CheckboxField.is_checked(subscribe)
        error = ''
        if ((not trans.app.config.allow_user_creation) and (not trans.user_is_admin())):
            error = 'User    registration    is    disabled.        Please    contact    your    local    Galaxy    administrator    for    an    account.'
        else:
            error = self.__validate(trans, params, email, password, confirm, username)
            if (not error):
                (message, status, user, success) = self.__register(trans, cntrller, subscribe_checked, **kwd)
                if success:
                    openid_objs = []
                    for openid in openids:
                        openid_provider_obj = trans.app.openid_providers.get(openid.provider)
                        if (not openid_provider_obj):
                            openid_provider_obj = trans.app.openid_providers.new_provider_from_identifier(openid.identifier)
                        if (not openid_provider_obj.never_associate_with_user):
                            openid.user = user
                            trans.sa_session.add(openid)
                            trans.log_event(('User    associated    OpenID:    %s' % openid.openid))
                        if openid_provider_obj.has_post_authentication_actions():
                            openid_objs.append(openid_provider_obj)
                    trans.sa_session.flush()
                    if (len(openid_objs) == 1):
                        return trans.response.send_redirect(url_for(controller='user', action='openid_auth', openid_provider=openid_objs[0].id, redirect=redirect, auto_associate=True))
                    elif openid_objs:
                        message = 'You    have    authenticated    with    several    OpenID    providers,    please    click    the    following    links    to    execute    the    post    authentication    actions.    '
                        message = ('%s<br/><ul>' % message)
                        for openid in openid_objs:
                            message = ('%s<li><a    href="%s"    target="_blank">%s</a></li>' % (message, url_for(controller='user', action='openid_auth', openid_provider=openid.id, redirect=redirect, auto_associate=True), openid.name))
                        message = ('%s</ul>' % message)
                        return trans.response.send_redirect(url_for(controller='user', action='openid_manage', use_panels=True, redirect=redirect, message=message, status='info'))
                    if redirect:
                        return trans.response.send_redirect(redirect)
                    return trans.response.send_redirect(url_for(controller='user', action='openid_manage', use_panels=use_panels, redirect=redirect, message=message, status='info'))
            else:
                message = error
                status = 'error'
    return trans.fill_template('/user/openid_associate.mako', cntrller=cntrller, email=email, password='', confirm='', username=transform_publicname(trans, username), header='', use_panels=use_panels, redirect=redirect, refresh_frames=[], message=message, status=status, active_view='user', subscribe_checked=False, openids=openids)
