コード例 #1
0
def accumulate_from_backends(request, method_name):
    list = []
    for backend in get_backends():
        method = getattr(backend, method_name, None)
        if callable(method):
            list += method(request)
    return list
コード例 #2
0
ファイル: views.py プロジェクト: incuna/authentic
def profile(request):

    frontends = get_backends('AUTH_FRONTENDS')

    if request.method == "POST":
        for frontend in frontends:
            if not frontend.enabled():
                continue
            if 'submit-%s' % frontend.id() in request.POST:
                form = frontend.form()(data=request.POST)
                if form.is_valid():
                    if request.session.test_cookie_worked():
                        request.session.delete_test_cookie()
                    return frontend.post(request, form, None, '/profile')
    # User attributes management
    try:
        user_profile = request.user.get_profile()
        profile = []
        for field_name in user_profile._meta.get_all_field_names():
            if field_name in ('id', 'user'):
                continue
            field = user_profile._meta.get_field_by_name(field_name)[0]
            value = getattr(user_profile, field_name)
            if value:
                profile.append((field.verbose_name, value))
    except (SiteProfileNotAvailable, ObjectDoesNotExist):
        profile = ()
    # Credentials management
    blocks = [ frontend.profile(request, next='/profile') for frontend in frontends \
            if hasattr(frontend, 'profile') ]
    return render_to_response('idp/account_management.html', { 'frontends_block': blocks, 'profile': profile },
            RequestContext(request))
コード例 #3
0
def profile(request):

    frontends = get_backends('AUTH_FRONTENDS')

    if request.method == "POST":
        for frontend in frontends:
            if not frontend.enabled():
                continue
            if 'submit-%s' % frontend.id() in request.POST:
                form = frontend.form()(data=request.POST)
                if form.is_valid():
                    if request.session.test_cookie_worked():
                        request.session.delete_test_cookie()
                    return frontend.post(request, form, None, '/profile')
    # User attributes management
    try:
        user_profile = request.user.get_profile()
        profile = []
        for field_name in user_profile._meta.get_all_field_names():
            if field_name in ('id', 'user'):
                continue
            field = user_profile._meta.get_field_by_name(field_name)[0]
            value = getattr(user_profile, field_name)
            if value:
                profile.append((field.verbose_name, value))
    except (SiteProfileNotAvailable, ObjectDoesNotExist):
        profile = ()
    # Credentials management
    blocks = [ frontend.profile(request, next='/profile') for frontend in frontends \
            if hasattr(frontend, 'profile') ]
    return render_to_response('idp/account_management.html', {
        'frontends_block': blocks,
        'profile': profile
    }, RequestContext(request))
コード例 #4
0
def accumulate_from_backends(request, method_name):
    list = []
    for backend in get_backends():
        method = getattr(backend, method_name, None)
        if callable(method):
            list += method(request)
    return list
コード例 #5
0
def profile(request):

    frontends = get_backends('AUTH_FRONTENDS')

    if request.method == "POST":
        for frontend in frontends:
            if not frontend.enabled():
                continue
            if 'submit-%s' % frontend.id() in request.POST:
                form = frontend.form()(data=request.POST)
                if form.is_valid():
                    if request.session.test_cookie_worked():
                        request.session.delete_test_cookie()
                    return frontend.post(request, form, None, '/profile')
    # User attributes management
    profile = []
    try:
        for field_name in getattr(request.user, 'USER_PROFILE', []):
            if isinstance(field_name, tuple):
                field_name, title = field_name
            elif isinstance(field_name, str):
                title = request.user._meta.get_field(field_name).verbose_name
            else:
                raise TypeError('USER_PROFILE must contain string or tuple')
            value = getattr(request.user, field_name, None)
            if not value:
                continue
            if callable(value):
                value = value()
            if not isinstance(value, basestring) and hasattr(value, '__iter__'):
                profile.append((title, map(unicode, value)))
            else:
                profile.append((title, [unicode(value)]))
    except (SiteProfileNotAvailable, ObjectDoesNotExist):
        pass
    # Credentials management
    blocks = [ frontend.profile(request, next='/profile') for frontend in frontends \
            if hasattr(frontend, 'profile') ]
    return render_to_response('idp/account_management.html', { 
        'frontends_block': blocks, 
        'profile': profile,
        'allow_account_deletion': app_settings.A2_REGISTRATION_CAN_DELETE_ACCOUNT,
        },
        RequestContext(request))
