def do_login(request): # FIXME Log stuff? errors = [] form = LoginForm(request.POST) origin = request.POST.get('origin', '').strip() if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] try: account = auth.authenticate(username, password) except ldapauth.Error, e: errors.append('Error while talking to LDAP:\n%s' % e) else: if account: try: request.session[ACCOUNT_ID_VAR] = account.id request.account = account except ldapauth.Error, e: errors.append('Error while talking to LDAP:\n%s' % e) else: _logger.info("%s successfully logged in", account.login) if not origin: origin = reverse('webfront-index') return HttpResponseRedirect(origin) else:
def login(request): """Controller for the login page""" if request.method == 'POST': return do_login(request) origin = request.GET.get('origin', '').strip() if 'noaccess' in request.GET: if request.account.is_default_account(): errors = ['You need to log in to access this resource'] else: errors = [ 'You have insufficient privileges to access this ' 'resource. Please log in as another user.' ] else: errors = [] return render( request, 'webfront/login.html', { 'form': LoginForm(initial={'origin': origin}), 'origin': origin, 'errors': errors, }, )
def do_login(request): """Do a login based on post parameters""" errors = [] form = LoginForm(request.POST) origin = request.POST.get('origin', '').strip() if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] try: account = auth.authenticate(username, password) except ldapauth.Error as error: errors.append('Error while talking to LDAP:\n%s' % error) else: if account: LogEntry.add_log_entry( account, 'log-in', '{actor} logged in', before=account ) try: request.session[ACCOUNT_ID_VAR] = account.id request.account = account except ldapauth.Error as error: errors.append('Error while talking to LDAP:\n%s' % error) else: _logger.info("%s successfully logged in", account.login) if not origin: origin = reverse('webfront-index') return HttpResponseRedirect(origin) else: _logger.info("failed login: %r", username) errors.append( 'Username or password is incorrect, or the ' 'account is locked.' ) # Something went wrong. Display login page with errors. return render( request, 'webfront/login.html', { 'form': form, 'errors': errors, 'origin': origin, }, )