2014-10-02 15:58:27 +00:00
|
|
|
import os
|
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
|
|
|
from django.core import validators
|
2014-11-14 22:19:58 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-09-30 09:49:07 +00:00
|
|
|
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-10-01 21:03:16 +00:00
|
|
|
return user
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUser(models.Model):
|
|
|
|
""" System users """
|
|
|
|
username = models.CharField(_("username"), max_length=64, unique=True,
|
2014-10-30 16:34:02 +00:00
|
|
|
help_text=_("Required. 64 characters or fewer. Letters, digits and ./-/_ only."),
|
2014-11-14 15:51:18 +00:00
|
|
|
validators=[
|
|
|
|
validators.RegexValidator(r'^[\w.-]+$', _("Enter a valid username."))
|
|
|
|
])
|
2014-09-30 09:49:07 +00:00
|
|
|
password = models.CharField(_("password"), max_length=128)
|
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
|
|
|
related_name='systemusers')
|
2014-11-14 22:19:58 +00:00
|
|
|
home = models.CharField(_("home"), max_length=256, blank=True,
|
2014-11-14 14:38:06 +00:00
|
|
|
help_text=_("Starting location when login with this no-shell user."))
|
|
|
|
directory = models.CharField(_("directory"), max_length=256, blank=True,
|
|
|
|
help_text=_("Optional directory relative to user's home."))
|
|
|
|
shell = models.CharField(_("shell"), max_length=32, choices=settings.SYSTEMUSERS_SHELLS,
|
|
|
|
default=settings.SYSTEMUSERS_DEFAULT_SHELL)
|
2014-10-27 17:34:14 +00:00
|
|
|
groups = models.ManyToManyField('self', blank=True, symmetrical=False,
|
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-23 15:38:46 +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
|
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
2014-10-02 15:58:27 +00:00
|
|
|
try:
|
|
|
|
return self.is_active and self.account.is_active
|
|
|
|
except type(self).account.field.rel.to.DoesNotExist:
|
|
|
|
return self.is_active
|
|
|
|
|
2014-10-23 21:25:44 +00:00
|
|
|
@cached_property
|
2014-10-23 15:38:46 +00:00
|
|
|
def is_main(self):
|
2014-10-23 21:25:44 +00:00
|
|
|
# TODO on account delete
|
2014-10-23 15:38:46 +00:00
|
|
|
# On account creation main_systemuser_id is still None
|
|
|
|
if self.account.main_systemuser_id:
|
|
|
|
return self.account.main_systemuser_id == self.pk
|
|
|
|
return self.account.username == self.username
|
|
|
|
|
2014-11-14 14:38:06 +00:00
|
|
|
@property
|
|
|
|
def has_shell(self):
|
|
|
|
return self.shell not in settings.SYSTEMUSERS_DISABLED_SHELLS
|
|
|
|
|
2014-11-14 22:19:58 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.home:
|
2014-11-14 14:38:06 +00:00
|
|
|
self.home = self.get_base_home()
|
2014-11-14 22:19:58 +00:00
|
|
|
super(SystemUser, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
2014-11-14 22:23:20 +00:00
|
|
|
if self.directory:
|
|
|
|
directory_error = None
|
|
|
|
if self.has_shell:
|
|
|
|
directory_error = _("Directory with shell users can not be specified.")
|
|
|
|
elif self.pk and self.is_main:
|
|
|
|
directory_error = _("Directory with main system users can not be specified.")
|
|
|
|
elif self.home == self.get_base_home():
|
|
|
|
directory_error = _("Directory on the user's base home is not allowed.")
|
|
|
|
if directory_error:
|
|
|
|
raise ValidationError({
|
|
|
|
'directory': directory_error,
|
|
|
|
})
|
2014-11-14 14:38:06 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def set_password(self, raw_password):
|
|
|
|
self.password = make_password(raw_password)
|
|
|
|
|
2014-11-14 14:38:06 +00:00
|
|
|
def get_base_home(self):
|
|
|
|
context = {
|
|
|
|
'username': self.username,
|
|
|
|
}
|
|
|
|
return settings.SYSTEMUSERS_HOME % context
|
|
|
|
|
2014-10-02 15:58:27 +00:00
|
|
|
def get_home(self):
|
2014-11-14 15:51:18 +00:00
|
|
|
return os.path.join(
|
|
|
|
self.home or self.get_base_home(),
|
|
|
|
self.directory
|
|
|
|
)
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
services.register(SystemUser)
|