103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\Access\PermissionService;
|
|
use MintyPHP\Service\Mail\MailServicesFactory;
|
|
use MintyPHP\Service\User\UserAccessTemplateService;
|
|
use MintyPHP\Service\User\UserServicesFactory;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
Guard::requireLogin();
|
|
|
|
$action = strtolower(trim((string) ($action ?? '')));
|
|
$allowedActions = ['activate', 'deactivate', 'delete', 'send-access'];
|
|
if (!in_array($action, $allowedActions, true)) {
|
|
Router::json(['ok' => 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,
|
|
]);
|