2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2022-09-21 10:19:17 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
2022-11-18 03:30:31 +00:00
|
|
|
from perms.models import AssetPermission
|
2022-06-23 05:52:28 +00:00
|
|
|
from tickets.models import ApplyAssetTicket
|
|
|
|
from .base import BaseHandler
|
|
|
|
|
|
|
|
|
|
|
|
class Handler(BaseHandler):
|
|
|
|
ticket: ApplyAssetTicket
|
|
|
|
|
|
|
|
def _on_step_approved(self, step):
|
|
|
|
is_finished = super()._on_step_approved(step)
|
|
|
|
if is_finished:
|
|
|
|
self._create_asset_permission()
|
|
|
|
|
|
|
|
def _create_asset_permission(self):
|
2022-07-15 03:38:50 +00:00
|
|
|
org_id = self.ticket.org_id
|
|
|
|
with tmp_to_org(org_id):
|
2022-06-23 05:52:28 +00:00
|
|
|
asset_permission = AssetPermission.objects.filter(id=self.ticket.id).first()
|
|
|
|
if asset_permission:
|
|
|
|
return asset_permission
|
|
|
|
|
2022-07-15 03:38:50 +00:00
|
|
|
apply_nodes = self.ticket.apply_nodes.all()
|
|
|
|
apply_assets = self.ticket.apply_assets.all()
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
apply_permission_name = self.ticket.apply_permission_name
|
|
|
|
apply_actions = self.ticket.apply_actions
|
2022-11-17 10:04:38 +00:00
|
|
|
apply_accounts = self.ticket.apply_accounts
|
2022-06-23 05:52:28 +00:00
|
|
|
apply_date_start = self.ticket.apply_date_start
|
|
|
|
apply_date_expired = self.ticket.apply_date_expired
|
|
|
|
permission_created_by = '{}:{}'.format(
|
|
|
|
str(self.ticket.__class__.__name__), str(self.ticket.id)
|
|
|
|
)
|
|
|
|
permission_comment = _(
|
|
|
|
'Created by the ticket '
|
|
|
|
'ticket title: {} '
|
|
|
|
'ticket applicant: {} '
|
|
|
|
'ticket processor: {} '
|
|
|
|
'ticket ID: {}'
|
|
|
|
).format(
|
|
|
|
self.ticket.title,
|
|
|
|
self.ticket.applicant,
|
|
|
|
','.join([i['processor_display'] for i in self.ticket.process_map]),
|
|
|
|
str(self.ticket.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
permission_data = {
|
|
|
|
'from_ticket': True,
|
2022-11-17 10:04:38 +00:00
|
|
|
'id': self.ticket.id,
|
2022-06-23 05:52:28 +00:00
|
|
|
'actions': apply_actions,
|
2022-11-17 10:04:38 +00:00
|
|
|
'accounts': apply_accounts,
|
|
|
|
'name': apply_permission_name,
|
2022-06-23 05:52:28 +00:00
|
|
|
'date_start': apply_date_start,
|
|
|
|
'date_expired': apply_date_expired,
|
2022-11-17 10:04:38 +00:00
|
|
|
'comment': str(permission_comment),
|
|
|
|
'created_by': permission_created_by,
|
2022-06-23 05:52:28 +00:00
|
|
|
}
|
|
|
|
with tmp_to_org(self.ticket.org_id):
|
|
|
|
asset_permission = AssetPermission.objects.create(**permission_data)
|
|
|
|
asset_permission.nodes.set(apply_nodes)
|
|
|
|
asset_permission.assets.set(apply_assets)
|
2022-11-17 10:04:38 +00:00
|
|
|
asset_permission.users.add(self.ticket.applicant)
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
return asset_permission
|