From 1385a2ef1696510468e306e59fad8d69cd0731cd Mon Sep 17 00:00:00 2001 From: Joel Falknau Date: Sun, 29 Dec 2024 21:15:48 +1000 Subject: [PATCH] unify user types --- .../services/modules/mumble/models.py | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/allianceauth/services/modules/mumble/models.py b/allianceauth/services/modules/mumble/models.py index 28217c8a..9deee343 100644 --- a/allianceauth/services/modules/mumble/models.py +++ b/allianceauth/services/modules/mumble/models.py @@ -1,6 +1,5 @@ import random import string -from typing import LiteralString from allianceauth.eveonline.models import EveCharacter from allianceauth.services.hooks import NameFormatter from passlib.hash import bcrypt_sha256 @@ -50,6 +49,11 @@ class MumbleUser(AbstractServiceModel): blank=True, null=True, editable=False, help_text="Timestamp of the users Last Disconnection from Mumble") + @property + def display_name(self) -> str: + from .auth_hooks import MumbleService + return NameFormatter(MumbleService(), self.user).format_name() + @staticmethod def get_username(user) -> str: return user.profile.main_character.character_name # main character as the user.username may be incorrect @@ -97,13 +101,6 @@ class MumbleUser(AbstractServiceModel): groups_str.append(str(group.name)) return ','.join({g.replace(' ', '-') for g in groups_str}) - def get_display_name(self) -> str: - from .auth_hooks import MumbleService - return NameFormatter(MumbleService(), self.user).format_name() - - def display_name(self) -> str: - return self.get_display_name() - def create(self, user): try: username = self.get_username(user) @@ -146,13 +143,16 @@ class TempLink(models.Model): class TempUser(models.Model): + class HashFunction(models.TextChoices): + SHA256 = 'bcrypt-sha256', _('SHA256') + SHA1 = 'sha1', _('SHA1') name = models.CharField(max_length=200) # Display name to show username = models.CharField(max_length=254, unique=True) pwhash = models.CharField(max_length=90) hashfn = models.CharField( max_length=15, - choices=MumbleUser.HashFunction.choices, - default=MumbleUser.HashFunction.SHA256) + choices=HashFunction.choices, + default=HashFunction.SHA256) certhash = models.CharField( verbose_name="Certificate Hash", max_length=254,blank=True, null=True, editable=False, @@ -178,9 +178,40 @@ class TempUser(models.Model): templink = models.ForeignKey(TempLink, on_delete=models.CASCADE, null=True, default=None) character_id = models.IntegerField(default=None, blank=True, null=True) + @property + def display_name(self) -> str: + from .auth_hooks import MumbleService + return NameFormatter(MumbleService(), self.user).format_name() + + @staticmethod + def get_username(user) -> str: + return user.profile.main_character.character_name # main character as the user.username may be incorrect + + @staticmethod + def sanitise_username(username) -> str: + return username.replace(" ", "_") + + @staticmethod + def generate_random_pass() -> str: + return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) + + @staticmethod + def gen_pwhash(password) -> str: + return bcrypt_sha256.encrypt(password.encode('utf-8')) + def __str__(self) -> str: return f"Temp User: {self.username} - {self.name}" + def group_string(self) -> str: + """Overwritten from MumbleUser, we could add features to this in the future + """ + return str(["Guest"]) + + class Meta: + verbose_name = _("Temp User") + verbose_name_plural = _("Temp Users") + permissions = () + class IdlerHandler(models.Model): name = models.CharField(_("Name"), max_length=50)