2018-07-12 16:00:35 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.tree import TreeNode
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.utils import lazyproperty, settings, get_logger
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
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
|
2022-07-21 07:31:56 +00:00
|
|
|
|
2022-03-16 09:50:07 +00:00
|
|
|
if role:
|
|
|
|
role_id = role.id
|
2022-07-21 07:31:56 +00:00
|
|
|
elif user.is_service_account:
|
|
|
|
role_id = BuiltinRole.system_component.id
|
|
|
|
else:
|
|
|
|
role_id = BuiltinRole.org_user.id
|
|
|
|
|
2022-03-16 09:50:07 +00:00
|
|
|
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 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')
|
|
|
|
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class Organization(OrgRoleMixin, JMSBaseModel):
|
2018-07-19 11:24:29 +00:00
|
|
|
name = models.CharField(max_length=128, unique=True, verbose_name=_("Name"))
|
2022-10-26 09:21:52 +00:00
|
|
|
builtin = models.BooleanField(default=False, verbose_name=_('Builtin'))
|
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'
|
2022-10-10 12:56:13 +00:00
|
|
|
DEFAULT_NAME = _('DEFAULT')
|
|
|
|
SYSTEM_ID = '00000000-0000-0000-0000-000000000004'
|
|
|
|
SYSTEM_NAME = _('SYSTEM')
|
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')),
|
2022-07-18 05:44:20 +00:00
|
|
|
('view_alljoinedorg', _('Can view all joined org')),
|
2022-02-17 12:13:31 +00:00
|
|
|
)
|
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)
|
2022-10-27 08:49:22 +00:00
|
|
|
obj, created = cls.objects.get_or_create(defaults=defaults, id=cls.DEFAULT_ID)
|
|
|
|
if not obj.builtin:
|
|
|
|
obj.builtin = True
|
|
|
|
obj.save()
|
2021-03-02 06:57:48 +00:00
|
|
|
return obj
|
2018-07-12 16:00:35 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@classmethod
|
|
|
|
def system(cls):
|
|
|
|
defaults = dict(id=cls.SYSTEM_ID, name=cls.SYSTEM_NAME)
|
|
|
|
obj, created = cls.objects.get_or_create(defaults=defaults, id=cls.SYSTEM_ID)
|
|
|
|
if not obj.builtin:
|
|
|
|
obj.builtin = True
|
|
|
|
obj.save()
|
|
|
|
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
|
2022-10-26 09:21:52 +00:00
|
|
|
return cls(id=cls.ROOT_ID, name=name, builtin=True)
|
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)
|