Skip to content

Commit de2acac

Browse files
[client] add new operation types for users background tasks (opencti #3343)
1 parent 4bd60e0 commit de2acac

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

pycti/utils/opencti_stix2.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,10 @@ def apply_patch(self, item):
25112511
self.opencti.notification.update_field(
25122512
id=item_id, input=field_patch_without_files
25132513
)
2514+
elif item["type"] == "user":
2515+
self.opencti.user.update_field(
2516+
id=item_id, input=field_patch_without_files
2517+
)
25142518
else:
25152519
self.opencti.stix_domain_object.update_field(
25162520
id=item_id, input=field_patch_without_files
@@ -2583,6 +2587,65 @@ def organization_unshare(self, item):
25832587
item["id"], organization_ids, sharing_direct_container
25842588
)
25852589

2590+
def element_add_organizations(self, item):
2591+
organization_ids = self.opencti.get_attribute_in_extension(
2592+
"organization_ids", item
2593+
)
2594+
if organization_ids is None:
2595+
organization_ids = item["organization_ids"]
2596+
if item["type"] == "user":
2597+
for organization_id in organization_ids:
2598+
self.opencti.user.add_organization(
2599+
id=item["id"], organization_id=organization_id
2600+
)
2601+
else:
2602+
raise ValueError(
2603+
"Add organizations operation not compatible with type",
2604+
{"type": item["type"]},
2605+
)
2606+
2607+
def element_remove_organizations(self, item):
2608+
organization_ids = self.opencti.get_attribute_in_extension(
2609+
"organization_ids", item
2610+
)
2611+
if organization_ids is None:
2612+
organization_ids = item["organization_ids"]
2613+
if item["type"] == "user":
2614+
for organization_id in organization_ids:
2615+
self.opencti.user.delete_organization(
2616+
id=item["id"], organization_id=organization_id
2617+
)
2618+
else:
2619+
raise ValueError(
2620+
"Remove organizations operation not compatible with type",
2621+
{"type": item["type"]},
2622+
)
2623+
2624+
def element_add_groups(self, item):
2625+
group_ids = self.opencti.get_attribute_in_extension("group_ids", item)
2626+
if group_ids is None:
2627+
group_ids = item["group_ids"]
2628+
if item["type"] == "user":
2629+
for group_id in group_ids:
2630+
self.opencti.user.add_membership(id=item["id"], group_id=group_id)
2631+
else:
2632+
raise ValueError(
2633+
"Add groups operation not compatible with type", {"type": item["type"]}
2634+
)
2635+
2636+
def element_remove_groups(self, item):
2637+
group_ids = self.opencti.get_attribute_in_extension("group_ids", item)
2638+
if group_ids is None:
2639+
group_ids = item["group_ids"]
2640+
if item["type"] == "user":
2641+
for group_id in group_ids:
2642+
self.opencti.user.delete_membership(id=item["id"], group_id=group_id)
2643+
else:
2644+
raise ValueError(
2645+
"Remove groups operation not compatible with type",
2646+
{"type": item["type"]},
2647+
)
2648+
25862649
def element_operation_delete(self, item, operation):
25872650
# If data is stix, just use the generic stix function for deletion
25882651
force_delete = operation == "delete_force"
@@ -2665,6 +2728,14 @@ def apply_opencti_operation(self, item, operation):
26652728
self.opencti.stix_core_object.ask_enrichments(
26662729
element_id=item["id"], connector_ids=connector_ids
26672730
)
2731+
elif operation == "add_organizations":
2732+
self.element_add_organizations(item)
2733+
elif operation == "remove_organizations":
2734+
self.element_remove_organizations(item)
2735+
elif operation == "add_groups":
2736+
self.element_add_groups(item)
2737+
elif operation == "remove_groups":
2738+
self.element_remove_groups(item)
26682739
else:
26692740
raise ValueError(
26702741
"Not supported opencti_operation",

0 commit comments

Comments
 (0)