2020-05-31 12:08:26 +00:00
|
|
|
from core.helpers.mail import get_template, simple_send_mail
|
2020-05-21 12:54:59 +00:00
|
|
|
from core.helpers.urls import relative_to_absolute as reltoabs
|
2020-06-01 16:29:22 +00:00
|
|
|
from core.models.auth import LoginLog, PWResetToken, IPLimit
|
2020-05-23 13:30:21 +00:00
|
|
|
from core.helpers.request import get_client_ip
|
2020-05-21 12:54:59 +00:00
|
|
|
|
|
|
|
from django.urls import reverse
|
2020-05-23 13:30:21 +00:00
|
|
|
from django.contrib import messages
|
2020-05-24 15:44:27 +00:00
|
|
|
from django.utils import timezone
|
2020-05-21 12:54:59 +00:00
|
|
|
|
|
|
|
from dbsettings.functions import getValue
|
|
|
|
|
2020-05-13 10:38:37 +00:00
|
|
|
def generate_pwreset_mail(user, token):
|
2020-05-21 12:54:59 +00:00
|
|
|
link = reltoabs(reverse("pwreset", kwargs={"pk": str(token.token)}))
|
|
|
|
template = get_template("backend/auth/pwreset", first_name=user.first_name, link=link, sitename=getValue("core.title", "Expephalon"))
|
2020-05-23 11:59:53 +00:00
|
|
|
return template
|
|
|
|
|
2020-05-23 13:30:21 +00:00
|
|
|
def login_fail(request, user=None, message=None):
|
|
|
|
LoginLog.objects.create(user=user, ip=get_client_ip(request), success=False)
|
|
|
|
if message:
|
|
|
|
messages.error(request, message)
|
|
|
|
|
|
|
|
def login_success(request, user):
|
2020-05-24 15:44:27 +00:00
|
|
|
LoginLog.objects.create(user=user, ip=get_client_ip(request), success=True)
|
|
|
|
|
|
|
|
def clear_login_log(maxage=int(getValue("core.auth.ratelimit.period", 600))):
|
2020-05-24 17:22:17 +00:00
|
|
|
timestamp = timezone.now() - timezone.timedelta(seconds=maxage)
|
|
|
|
LoginLog.objects.filter(timestamp__lt=timestamp).delete()
|
|
|
|
|
|
|
|
def clear_ratelimits(maxage=int(getValue("core.auth.ratelimit.block", 3600))):
|
2020-05-24 15:44:27 +00:00
|
|
|
timestamp = timezone.now() - timezone.timedelta(seconds=maxage)
|
2020-06-01 16:29:22 +00:00
|
|
|
IPLimit.objects.filter(end__lt=timestamp).delete()
|
2020-05-31 12:08:26 +00:00
|
|
|
|
|
|
|
def request_password(user):
|
|
|
|
token = PWResetToken.objects.create(user=user)
|
|
|
|
mail = generate_pwreset_mail(user, token)
|
|
|
|
simple_send_mail("Password Reset", mail, user.email)
|