false, 'error' => 'invalid_action']); } if ($action === 'delete') { Guard::requirePermissionOrForbidden(PermissionService::USERS_DELETE); } if ($action === 'send-access') { Guard::requirePermissionOrForbidden(PermissionService::USERS_UPDATE); } $raw = $_POST['uuids'] ?? ''; $uuids = []; if (is_array($raw)) { $uuids = $raw; } elseif (is_string($raw)) { $uuids = array_filter(array_map('trim', explode(',', $raw))); } $uuids = array_values(array_unique(array_filter($uuids))); if (!$uuids) { Router::json(['ok' => false, 'error' => 'no_selection']); } $currentUserId = (int) ($_SESSION['user']['id'] ?? 0); $userAccountService = (new UserServicesFactory())->createUserAccountService(); $mailService = (new MailServicesFactory())->createMailService(); if ($action === 'activate') { $result = $userAccountService->setActiveByUuids($uuids, true, $currentUserId); if (!($result['ok'] ?? false)) { Router::json(['ok' => false, 'error' => 'update_failed']); } Router::json(['ok' => true, 'count' => (int) ($result['count'] ?? 0)]); } if ($action === 'deactivate') { if ($currentUserId > 0) { $currentUser = $userAccountService->findById($currentUserId); $currentUuid = $currentUser['uuid'] ?? ''; if ($currentUuid !== '') { $uuids = array_values(array_filter($uuids, static fn ($uuid) => $uuid !== $currentUuid)); } if (!$uuids) { Router::json(['ok' => false, 'error' => 'self_deactivate']); } } $result = $userAccountService->setActiveByUuids($uuids, false, $currentUserId); if (!($result['ok'] ?? false)) { Router::json(['ok' => false, 'error' => 'update_failed']); } Router::json(['ok' => true, 'count' => (int) ($result['count'] ?? 0)]); } if ($action === 'delete') { $result = $userAccountService->deleteByUuids($uuids, $currentUserId); if (!($result['ok'] ?? false)) { Router::json(['ok' => false, 'error' => $result['error'] ?? 'delete_failed']); } Router::json(['ok' => true, 'count' => (int) ($result['count'] ?? 0)]); } $successCount = 0; $failedCount = 0; foreach ($uuids as $uuid) { $user = $userAccountService->findByUuid($uuid); if (!$user || empty($user['email'])) { $failedCount++; continue; } $context = UserAccessTemplateService::buildContext($user); $locale = (string) ($context['locale'] ?? ''); $subject = (string) ($context['subject'] ?? t('Your access details')); $vars = is_array($context['vars'] ?? null) ? $context['vars'] : []; $result = $mailService->sendTemplate('access_info', $vars, (string) $user['email'], $subject, $locale); if ($result['ok'] ?? false) { $successCount++; } else { $failedCount++; } } Router::json([ 'ok' => $successCount > 0, 'count' => $successCount, 'failed' => $failedCount, ]);