diff --git a/fet2020/accounts/__init__.py b/fet2020/accounts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fet2020/accounts/admin.py b/fet2020/accounts/admin.py new file mode 100644 index 00000000..4185d360 --- /dev/null +++ b/fet2020/accounts/admin.py @@ -0,0 +1,3 @@ +# from django.contrib import admin + +# Register your models here. diff --git a/fet2020/accounts/apps.py b/fet2020/accounts/apps.py new file mode 100644 index 00000000..9b3fc5a4 --- /dev/null +++ b/fet2020/accounts/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AccountsConfig(AppConfig): + name = 'accounts' diff --git a/fet2020/accounts/authentications.py b/fet2020/accounts/authentications.py new file mode 100644 index 00000000..cf928a75 --- /dev/null +++ b/fet2020/accounts/authentications.py @@ -0,0 +1,27 @@ +import ldap3 + + +def authentication(username, password): + # no empty passwords + if password is None or password.strip() == "": + # messages.info("username:%s Login denied for blank password", username) + return None + + # username format + new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + userdn = new_username.format(username=username) + + server_uri = 'ipa.demo1.freeipa.org' + server = ldap3.Server(server_uri, get_info=ldap3.ALL) + + try: + ldap3.Connection( + server, + userdn, + password, + auto_bind=True, + ) + except ldap3.core.exceptions.LDAPBindError: + username = None + + return username diff --git a/fet2020/accounts/decorators.py b/fet2020/accounts/decorators.py new file mode 100644 index 00000000..db0e0b60 --- /dev/null +++ b/fet2020/accounts/decorators.py @@ -0,0 +1,11 @@ +from django.shortcuts import redirect + + +def unauthenticated_user(view_func): + def wrapper_func(request, *args, **kwargs): + if request.user.is_authenticated: + return redirect('home') + else: + return view_func(request, *args, **kwargs) + + return wrapper_func diff --git a/fet2020/accounts/models.py b/fet2020/accounts/models.py new file mode 100644 index 00000000..0b4331b3 --- /dev/null +++ b/fet2020/accounts/models.py @@ -0,0 +1,3 @@ +# from django.db import models + +# Create your models here. diff --git a/fet2020/accounts/tests.py b/fet2020/accounts/tests.py new file mode 100644 index 00000000..a79ca8be --- /dev/null +++ b/fet2020/accounts/tests.py @@ -0,0 +1,3 @@ +# from django.test import TestCase + +# Create your tests here. diff --git a/fet2020/accounts/urls.py b/fet2020/accounts/urls.py new file mode 100644 index 00000000..7c084c15 --- /dev/null +++ b/fet2020/accounts/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + + +urlpatterns = [ + path('login/', views.loginPage, name="login"), + path('logout/', views.logoutUser, name="logout"), +] diff --git a/fet2020/accounts/views.py b/fet2020/accounts/views.py new file mode 100644 index 00000000..7ec72d10 --- /dev/null +++ b/fet2020/accounts/views.py @@ -0,0 +1,40 @@ +from django.shortcuts import render, redirect +from django.contrib.auth import login, logout +from django.contrib import messages +from django.contrib.auth.models import User + +from .authentications import authentication +# from .decorators import 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') + + auth_user = authentication(username, password) + + if auth_user is not None: + try: + user = User.objects.get(username=username) + except User.DoesNotExist: + user = User.objects.create_user(auth_user) + + login(request, user) + return redirect('home') + else: + messages.info(request, 'username OR password is incorrect') + + context = {} + return render(request, 'accounts/login.html', context) + + +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/documents/__init__.py b/fet2020/documents/__init__.py index 2fbee146..47b229c3 100644 --- a/fet2020/documents/__init__.py +++ b/fet2020/documents/__init__.py @@ -1,5 +1,5 @@ from etherpad_lite import EtherpadLiteClient -from django.conf import settings +# from django.conf import settings from datetime import datetime, timedelta from django.utils.text import slugify import urllib.parse @@ -78,4 +78,4 @@ def add_ep_to_response(request, response): httponly=False ) - return response \ No newline at end of file + return response diff --git a/fet2020/documents/apps.py b/fet2020/documents/apps.py new file mode 100644 index 00000000..1450e243 --- /dev/null +++ b/fet2020/documents/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig +# from django.contrib.admin.apps import AdminConfig + + +class DocumentsConfig(AppConfig): + name = 'documents' diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index 8f60ff18..f907bb10 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -64,8 +64,10 @@ INSTALLED_APPS = [ 'rest_framework', 'django_filters', 'django_static_jquery_ui', + 'accounts.apps.AccountsConfig', 'posts.apps.PostsConfig', 'members.apps.MembersConfig', + 'documents.apps.DocumentsConfig', ] MIDDLEWARE = [ @@ -114,7 +116,7 @@ DATABASES = { } AUTHENTICATION_BACKENDS = [ - 'django.contrib.auth.backends.RemoteUserBackend' + 'django.contrib.auth.backends.RemoteUserBackend', ] # Password validation diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index fb8164f1..7e33c346 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -35,4 +35,6 @@ urlpatterns = [ path('ckeditor/', include('ckeditor_uploader.urls')), path('api/', include(router.urls)), path('members/', include('members.urls'), name='members'), + path('accounts/', include('accounts.urls')), + ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/fet2020/requirements.txt b/fet2020/requirements.txt index a7c752ff..51907c99 100644 --- a/fet2020/requirements.txt +++ b/fet2020/requirements.txt @@ -5,4 +5,5 @@ Pillow djangorestframework django-static-jquery-ui docutils -easy-thumbnails \ No newline at end of file +easy-thumbnails +etherpad-lite \ No newline at end of file diff --git a/fet2020/templates/accounts/login.html b/fet2020/templates/accounts/login.html new file mode 100644 index 00000000..85620a87 --- /dev/null +++ b/fet2020/templates/accounts/login.html @@ -0,0 +1,123 @@ + + + +
+{{message}}
+ {% endfor %} + +