jumpserver/apps/perms/models.py

93 lines
4.0 KiB
Python
Raw Normal View History

2017-11-23 06:08:01 +00:00
import uuid
2016-08-08 16:43:11 +00:00
from django.db import models
2016-09-10 13:08:10 +00:00
from django.utils.translation import ugettext_lazy as _
2016-09-11 01:50:42 +00:00
from django.utils import timezone
2016-08-08 16:43:11 +00:00
from common.utils import date_expired_default, set_or_append_attr_bulk
2016-09-10 13:08:10 +00:00
from orgs.mixins import OrgModelMixin, OrgManager
2016-09-10 13:08:10 +00:00
class AssetPermissionQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def valid(self):
return self.active().filter(date_start__lt=timezone.now())\
2018-04-07 16:16:37 +00:00
.filter(date_expired__gt=timezone.now())
class AssetPermissionManager(OrgManager):
def valid(self):
return self.get_queryset().valid()
class AssetPermission(OrgModelMixin):
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2018-07-27 08:21:55 +00:00
name = models.CharField(max_length=128, verbose_name=_('Name'))
2018-04-07 16:16:37 +00:00
users = models.ManyToManyField('users.User', related_name='asset_permissions', blank=True, verbose_name=_("User"))
user_groups = models.ManyToManyField('users.UserGroup', related_name='asset_permissions', blank=True, verbose_name=_("User group"))
assets = models.ManyToManyField('assets.Asset', related_name='granted_by_permissions', blank=True, verbose_name=_("Asset"))
nodes = models.ManyToManyField('assets.Node', related_name='granted_by_permissions', blank=True, verbose_name=_("Nodes"))
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_permissions', verbose_name=_("System user"))
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
date_start = models.DateTimeField(default=timezone.now, db_index=True, verbose_name=_("Date start"))
date_expired = models.DateTimeField(default=date_expired_default, db_index=True, verbose_name=_('Date expired'))
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
2016-09-11 08:59:19 +00:00
comment = models.TextField(verbose_name=_('Comment'), blank=True)
2016-09-11 01:50:42 +00:00
objects = AssetPermissionManager.from_queryset(AssetPermissionQuerySet)()
2018-04-07 16:16:37 +00:00
2018-07-27 08:21:55 +00:00
class Meta:
unique_together = [('org_id', 'name')]
2017-12-18 10:38:30 +00:00
def __str__(self):
2016-09-16 08:09:11 +00:00
return self.name
2016-09-11 01:50:42 +00:00
2018-04-07 16:16:37 +00:00
@property
def id_str(self):
return str(self.id)
2016-09-11 01:50:42 +00:00
@property
2016-09-11 14:45:24 +00:00
def is_valid(self):
2018-04-07 16:16:37 +00:00
if self.date_expired > timezone.now() > self.date_start and self.is_active:
2016-09-11 01:50:42 +00:00
return True
2017-12-18 10:38:30 +00:00
return False
2016-09-11 01:50:42 +00:00
def get_all_users(self):
users = set(self.users.all())
for group in self.user_groups.all():
_users = group.users.all()
set_or_append_attr_bulk(_users, 'inherit', group.name)
users.update(set(_users))
return users
def get_all_assets(self):
assets = set(self.assets.all())
for node in self.nodes.all():
_assets = node.get_all_assets()
set_or_append_attr_bulk(_assets, 'inherit', node.value)
assets.update(set(_assets))
return assets
2018-02-01 07:06:44 +00:00
class NodePermission(OrgModelMixin):
2018-02-01 09:14:15 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2018-02-01 07:06:44 +00:00
node = models.ForeignKey('assets.Node', on_delete=models.CASCADE, verbose_name=_("Node"))
user_group = models.ForeignKey('users.UserGroup', on_delete=models.CASCADE, verbose_name=_("User group"))
system_user = models.ForeignKey('assets.SystemUser', on_delete=models.CASCADE, verbose_name=_("System user"))
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired'))
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
comment = models.TextField(verbose_name=_('Comment'), blank=True)
def __str__(self):
2018-02-02 10:52:09 +00:00
return "{}:{}:{}".format(self.node.value, self.user_group.name, self.system_user.name)
2018-02-01 07:06:44 +00:00
class Meta:
unique_together = ('node', 'user_group', 'system_user')
2018-02-02 10:52:09 +00:00
verbose_name = _("Asset permission")