Refactor.

see #891
This commit is contained in:
Antoine Nguyen
2016-09-08 21:31:23 +02:00
parent 8e45f2c7cb
commit c8a2f739ed
3 changed files with 24 additions and 43 deletions
+23 -11
View File
@@ -9,12 +9,33 @@ from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
from modoboa.core.forms import LoginForm
from modoboa.lib import events
from modoboa.lib import events, parameters
from modoboa.lib.web_utils import _render_to_string
from ..extensions import exts_pool
logger = logging.getLogger("modoboa.auth")
def find_nextlocation(request, user):
"""Find next location for given user after login."""
if not user.last_login:
# Redirect to profile on first login
return reverse("core:user_index")
nextlocation = request.POST.get("next", None)
if nextlocation is None or nextlocation == "None":
if request.user.role == "SimpleUsers":
topredir = parameters.get_admin("DEFAULT_TOP_REDIRECTION")
if topredir != "user":
infos = exts_pool.get_extension_infos(topredir)
nextlocation = infos["url"] if infos["url"] else infos["name"]
else:
nextlocation = reverse("core:user_index")
else:
nextlocation = reverse("core:dashboard")
return nextlocation
def dologin(request):
"""Try to authenticate."""
error = None
@@ -25,10 +46,6 @@ def dologin(request):
user = authenticate(username=form.cleaned_data["username"],
password=form.cleaned_data["password"])
if user and user.is_active:
nextlocation = None
if not user.last_login:
# Redirect to profile on first login
nextlocation = reverse("core:user_index")
login(request, user)
if not form.cleaned_data["rememberme"]:
request.session.set_expiry(0)
@@ -43,12 +60,7 @@ def dologin(request):
events.raiseEvent("UserLogin", request,
form.cleaned_data["username"],
form.cleaned_data["password"])
if nextlocation is None:
nextlocation = request.POST.get("next", None)
if nextlocation is None or nextlocation == "None":
nextlocation = reverse("topredirection")
return HttpResponseRedirect(nextlocation)
return HttpResponseRedirect(find_nextlocation(request, user))
error = _(
"Your username and password didn't match. Please try again.")
logger.warning(
+1 -31
View File
@@ -10,9 +10,7 @@ import sys
from django import template
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.template.loader import render_to_string
@@ -138,34 +136,6 @@ def size2integer(value):
return 0
@login_required
def topredirection(request):
"""Simple view to redirect the request when no application is specified.
The default "top redirection" can be specified in the *Admin >
Settings* panel. It is the application that will be
launched. Those not allowed to access the application will be
redirected to their preferences page.
This feature only applies to simple users.
:param request: a Request object
"""
from modoboa.lib import parameters
from modoboa.core.extensions import exts_pool
if request.user.role == "SimpleUsers":
topredir = parameters.get_admin("DEFAULT_TOP_REDIRECTION", app="core")
if topredir != "user":
infos = exts_pool.get_extension_infos(topredir)
path = infos["url"] if infos["url"] else infos["name"]
else:
path = reverse("core:user_index")
else:
path = reverse("core:dashboard")
return HttpResponseRedirect(path)
class NavigationParameters(object):
"""
Just a simple object to manipulate navigation parameters.
-1
View File
@@ -7,7 +7,6 @@ from modoboa.lib import events
urlpatterns = patterns(
'',
url(r'^$', 'modoboa.lib.web_utils.topredirection', name="topredirection"),
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog',
{'packages': ('modoboa', ), }),
('', include('modoboa.core.urls', namespace="core")),