2014-09-30 09:49:07 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
|
|
|
from django.core import validators
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.db import models
|
2014-10-01 16:42:40 +00:00
|
|
|
from django.utils.functional import cached_property
|
2014-09-30 09:49:07 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.core import services
|
|
|
|
|
|
|
|
from . import settings
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserQuerySet(models.QuerySet):
|
|
|
|
def create_user(self, username, password='', **kwargs):
|
|
|
|
user = super(SystemUserQuerySet, self).create(username=username, **kwargs)
|
|
|
|
user.set_password(password)
|
2014-09-30 16:06:42 +00:00
|
|
|
user.save(update_fields=['password'])
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUser(models.Model):
|
|
|
|
""" System users """
|
|
|
|
username = models.CharField(_("username"), max_length=64, unique=True,
|
|
|
|
help_text=_("Required. 30 characters or fewer. Letters, digits and ./-/_ only."),
|
|
|
|
validators=[validators.RegexValidator(r'^[\w.-]+$',
|
|
|
|
_("Enter a valid username."), 'invalid')])
|
|
|
|
password = models.CharField(_("password"), max_length=128)
|
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
|
|
|
related_name='systemusers')
|
|
|
|
home = models.CharField(_("home"), max_length=256, blank=True,
|
2014-09-30 10:20:11 +00:00
|
|
|
help_text=_("Home directory relative to account's ~main_user"))
|
2014-09-30 09:49:07 +00:00
|
|
|
shell = models.CharField(_("shell"), max_length=32,
|
2014-10-01 16:42:40 +00:00
|
|
|
choices=settings.SYSTEMUSERS_SHELLS, default=settings.SYSTEMUSERS_DEFAULT_SHELL)
|
|
|
|
groups = models.ManyToManyField('self', blank=True,
|
2014-09-30 09:49:07 +00:00
|
|
|
help_text=_("A new group will be created for the user. "
|
|
|
|
"Which additional groups would you like them to be a member of?"))
|
2014-10-01 16:42:40 +00:00
|
|
|
is_main = models.BooleanField(_("is main"), default=False)
|
2014-09-30 09:49:07 +00:00
|
|
|
is_active = models.BooleanField(_("active"), default=True,
|
|
|
|
help_text=_("Designates whether this account should be treated as active. "
|
|
|
|
"Unselect this instead of deleting accounts."))
|
|
|
|
|
|
|
|
objects = SystemUserQuerySet.as_manager()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.username
|
|
|
|
|
|
|
|
def set_password(self, raw_password):
|
|
|
|
self.password = make_password(raw_password)
|
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
|
|
|
return self.is_active and self.account.is_active
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
## TODO user deletion and group handling.
|
|
|
|
#class SystemGroup(models.Model):
|
|
|
|
# name = models.CharField(_("name"), max_length=64, unique=True,
|
|
|
|
# help_text=_("Required. 30 characters or fewer. Letters, digits and ./-/_ only."),
|
|
|
|
# validators=[validators.RegexValidator(r'^[\w.-]+$',
|
|
|
|
# _("Enter a valid group name."), 'invalid')])
|
|
|
|
# account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
|
|
|
# related_name='systemgroups')
|
|
|
|
#
|
|
|
|
# def __unicode__(self):
|
|
|
|
# return self.name
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
services.register(SystemUser)
|