MobileBlur at [8cd4f18daf]

File gluon/contrib/login_methods/ artifact 6b873fea64 part of check-in 8cd4f18daf

#!/usr/bin/env python
# coding: utf8

ExtendedLoginForm is used to extend normal login form in web2py with one more login method.
So user can choose the built-in login or extended login methods.

from gluon.html import DIV

class ExtendedLoginForm(object):
    Put extended_login_form under web2py/gluon/contrib/login_methods folder.
    Then inside your model where defines the auth:

    auth = Auth(globals(),db)              # authentication/authorization
    auth.define_tables()                   # You might like to put the code after auth.define_tables
    ...                                    # if the alt_login_form deals with tables of auth.

    alt_login_form = RPXAccount(request,
                                url = "http://localhost:8000/%s/default/user/login" % request.application)
    extended_login_form = ExtendedLoginForm(auth, alt_login_form, signals=['token'])

    auth.settings.login_form = extended_login_form

        Since rpx_account doesn't create the password for the user, you
        might need to provide a way for user to create password to do
        normal login.


    def __init__(self,
                 login_arg = 'login'
        self.auth = auth
        self.alt_login_form = alt_login_form
        self.signals = signals
        self.login_arg = login_arg

    def get_user(self):
        Delegate the get_user to alt_login_form.get_user.
        if hasattr(self.alt_login_form, 'get_user'):
            return self.alt_login_form.get_user()
        return None # let do the rest

    def login_url(self, next):
        Optional implement for alt_login_form.

        In normal case, this should be replaced by get_user, and never get called.
        if hasattr(self.alt_login_form, 'login_url'):
            return self.alt_login_form.login_url(next)
        return self.auth.settings.login_url

    def logout_url(self, next):
        Optional implement for alt_login_form.

        Called if bool(alt_login_form.get_user) is True.

        If alt_login_form implemented logout_url function, it will return that function call.
        if hasattr(self.alt_login_form, 'logout_url'):
            return self.alt_login_form.logout_url(next)
        return next

    def login_form(self):
        Combine the auth() form with alt_login_form.

        If signals are set and a parameter in request matches any signals,
        it will return the call of alt_login_form.login_form instead.
        So alt_login_form can handle some particular situations, for example,
        multiple steps of OpenID login inside alt_login_form.login_form.

        Otherwise it will render the normal login form combined with
        request = self.auth.environment.request
        args = request.args

        if (self.signals and
            any([True for signal in self.signals if request.vars.has_key(signal)])
            return self.alt_login_form.login_form()

        self.auth.settings.login_form = self.auth
        form = DIV(self.auth())
        self.auth.settings.login_form = self

        return form