diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py index db0e0b60..d3af1cae 100644 --- a/fet2020/authentications/decorators.py +++ b/fet2020/authentications/decorators.py @@ -9,3 +9,13 @@ def unauthenticated_user(view_func): return view_func(request, *args, **kwargs) return wrapper_func + + +def authenticated_user(view_func): + def wrapper_func(request, *args, **kwargs): + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) + else: + return redirect('home') + + return wrapper_func diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py index e9907280..a861ee8c 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -4,16 +4,11 @@ from django.contrib import messages from django.contrib.auth.models import User from .authentications import authentication -# from .decorators import unauthenticated_user +from .decorators import unauthenticated_user, authenticated_user -# @unauthenticated_user +@unauthenticated_user def loginPage(request): - """ - if request.user.is_authenticated: - return redirect('home') - else: - """ if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') @@ -29,12 +24,13 @@ def loginPage(request): login(request, user) return redirect('home') else: - messages.info(request, 'username OR password is incorrect') + messages.info(request, 'username or password is incorrect') context = {} return render(request, 'authentications/login.html', context) +@authenticated_user def logoutUser(request): logout(request) return redirect('home') diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index 4fb52c2f..cc773ddf 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -115,7 +115,7 @@ DATABASES = { } AUTHENTICATION_BACKENDS = [ - 'django.contrib.auth.backends.RemoteUserBackend', + # 'django.contrib.auth.backends.RemoteUserBackend', 'django.contrib.auth.backends.ModelBackend', ] diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index 41a166a2..6a515d5f 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -21,6 +21,7 @@ from . import views from posts.views import PostViewSet from members.views import MemberViewSet from rest_framework import routers +from authentications.decorators import authenticated_user router = routers.DefaultRouter() router.register(r'posts', PostViewSet) @@ -29,7 +30,7 @@ router.register(r'members', MemberViewSet) urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), - path('admin/', admin.site.urls), + path('admin/', authenticated_user(admin.site.urls)), path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), diff --git a/fet2020/templates/layout.html b/fet2020/templates/layout.html index 66493f63..8ffbc2e1 100644 --- a/fet2020/templates/layout.html +++ b/fet2020/templates/layout.html @@ -6,9 +6,9 @@ - FET DjangoLayout + FET - {% csrf_token %} + {% csrf_token %} {% block header %} {% endblock %} @@ -20,11 +20,16 @@
@@ -36,7 +41,6 @@