You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

108 rivejä
2.8 KiB

  1. import uuid
  2. from django.db import models
  3. from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
  4. class LesspassUserManager(BaseUserManager):
  5. def create_user(self, email, password=None):
  6. if not email:
  7. raise ValueError("Users must have an email address")
  8. user = self.model(
  9. email=self.normalize_email(email),
  10. )
  11. user.set_password(password)
  12. user.save(using=self._db)
  13. return user
  14. def create_superuser(self, email, password):
  15. user = self.create_user(
  16. email,
  17. password=password,
  18. )
  19. user.is_admin = True
  20. user.save(using=self._db)
  21. return user
  22. class LessPassUser(AbstractBaseUser):
  23. email = models.EmailField(verbose_name="email address", max_length=255, unique=True)
  24. is_active = models.BooleanField(default=True)
  25. is_admin = models.BooleanField(default=False)
  26. key = models.TextField(null=True)
  27. objects = LesspassUserManager()
  28. USERNAME_FIELD = "email"
  29. REQUIRED_FIELDS = ["key"]
  30. def get_full_name(self):
  31. return self.email
  32. def get_short_name(self):
  33. return self.email
  34. def __str__(self):
  35. return self.email
  36. def has_perm(self, perm, obj=None):
  37. return True
  38. def has_module_perms(self, app_label):
  39. return True
  40. @property
  41. def is_superuser(self):
  42. return self.is_admin
  43. @property
  44. def is_staff(self):
  45. return self.is_admin
  46. class Meta:
  47. verbose_name_plural = "Users"
  48. class DateMixin(models.Model):
  49. created = models.DateTimeField(auto_now_add=True, verbose_name="created")
  50. modified = models.DateTimeField(auto_now=True, verbose_name="modified")
  51. class Meta:
  52. abstract = True
  53. class Password(DateMixin):
  54. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  55. user = models.ForeignKey(
  56. LessPassUser, on_delete=models.CASCADE, related_name="passwords"
  57. )
  58. login = models.CharField(max_length=255, null=True, blank=True)
  59. site = models.CharField(max_length=255, null=True, blank=True)
  60. lowercase = models.BooleanField(default=True)
  61. uppercase = models.BooleanField(default=True)
  62. symbols = models.BooleanField(default=True)
  63. numbers = models.BooleanField(default=True)
  64. length = models.IntegerField(default=16)
  65. counter = models.IntegerField(default=1)
  66. version = models.IntegerField(default=2)
  67. def __str__(self):
  68. return str(self.id)
  69. class EncryptedPasswordProfile(DateMixin):
  70. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  71. user = models.ForeignKey(
  72. LessPassUser,
  73. on_delete=models.CASCADE,
  74. related_name="encrypted_password_profiles",
  75. )
  76. password_profile = models.TextField()
  77. def __str__(self):
  78. return str(self.id)