daUser->username(); $prefix = $ctx->daUser->prefix(); $showRemoteHosts = $ctx->settings->allowRemoteHosts(); $databases = $ctx->pg->listDatabasesForUser($daUser); $dbNames = array_map(static fn(array $db): string => $db['name'], $databases); if ($ctx->isPost()) { try { $ctx->requireCsrf('create_user_submit'); $role = NamePolicy::normalizeRoleName($ctx->postString('role_name'), $prefix); if ($ctx->pg->roleExists($role)) { throw new RuntimeException('Użytkownik PostgreSQL już istnieje: ' . $role); } $password = $ctx->postString('password'); if (strlen($password) < 12) { throw new RuntimeException('Hasło użytkownika musi mieć co najmniej 12 znaków.'); } $selectedDbsRaw = $ctx->postArray('databases'); $selectedDbs = []; foreach ($selectedDbsRaw as $db) { $normalizedDb = NamePolicy::normalizeDatabaseName($db, $prefix); if (!in_array($normalizedDb, $dbNames, true)) { throw new RuntimeException('Nieprawidłowa baza do przypisania: ' . $normalizedDb); } $selectedDbs[] = $normalizedDb; } $selectedDbs = array_values(array_unique($selectedDbs)); $privileges = NamePolicy::sanitizePrivileges($ctx->postArray('privileges')); if (empty($privileges)) { $privileges = NamePolicy::defaultPrivileges(); } $globalHostEntries = [['host' => 'localhost', 'note' => '']]; $remoteHostEntry = null; if ($showRemoteHosts) { $remoteHostRaw = trim($ctx->postString('remote_host')); if ($remoteHostRaw !== '') { $remoteHost = NamePolicy::normalizeRemoteIpv4AccessPattern($remoteHostRaw); $remoteComment = NamePolicy::normalizeHostComment($ctx->postString('remote_comment')); $remoteHostEntry = ['host' => $remoteHost, 'note' => $remoteComment]; } } if ($remoteHostEntry !== null && empty($selectedDbs)) { throw new RuntimeException('Host zdalny można dodać tylko dla wybranej bazy.'); } $ctx->pg->createRole($role, $password); try { $ctx->pg->replaceRoleHostsWithNotes($daUser, $role, $globalHostEntries); foreach ($selectedDbs as $dbName) { $ctx->pg->grantPrivileges($dbName, $role, $privileges); if ($remoteHostEntry !== null) { $ctx->pg->replaceRoleHostsWithNotes($daUser, $role, [$remoteHostEntry], $dbName); } } $sync = $ctx->pg->syncHbaRules(); if (!$sync['ok']) { $errors[] = 'Użytkownik został utworzony, ale synchronizacja pg_hba nie powiodła się: ' . $sync['output']; } $ok[] = 'Utworzono użytkownika ' . $role . ' i przypisano ' . count($selectedDbs) . ' baz(y).'; } catch (Throwable $inner) { try { foreach ($selectedDbs as $dbName) { $ctx->pg->revokePrivileges($dbName, $role); } $ctx->pg->deleteRoleHosts($daUser, $role); $ctx->pg->dropRole($role); } catch (Throwable $ignored) { } throw $inner; } } catch (Throwable $e) { $errors[] = $e->getMessage(); } } $dbCheckboxes = ''; foreach ($dbNames as $dbName) { $dbCheckboxes .= ''; } if ($dbCheckboxes === '') { $dbCheckboxes = '

Brak baz do przypisania.

'; } $privCheckboxes = ''; foreach (NamePolicy::PRIVILEGE_LABELS as $code => $label) { $checked = ($code === 'ALL') ? ' checked' : ''; $privCheckboxes .= ''; } $remoteFields = ''; if ($showRemoteHosts) { $remoteFields .= '
'; $remoteFields .= '
'; $remoteFields .= '
'; $remoteFields .= '
'; } else { $remoteFields .= '

Hosty zdalne są wyłączone w konfiguracji pluginu (`allow_remote_hosts=0`).

'; } $body = ''; $body .= '
'; $body .= $ctx->csrfField('create_user_submit'); $body .= '
'; $body .= '
'; $body .= '
'; $body .= '
'; $body .= '
'; $body .= '

`localhost` jest dodawany automatycznie.

'; $body .= '
' . $dbCheckboxes . '
'; $body .= '
'; $body .= $remoteFields; $body .= '

Uprawnienia dla przypisywanych baz

' . $privCheckboxes . '
'; $body .= '
'; $body .= '
'; $ctx->renderPage('Tworzenie Użytkownika PostgreSQL', $body, $ok, $errors); };