Skip to content

Commit

Permalink
auth: Add model to store user invitations
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Jul 14, 2023
1 parent 5674acc commit aae1ed2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
54 changes: 54 additions & 0 deletions weblate/auth/migrations/0028_invitation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Generated by Django 4.2.1 on 2023-07-14 08:31

import uuid

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

import weblate.utils.fields


class Migration(migrations.Migration):
dependencies = [
("weblate_auth", "0027_alter_group_components"),
]

operations = [
migrations.CreateModel(
name="Invitation",
fields=[
(
"uuid",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("timestamp", models.DateTimeField(auto_now_add=True)),
("email", weblate.utils.fields.EmailField(blank=True, max_length=190)),
("is_superuser", models.BooleanField(default=False)),
(
"group",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="weblate_auth.group",
),
),
(
"user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
19 changes: 19 additions & 0 deletions weblate/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import re
import uuid
from collections import defaultdict
from functools import cache as functools_cache
from itertools import chain
Expand Down Expand Up @@ -906,6 +907,24 @@ def setup_project_groups(
group.roles.add(Role.objects.get(name=ACL_GROUPS[group_name]))


class Invitation(models.Model):
"""
User invitation store.
Either user or e-mail attribute is set, this is to invite current and new users.
"""

uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
timestamp = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.deletion.CASCADE, null=True)
group = models.ForeignKey(Group, on_delete=models.deletion.CASCADE)
email = EmailField(blank=True)
is_superuser = models.BooleanField(default=False)

def __str__(self):
return "invitation for {self.user or self.email} to {group}"


class WeblateAuthConf(AppConf):
"""Authentication settings."""

Expand Down
16 changes: 15 additions & 1 deletion weblate/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from datetime import timedelta

from celery.schedules import crontab
from django.conf import settings
from django.utils import timezone

from weblate.auth.models import User
from weblate.auth.models import Invitation, User
from weblate.utils.celery import app


Expand All @@ -15,6 +19,16 @@ def disable_expired():
)


@app.task(trail=False)
def cleanup_invitations():
Invitation.objects.filter(
timestamp__lte=timezone.now() - timedelta(seconds=settings.AUTH_TOKEN_VALID)
).delete()


@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(3600, disable_expired.s(), name="disable-expired")
sender.add_periodic_task(
crontab(hour=6, minute=6), cleanup_invitations.s(), name="cleanup_invitations"
)

0 comments on commit aae1ed2

Please sign in to comment.