65c1b5fb75
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace KTXC\Controllers;
|
|
|
|
use KTXC\Http\Request\Request;
|
|
use KTXC\Http\Response\JsonResponse;
|
|
use KTXC\L10N\LocaleResolver;
|
|
use KTXC\Module\ModuleManager;
|
|
use KTXC\Security\Authorization\PermissionChecker;
|
|
use KTXC\Service\UserAccountsService;
|
|
use KTXC\Context\IdentityContextInterface;
|
|
use KTXF\Controller\ControllerAbstract;
|
|
use KTXC\Context\TenantContextInterface;
|
|
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
|
|
class InitController extends ControllerAbstract
|
|
{
|
|
public function __construct(
|
|
private readonly TenantContextInterface $tenantContext,
|
|
private readonly IdentityContextInterface $identityContext,
|
|
private readonly ModuleManager $moduleManager,
|
|
private readonly UserAccountsService $userService,
|
|
private readonly PermissionChecker $permissionChecker,
|
|
private readonly LocaleResolver $localeResolver,
|
|
) {}
|
|
|
|
#[AuthenticatedRoute('/init', name: 'init', methods: ['GET'])]
|
|
public function index(Request $request): JsonResponse {
|
|
|
|
$configuration = [];
|
|
|
|
// modules - filter by permissions
|
|
$configuration['modules'] = [];
|
|
foreach ($this->moduleManager->list(true, true) as $module) {
|
|
// Check if user has permission to view this module
|
|
// Allow access if user has: {module_handle}, {module_handle}.*, or * permission
|
|
$handle = $module->handle();
|
|
if (!$this->hasModuleViewPermission($handle)) {
|
|
continue;
|
|
}
|
|
|
|
$integrations = $module->registerBI();
|
|
if ($integrations !== null) {
|
|
$configuration['modules'][$handle] = $integrations;
|
|
}
|
|
}
|
|
|
|
// localization
|
|
$configuration['l10n'] = [
|
|
'locale' => $this->localeResolver->resolve($request),
|
|
'fallback' => LocaleResolver::FALLBACK,
|
|
'available' => $this->localeResolver->available(),
|
|
];
|
|
|
|
// tenant
|
|
$configuration['tenant'] = [
|
|
'id' => $this->tenantContext->identifier(),
|
|
'domain' => $this->tenantContext->domain(),
|
|
'label' => $this->tenantContext->label(),
|
|
];
|
|
|
|
// user
|
|
$configuration['user'] = [
|
|
'auth' => [
|
|
'identifier' => $this->identityContext->identifier(),
|
|
'identity' => $this->identityContext->identity()->getIdentity(),
|
|
'label' => $this->identityContext->label(),
|
|
'roles' => $this->identityContext->identity()->getRoles(),
|
|
'permissions' => $this->identityContext->identity()->getPermissions(),
|
|
],
|
|
'profile' => $this->userService->getEditableFields($this->identityContext->identifier()),
|
|
'settings' => $this->userService->fetchSettings([], true),
|
|
];
|
|
|
|
return new JsonResponse($configuration);
|
|
|
|
}
|
|
|
|
/**
|
|
* Check if user has permission to view a module
|
|
*
|
|
* Checks for the following permissions (in order):
|
|
* 1. {module_handle} - module access permission
|
|
* 2. {module_handle}.* - wildcard for the module
|
|
* 3. * - global wildcard
|
|
*
|
|
* @param string $moduleHandle The module handle to check
|
|
* @return bool
|
|
*/
|
|
private function hasModuleViewPermission(string $moduleHandle): bool
|
|
{
|
|
// Core module is always accessible to authenticated users
|
|
if ($moduleHandle === 'core') {
|
|
return true;
|
|
}
|
|
|
|
// Check for specific module permission or wildcard permissions
|
|
return $this->permissionChecker->canAny([
|
|
"{$moduleHandle}",
|
|
"{$moduleHandle}.*",
|
|
]);
|
|
}
|
|
|
|
}
|