コード例 #6
0
ファイル: views.py プロジェクト: incuna/authentic
def login(request, template_name='auth/login.html',
          login_form_template='auth/login_form.html',
          redirect_field_name=REDIRECT_FIELD_NAME,
          authentication_form=WithNonceAuthenticationForm):
    """Displays the login form and handles the login action."""

    redirect_to = request.REQUEST.get(redirect_field_name)
    if not redirect_to or ' ' in redirect_to:
        redirect_to = settings.LOGIN_REDIRECT_URL
    # Heavier security check -- redirects to http://example.com should
    # not be allowed, but things like /view/?param=http://example.com
    # should be allowed. This regex checks if there is a '//' *before* a
    # question mark.
    elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to):
            redirect_to = settings.LOGIN_REDIRECT_URL
    nonce = request.REQUEST.get(NONCE_FIELD_NAME)

    frontends = get_backends('AUTH_FRONTENDS')

    # If already logged, leave now
    if not request.user.is_anonymous() \
            and nonce is None \
            and request.method != 'POST':
        return HttpResponseRedirect(redirect_to)

    if request.method == "POST":
        if 'cancel' in request.POST:
            redirect_to = add_arg(redirect_to, 'cancel')
            return HttpResponseRedirect(redirect_to)
        else:
            forms = []
            for frontend in frontends:
                if not frontend.enabled():
                    continue
                if 'submit-%s' % frontend.id() in request.POST:
                    form = frontend.form()(data=request.POST)
                    if form.is_valid():
                        if request.session.test_cookie_worked():
                            request.session.delete_test_cookie()
                        return frontend.post(request, form, nonce, redirect_to)
                    forms.append((frontend.name(), {'form': form, 'backend': frontend}))
                else:
                    forms.append((frontend.name(), {'form': frontend.form()(), 'backend': frontend}))
    else:
        forms = [(frontend.name(), { 'form': frontend.form()(), 'backend': frontend }) \
                for frontend in frontends if frontend.enabled()]

    rendered_forms = []
    for name, d in forms:
        context = { 'cancel': nonce is not None,
                    'submit_name': 'submit-%s' % d['backend'].id(),
                    redirect_field_name: redirect_to,
                    'form': d['form'] }
        if hasattr(d['backend'], 'get_context'):
            context.update(d['backend'].get_context())
        rendered_forms.append((name,
            render_to_string(d['backend'].template(),
                RequestContext(request, context))))

    request.session.set_test_cookie()

    if Site._meta.installed:
        current_site = Site.objects.get_current()
    else:
        current_site = RequestSite(request)

    return render_to_response(template_name, {
        'methods': rendered_forms,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }, context_instance=RequestContext(request))
コード例 #7
0
ファイル: views.py プロジェクト: Cloudxtreme/authentic
def login(request,
          template_name='auth/login.html',
          login_form_template='auth/login_form.html',
          redirect_field_name=REDIRECT_FIELD_NAME,
          authentication_form=WithNonceAuthenticationForm):
    """Displays the login form and handles the login action."""

    redirect_to = request.REQUEST.get(redirect_field_name)
    if not redirect_to or ' ' in redirect_to:
        redirect_to = settings.LOGIN_REDIRECT_URL
    # Heavier security check -- redirects to http://example.com should
    # not be allowed, but things like /view/?param=http://example.com
    # should be allowed. This regex checks if there is a '//' *before* a
    # question mark.
    elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to):
        redirect_to = settings.LOGIN_REDIRECT_URL
    nonce = request.REQUEST.get(NONCE_FIELD_NAME)

    frontends = get_backends('AUTH_FRONTENDS')

    # If already logged, leave now
    if not request.user.is_anonymous() \
            and nonce is None \
            and request.method != 'POST':
        return HttpResponseRedirect(redirect_to)

    if request.method == "POST":
        if 'cancel' in request.POST:
            redirect_to = add_arg(redirect_to, 'cancel')
            return HttpResponseRedirect(redirect_to)
        else:
            forms = []
            for frontend in frontends:
                if not frontend.enabled():
                    continue
                if 'submit-%s' % frontend.id() in request.POST:
                    form = frontend.form()(data=request.POST)
                    if form.is_valid():
                        if request.session.test_cookie_worked():
                            request.session.delete_test_cookie()
                        return frontend.post(request, form, nonce, redirect_to)
                    forms.append((frontend.name(), {
                        'form': form,
                        'backend': frontend
                    }))
                else:
                    forms.append((frontend.name(), {
                        'form': frontend.form()(),
                        'backend': frontend
                    }))
    else:
        forms = [(frontend.name(), { 'form': frontend.form()(), 'backend': frontend }) \
                for frontend in frontends if frontend.enabled()]

    rendered_forms = []
    for name, d in forms:
        context = {
            'cancel': nonce is not None,
            'submit_name': 'submit-%s' % d['backend'].id(),
            redirect_field_name: redirect_to,
            'form': d['form']
        }
        if hasattr(d['backend'], 'get_context'):
            context.update(d['backend'].get_context())
        rendered_forms.append(
            (name,
             render_to_string(d['backend'].template(),
                              RequestContext(request, context))))

    request.session.set_test_cookie()

    if Site._meta.installed:
        current_site = Site.objects.get_current()
    else:
        current_site = RequestSite(request)

    return render_to_response(template_name, {
        'methods': rendered_forms,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    },
                              context_instance=RequestContext(request))