|
|
@@ -27,19 +27,29 @@ from desktop.models import uuid_default
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
-# -----------------------------------------------------------------------
|
|
|
-# Organizations
|
|
|
-# -----------------------------------------------------------------------
|
|
|
+'''
|
|
|
+Organizations
|
|
|
+'''
|
|
|
+
|
|
|
+def default_organization():
|
|
|
+ default_organization, created = Organization.objects.get_or_create(name='default')
|
|
|
+ return default_organization
|
|
|
+
|
|
|
+def get_organization(user):
|
|
|
+ # TODO: depends on the logged-in user and its organization
|
|
|
+ return default_organization()
|
|
|
+
|
|
|
|
|
|
def uuid_default():
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
class OrganizationManager(models.Manager):
|
|
|
- use_in_migrations = True
|
|
|
+ use_in_migrations = True
|
|
|
+
|
|
|
+ def get_by_natural_key(self, name):
|
|
|
+ return self.get(name=name)
|
|
|
|
|
|
- def get_by_natural_key(self, name):
|
|
|
- return self.get(name=name)
|
|
|
|
|
|
class Organization(models.Model):
|
|
|
name = models.CharField(max_length=200, help_text=_t("The name of the organization"))
|
|
|
@@ -58,6 +68,7 @@ class OrganizationGroupManager(models.Manager):
|
|
|
def natural_key(self):
|
|
|
return (self.organization, self.name,)
|
|
|
|
|
|
+
|
|
|
class OrganizationGroup(models.Model):
|
|
|
name = models.CharField(_t('name'), max_length=80, unique=False)
|
|
|
organization = models.ForeignKey(Organization)
|
|
|
@@ -85,80 +96,72 @@ class UserManager(BaseUserManager):
|
|
|
use_in_migrations = True
|
|
|
|
|
|
def _create_user(self, email, password, **extra_fields):
|
|
|
- """Create and save a User with the given email and password."""
|
|
|
- if not email:
|
|
|
- raise ValueError('The given email must be set')
|
|
|
- email = self.normalize_email(email)
|
|
|
- user = self.model(email=email, **extra_fields)
|
|
|
- user.set_password(password)
|
|
|
- user.save(using=self._db)
|
|
|
- return user
|
|
|
+ """Create and save a User with the given email and password."""
|
|
|
+ if not email:
|
|
|
+ raise ValueError('The given email must be set')
|
|
|
+ email = self.normalize_email(email)
|
|
|
+ user = self.model(email=email, **extra_fields)
|
|
|
+ user.set_password(password)
|
|
|
+ user.save(using=self._db)
|
|
|
+ return user
|
|
|
|
|
|
def create_user(self, email, password=None, **extra_fields):
|
|
|
- """Create and save a regular User with the given email and password."""
|
|
|
- extra_fields.setdefault('is_staff', False)
|
|
|
- extra_fields.setdefault('is_superuser', False)
|
|
|
- extra_fields.setdefault('is_admin', False)
|
|
|
- return self._create_user(email, password, **extra_fields)
|
|
|
+ """Create and save a regular User with the given email and password."""
|
|
|
+ extra_fields.setdefault('is_staff', False)
|
|
|
+ extra_fields.setdefault('is_superuser', False)
|
|
|
+ extra_fields.setdefault('is_admin', False)
|
|
|
+ return self._create_user(email, password, **extra_fields)
|
|
|
|
|
|
def create_superuser(self, email, password, **extra_fields):
|
|
|
- """Create and save a SuperUser with the given email and password."""
|
|
|
- extra_fields.setdefault('is_staff', False)
|
|
|
- extra_fields.setdefault('is_superuser', False)
|
|
|
- extra_fields.setdefault('is_admin', True)
|
|
|
+ """Create and save a SuperUser with the given email and password."""
|
|
|
+ extra_fields.setdefault('is_staff', False)
|
|
|
+ extra_fields.setdefault('is_superuser', False)
|
|
|
+ extra_fields.setdefault('is_admin', True)
|
|
|
|
|
|
- if extra_fields.get('is_staff') is not False:
|
|
|
- raise ValueError('Organization superuser must have is_staff=False.')
|
|
|
- if extra_fields.get('is_superuser') is not False:
|
|
|
- raise ValueError('Organization superuser must have is_superuser=False.')
|
|
|
- if extra_fields.get('is_admin') is not True:
|
|
|
- raise ValueError('Organization superuser must have is_admin=False.')
|
|
|
+ if extra_fields.get('is_staff') is not False:
|
|
|
+ raise ValueError('Organization superuser must have is_staff=False.')
|
|
|
+ if extra_fields.get('is_superuser') is not False:
|
|
|
+ raise ValueError('Organization superuser must have is_superuser=False.')
|
|
|
+ if extra_fields.get('is_admin') is not True:
|
|
|
+ raise ValueError('Organization superuser must have is_admin=True.')
|
|
|
|
|
|
- return self._create_user(email, password, **extra_fields)
|
|
|
+ return self._create_user(email, password, **extra_fields)
|
|
|
|
|
|
-def default_organization():
|
|
|
- default_organization, created = Organization.objects.get_or_create(name='default')
|
|
|
- return default_organization
|
|
|
|
|
|
-def get_organization(user):
|
|
|
- # TODO: depends on the logged-in user and its organization
|
|
|
- return default_organization()
|
|
|
+class OrganizationUser(AbstractUser):
|
|
|
+ """User model in a multi tenant setup."""
|
|
|
|
|
|
+ username = None
|
|
|
+ email = models.EmailField(_t('Email address'), unique=True)
|
|
|
+ token = models.CharField(_t('Token'), max_length=128, default=None, null=True)
|
|
|
+ organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
|
|
|
+ is_admin = models.BooleanField(default=False)
|
|
|
|
|
|
-class OrganizationUser(AbstractUser):
|
|
|
- """User model in a multi tenant setup."""
|
|
|
-
|
|
|
- username = None
|
|
|
- email = models.EmailField(_t('Email address'), unique=True)
|
|
|
- token = models.CharField(_t('Token'), max_length=128, default=None, null=True)
|
|
|
- organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
|
|
|
- is_admin = models.BooleanField(default=False)
|
|
|
-
|
|
|
- groups = models.ManyToManyField(
|
|
|
- OrganizationGroup,
|
|
|
- verbose_name=_t('groups'),
|
|
|
- blank=True,
|
|
|
- help_text=_t(
|
|
|
- 'The groups this user belongs to. A user will get all permissions '
|
|
|
- 'granted to each of their groups.'
|
|
|
- ),
|
|
|
- related_name="user_set",
|
|
|
- related_query_name="user",
|
|
|
- )
|
|
|
-
|
|
|
- USERNAME_FIELD = 'email'
|
|
|
- REQUIRED_FIELDS = []
|
|
|
-
|
|
|
- objects = UserManager()
|
|
|
-
|
|
|
- @property
|
|
|
- def username(self):
|
|
|
- return self.email
|
|
|
-
|
|
|
- @property
|
|
|
- def username_short(self):
|
|
|
- return self.email.split('@')[0]
|
|
|
-
|
|
|
- @username.setter
|
|
|
- def username(self, value):
|
|
|
- pass
|
|
|
+ groups = models.ManyToManyField(
|
|
|
+ OrganizationGroup,
|
|
|
+ verbose_name=_t('groups'),
|
|
|
+ blank=True,
|
|
|
+ help_text=_t(
|
|
|
+ 'The groups this user belongs to. A user will get all permissions '
|
|
|
+ 'granted to each of their groups.'
|
|
|
+ ),
|
|
|
+ related_name="user_set",
|
|
|
+ related_query_name="user",
|
|
|
+ )
|
|
|
+
|
|
|
+ USERNAME_FIELD = 'email'
|
|
|
+ REQUIRED_FIELDS = []
|
|
|
+
|
|
|
+ objects = UserManager()
|
|
|
+
|
|
|
+ @property
|
|
|
+ def username(self):
|
|
|
+ return self.email
|
|
|
+
|
|
|
+ @property
|
|
|
+ def username_short(self):
|
|
|
+ return self.email.split('@')[0]
|
|
|
+
|
|
|
+ @username.setter
|
|
|
+ def username(self, value):
|
|
|
+ pass
|