def signout(request): if request.user.is_authenticated(): log_entry = MyLogEntry(user=User.objects.get(pk=request.user.id), message="logged out.") log_entry.save() logout(request) return HttpResponseRedirect('/')
def signin(request, **kwargs): if request.user.is_authenticated(): return redirect(settings.LOGIN_REDIRECT_URL) elif request.method == 'POST': username = request.POST.get('username', '') password = request.POST.get('password', '') user = auth.authenticate(username=username, password=password) user_filter = User.objects.filter(username=username) user_obj = user_filter[0] if len(user_filter) else None if user and user_obj and user_obj.is_active: log_entry = MyLogEntry(user=User.objects.get(pk=user.id), message="logged in.") log_entry.save() elif username and password: pool = request.POST.get('pool', '') year = request.POST.get('year', '') s = ldap3.Server("ldaps://ldap.42.fr", port=636, use_ssl=True, get_info=ldap3.GET_ALL_INFO) try: c = ldap3.Connection(s, authentication=ldap3.AUTH_SIMPLE, check_names=True, auto_bind=True, user="******"+username+",ou="+pool+",ou="+year+",ou=paris,ou=people,dc=42,dc=fr", password=password) except ldap3.LDAPBindError: return login(request, **kwargs) if c.result['description'] == 'success': user_obj = User.objects.get(username=username) user_obj.set_password(password) user_obj.is_active = True user_obj.save() return login(request, **kwargs)