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.

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