2018-07-12 16:00:35 +00:00
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
2021-04-08 05:55:58 +00:00
|
|
|
|
from common.utils import lazyproperty, settings
|
2021-07-27 08:06:00 +00:00
|
|
|
|
from common.tree import TreeNode
|
2018-08-16 04:44:39 +00:00
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2022-03-16 09:50:07 +00:00
|
|
|
|
class OrgRoleMixin:
|
|
|
|
|
members: models.Manager
|
|
|
|
|
|
|
|
|
|
def get_members(self):
|
|
|
|
|
return self.members.all().distinct()
|
|
|
|
|
|
|
|
|
|
def add_member(self, user, role=None):
|
|
|
|
|
from rbac.builtin import BuiltinRole
|
|
|
|
|
from .utils import tmp_to_org
|
|
|
|
|
role_id = BuiltinRole.org_user.id
|
|
|
|
|
if role:
|
|
|
|
|
role_id = role.id
|
|
|
|
|
with tmp_to_org(self):
|
|
|
|
|
defaults = {
|
|
|
|
|
'user': user, 'role_id': role_id,
|
|
|
|
|
'org_id': self.id, 'scope': 'org'
|
|
|
|
|
}
|
|
|
|
|
self.members.through.objects.update_or_create(**defaults, defaults=defaults)
|
|
|
|
|
|
|
|
|
|
def get_origin_role_members(self, role_name):
|
|
|
|
|
from rbac.models import OrgRoleBinding
|
|
|
|
|
from users.models import User
|
|
|
|
|
from rbac.builtin import BuiltinRole
|
|
|
|
|
from .utils import tmp_to_org
|
|
|
|
|
|
|
|
|
|
role_mapper = {
|
|
|
|
|
'user': BuiltinRole.org_user,
|
|
|
|
|
'auditor': BuiltinRole.org_auditor,
|
|
|
|
|
'admin': BuiltinRole.org_admin
|
|
|
|
|
}
|
2022-03-16 09:53:06 +00:00
|
|
|
|
assert role_name in role_mapper
|
|
|
|
|
role = role_mapper.get(role_name).get_role()
|
2022-03-16 09:50:07 +00:00
|
|
|
|
with tmp_to_org(self):
|
|
|
|
|
org_admins = OrgRoleBinding.get_role_users(role)
|
|
|
|
|
return org_admins
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def admins(self):
|
|
|
|
|
from users.models import User
|
|
|
|
|
admins = self.get_origin_role_members('admin')
|
|
|
|
|
if not admins:
|
|
|
|
|
admins = User.objects.filter(username='admin')
|
|
|
|
|
return admins
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def auditors(self):
|
|
|
|
|
return self.get_origin_role_members('auditor')
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def users(self):
|
|
|
|
|
return self.get_origin_role_members('user')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Organization(OrgRoleMixin, models.Model):
|
2018-07-12 16:00:35 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2018-07-19 11:24:29 +00:00
|
|
|
|
name = models.CharField(max_length=128, unique=True, verbose_name=_("Name"))
|
2018-07-12 16:00:35 +00:00
|
|
|
|
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
|
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
|
2020-10-19 03:37:27 +00:00
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2022-02-18 09:28:45 +00:00
|
|
|
|
members = models.ManyToManyField(
|
|
|
|
|
'users.User', related_name='orgs', through='rbac.RoleBinding', through_fields=('org', 'user')
|
|
|
|
|
)
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2019-06-25 12:32:12 +00:00
|
|
|
|
ROOT_ID = '00000000-0000-0000-0000-000000000000'
|
2021-03-02 06:57:48 +00:00
|
|
|
|
ROOT_NAME = _('GLOBAL')
|
2021-03-16 12:17:13 +00:00
|
|
|
|
DEFAULT_ID = '00000000-0000-0000-0000-000000000002'
|
|
|
|
|
DEFAULT_NAME = 'Default'
|
2021-03-03 03:20:40 +00:00
|
|
|
|
orgs_mapping = None
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("Organization")
|
2022-02-17 12:13:31 +00:00
|
|
|
|
permissions = (
|
|
|
|
|
('view_rootorg', _('Can view root org')),
|
|
|
|
|
)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
|
def __str__(self):
|
2021-03-03 03:20:40 +00:00
|
|
|
|
return str(self.name)
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2021-03-03 03:20:40 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_instance(cls, id_or_name, default=None):
|
|
|
|
|
assert default is None or isinstance(default, cls), (
|
|
|
|
|
'`default` must be None or `Organization` instance'
|
|
|
|
|
)
|
|
|
|
|
org = cls.get_instance_from_memory(id_or_name)
|
|
|
|
|
org = org or default
|
|
|
|
|
return org
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2021-03-03 03:20:40 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_instance_from_memory(cls, id_or_name):
|
|
|
|
|
if not isinstance(cls.orgs_mapping, dict):
|
|
|
|
|
cls.orgs_mapping = cls.construct_orgs_mapping()
|
2021-03-15 03:38:08 +00:00
|
|
|
|
|
|
|
|
|
org = cls.orgs_mapping.get(str(id_or_name))
|
|
|
|
|
if not org:
|
|
|
|
|
# 内存失效速度慢于读取速度(on_org_create_or_update)
|
|
|
|
|
cls.orgs_mapping = cls.construct_orgs_mapping()
|
|
|
|
|
|
|
|
|
|
org = cls.orgs_mapping.get(str(id_or_name))
|
|
|
|
|
return org
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2021-03-03 03:20:40 +00:00
|
|
|
|
def construct_orgs_mapping(cls):
|
|
|
|
|
orgs_mapping = {}
|
|
|
|
|
for org in cls.objects.all():
|
|
|
|
|
orgs_mapping[str(org.id)] = org
|
|
|
|
|
orgs_mapping[str(org.name)] = org
|
|
|
|
|
root_org = cls.root()
|
|
|
|
|
orgs_mapping.update({
|
|
|
|
|
root_org.id: root_org,
|
|
|
|
|
'GLOBAL': root_org,
|
|
|
|
|
'全局组织': root_org
|
|
|
|
|
})
|
|
|
|
|
return orgs_mapping
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2021-03-03 03:20:40 +00:00
|
|
|
|
def expire_orgs_mapping(cls):
|
|
|
|
|
cls.orgs_mapping = None
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
def org_id(self):
|
2021-03-02 06:57:48 +00:00
|
|
|
|
return self.id
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def default(cls):
|
2021-03-03 03:20:40 +00:00
|
|
|
|
defaults = dict(id=cls.DEFAULT_ID, name=cls.DEFAULT_NAME)
|
2021-03-02 06:57:48 +00:00
|
|
|
|
obj, created = cls.objects.get_or_create(defaults=defaults, id=cls.DEFAULT_ID)
|
|
|
|
|
return obj
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def root(cls):
|
2021-04-08 06:18:53 +00:00
|
|
|
|
name = settings.GLOBAL_ORG_DISPLAY_NAME or cls.ROOT_NAME
|
|
|
|
|
return cls(id=cls.ROOT_ID, name=name)
|
2018-07-12 16:00:35 +00:00
|
|
|
|
|
2018-08-15 09:44:06 +00:00
|
|
|
|
def is_root(self):
|
2021-03-02 06:57:48 +00:00
|
|
|
|
return self.id == self.ROOT_ID
|
2018-08-15 09:44:06 +00:00
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
|
def is_default(self):
|
2021-03-02 06:57:48 +00:00
|
|
|
|
return str(self.id) == self.DEFAULT_ID
|
2019-01-15 02:23:30 +00:00
|
|
|
|
|
|
|
|
|
def change_to(self):
|
|
|
|
|
from .utils import set_current_org
|
|
|
|
|
set_current_org(self)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
2021-01-17 04:08:21 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def resource_statistics_cache(self):
|
|
|
|
|
from .caches import OrgResourceStatisticsCache
|
|
|
|
|
return OrgResourceStatisticsCache(self)
|
|
|
|
|
|
|
|
|
|
def get_total_resources_amount(self):
|
|
|
|
|
from django.apps import apps
|
|
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2022-02-17 12:13:31 +00:00
|
|
|
|
summary = {'users.Members': self.get_members().count()}
|
2021-01-17 04:08:21 +00:00
|
|
|
|
for app_name, app_config in apps.app_configs.items():
|
|
|
|
|
models_cls = app_config.get_models()
|
|
|
|
|
for model in models_cls:
|
|
|
|
|
if not issubclass(model, OrgModelMixin):
|
|
|
|
|
continue
|
|
|
|
|
key = '{}.{}'.format(app_name, model.__name__)
|
|
|
|
|
summary[key] = self.get_resource_amount(model)
|
|
|
|
|
return summary
|
|
|
|
|
|
|
|
|
|
def get_resource_amount(self, resource_model):
|
|
|
|
|
from .utils import tmp_to_org
|
|
|
|
|
from .mixins.models import OrgModelMixin
|
|
|
|
|
|
|
|
|
|
if not issubclass(resource_model, OrgModelMixin):
|
|
|
|
|
return 0
|
|
|
|
|
with tmp_to_org(self):
|
|
|
|
|
return resource_model.objects.all().count()
|
|
|
|
|
|
2021-08-30 08:48:46 +00:00
|
|
|
|
def as_tree_node(self, oid, pid, opened=True):
|
2021-07-27 08:06:00 +00:00
|
|
|
|
node = TreeNode(**{
|
2021-08-30 08:48:46 +00:00
|
|
|
|
'id': oid,
|
2021-07-27 08:06:00 +00:00
|
|
|
|
'name': self.name,
|
|
|
|
|
'title': self.name,
|
|
|
|
|
'pId': pid,
|
|
|
|
|
'open': opened,
|
|
|
|
|
'isParent': True,
|
|
|
|
|
'meta': {
|
|
|
|
|
'type': 'org'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return node
|
|
|
|
|
|
2022-02-18 09:28:45 +00:00
|
|
|
|
def delete_related_models(self):
|
|
|
|
|
from orgs.utils import tmp_to_root_org
|
|
|
|
|
from tickets.models import TicketFlow
|
|
|
|
|
with tmp_to_root_org():
|
|
|
|
|
TicketFlow.objects.filter(org_id=self.id).delete()
|
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
|
self.delete_related_models()
|
|
|
|
|
return super().delete(*args, **kwargs)
|
|
|
|
|
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
|
|
class OrganizationMember(models.Model):
|
|
|
|
|
"""
|
|
|
|
|
注意:直接调用该 `Model.delete` `Model.objects.delete` 不会触发清理该用户的信号
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-02-18 09:28:45 +00:00
|
|
|
|
org = models.ForeignKey(
|
|
|
|
|
Organization, related_name='m2m_org_members', on_delete=models.CASCADE, verbose_name=_('Organization')
|
|
|
|
|
)
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
|
'users.User', related_name='m2m_org_members', on_delete=models.CASCADE, verbose_name=_('User')
|
|
|
|
|
)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
role = models.CharField(max_length=16, default='User', verbose_name=_("Role"))
|
2020-07-20 02:42:22 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_("Date created"))
|
|
|
|
|
date_updated = models.DateTimeField(auto_now=True, verbose_name=_("Date updated"))
|
|
|
|
|
created_by = models.CharField(max_length=128, null=True, verbose_name=_('Created by'))
|
2022-02-18 09:28:45 +00:00
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
|
# objects = OrgMemberManager()
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
unique_together = [('org', 'user', 'role')]
|
|
|
|
|
db_table = 'orgs_organization_members'
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-02-17 12:13:31 +00:00
|
|
|
|
return '{} | {}'.format(self.user, self.org)
|