unify user types

This commit is contained in:
Joel Falknau 2024-12-29 21:15:48 +10:00
parent d09892397b
commit 1385a2ef16
No known key found for this signature in database

View File

@ -1,6 +1,5 @@
import random import random
import string import string
from typing import LiteralString
from allianceauth.eveonline.models import EveCharacter from allianceauth.eveonline.models import EveCharacter
from allianceauth.services.hooks import NameFormatter from allianceauth.services.hooks import NameFormatter
from passlib.hash import bcrypt_sha256 from passlib.hash import bcrypt_sha256
@ -50,6 +49,11 @@ class MumbleUser(AbstractServiceModel):
blank=True, null=True, editable=False, blank=True, null=True, editable=False,
help_text="Timestamp of the users Last Disconnection from Mumble") 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 @staticmethod
def get_username(user) -> str: def get_username(user) -> str:
return user.profile.main_character.character_name # main character as the user.username may be incorrect 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)) groups_str.append(str(group.name))
return ','.join({g.replace(' ', '-') for g in groups_str}) 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): def create(self, user):
try: try:
username = self.get_username(user) username = self.get_username(user)
@ -146,13 +143,16 @@ class TempLink(models.Model):
class TempUser(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 name = models.CharField(max_length=200) # Display name to show
username = models.CharField(max_length=254, unique=True) username = models.CharField(max_length=254, unique=True)
pwhash = models.CharField(max_length=90) pwhash = models.CharField(max_length=90)
hashfn = models.CharField( hashfn = models.CharField(
max_length=15, max_length=15,
choices=MumbleUser.HashFunction.choices, choices=HashFunction.choices,
default=MumbleUser.HashFunction.SHA256) default=HashFunction.SHA256)
certhash = models.CharField( certhash = models.CharField(
verbose_name="Certificate Hash", verbose_name="Certificate Hash",
max_length=254,blank=True, null=True, editable=False, 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) templink = models.ForeignKey(TempLink, on_delete=models.CASCADE, null=True, default=None)
character_id = models.IntegerField(default=None, blank=True, null=True) 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: def __str__(self) -> str:
return f"Temp User: {self.username} - {self.name}" 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): class IdlerHandler(models.Model):
name = models.CharField(_("Name"), max_length=50) name = models.CharField(_("Name"), max_length=50)