instances added god may help
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
<?php
|
||||
|
||||
use MintyPHP\Support\Guard;
|
||||
use MintyPHP\Router;
|
||||
use MintyPHP\Service\Access\PermissionService;
|
||||
use MintyPHP\Service\User\UserService;
|
||||
use MintyPHP\Service\Mail\MailServicesFactory;
|
||||
use MintyPHP\Service\User\UserAccessTemplateService;
|
||||
use MintyPHP\Service\Mail\MailService;
|
||||
use MintyPHP\Service\User\UserServicesFactory;
|
||||
use MintyPHP\Support\Guard;
|
||||
|
||||
Guard::requireLogin();
|
||||
|
||||
$action = strtolower(trim((string) ($action ?? '')));
|
||||
$handlers = [
|
||||
'activate' => 'handleActivate',
|
||||
'deactivate' => 'handleDeactivate',
|
||||
'delete' => 'handleDelete',
|
||||
'send-access' => 'handleSendAccess',
|
||||
];
|
||||
if (!isset($handlers[$action])) {
|
||||
$allowedActions = ['activate', 'deactivate', 'delete', 'send-access'];
|
||||
if (!in_array($action, $allowedActions, true)) {
|
||||
Router::json(['ok' => false, 'error' => 'invalid_action']);
|
||||
}
|
||||
if ($action === 'delete') {
|
||||
@@ -39,21 +34,20 @@ if (!$uuids) {
|
||||
}
|
||||
|
||||
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
||||
call_user_func($handlers[$action], $uuids, $currentUserId);
|
||||
$userAccountService = (new UserServicesFactory())->createUserAccountService();
|
||||
$mailService = (new MailServicesFactory())->createMailService();
|
||||
|
||||
function handleActivate(array $uuids, int $currentUserId): void
|
||||
{
|
||||
$result = UserService::setActiveByUuids($uuids, true, $currentUserId);
|
||||
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)]);
|
||||
}
|
||||
|
||||
function handleDeactivate(array $uuids, int $currentUserId): void
|
||||
{
|
||||
if ($action === 'deactivate') {
|
||||
if ($currentUserId > 0) {
|
||||
$currentUser = UserService::findById($currentUserId);
|
||||
$currentUser = $userAccountService->findById($currentUserId);
|
||||
$currentUuid = $currentUser['uuid'] ?? '';
|
||||
if ($currentUuid !== '') {
|
||||
$uuids = array_values(array_filter($uuids, static fn ($uuid) => $uuid !== $currentUuid));
|
||||
@@ -63,50 +57,46 @@ function handleDeactivate(array $uuids, int $currentUserId): void
|
||||
}
|
||||
}
|
||||
|
||||
$result = UserService::setActiveByUuids($uuids, false, $currentUserId);
|
||||
$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)]);
|
||||
}
|
||||
|
||||
function handleDelete(array $uuids, int $currentUserId): void
|
||||
{
|
||||
$result = UserService::deleteByUuids($uuids, $currentUserId);
|
||||
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)]);
|
||||
}
|
||||
|
||||
function handleSendAccess(array $uuids, int $currentUserId): void
|
||||
{
|
||||
$successCount = 0;
|
||||
$failedCount = 0;
|
||||
$successCount = 0;
|
||||
$failedCount = 0;
|
||||
|
||||
foreach ($uuids as $uuid) {
|
||||
$user = UserService::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++;
|
||||
}
|
||||
foreach ($uuids as $uuid) {
|
||||
$user = $userAccountService->findByUuid($uuid);
|
||||
if (!$user || empty($user['email'])) {
|
||||
$failedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Router::json([
|
||||
'ok' => $successCount > 0,
|
||||
'count' => $successCount,
|
||||
'failed' => $failedCount,
|
||||
]);
|
||||
$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,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user