Skip to content

Commit af111bd

Browse files
Prevent removal of machines with assigned services
Ensures machines cannot be removed if user services are still assigned, preventing orphaned user service records from counting against the limit and improving data consistency.
1 parent 20e9689 commit af111bd

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/uds/core/services/generics/fixed/service.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"""
2929
Author: Adolfo Gómez, dkmaster at dkmon dot com
3030
"""
31+
3132
import random # Not for cryptographic purposes, just to randomize the assignation of machines
3233
import abc
3334
import contextlib
@@ -147,9 +148,16 @@ def initialize(self, values: 'types.core.ValuesType') -> None:
147148
if not self.machines.value:
148149
raise exceptions.ui.ValidationError(gettext('We need at least a machine'))
149150

150-
# Remove machines not in values from "assigned" set
151+
# Do not allow removing machines that still have assigned user services.
152+
# (their UserService rows would linger, counting against userservices_limit)
151153
with self._assigned_access() as assigned_vms:
152-
assigned_vms &= set(self.machines.as_list())
154+
removed_assigned = assigned_vms - set(self.machines.as_list())
155+
if removed_assigned:
156+
raise exceptions.ui.ValidationError(
157+
gettext('Cannot remove machines with assigned services: {}').format(
158+
', '.join(self.get_name(m) or m for m in sorted(removed_assigned))
159+
)
160+
)
153161
self.token.value = self.token.value.strip()
154162

155163
@contextlib.contextmanager

0 commit comments

Comments
 (0)