daUser->username(); $prefix = $ctx->daUser->prefix(); $dbNames = array_map(static fn(array $db): string => $db['name'], $ctx->mysql->listDatabasesForUser($daUser)); $roleNames = array_map(static fn(array $r): string => $r['name'], $ctx->mysql->listRolesForUser($daUser)); if ($ctx->isPost()) { try { $ctx->requireCsrf('assign_database_submit'); $dbName = NamePolicy::normalizeDatabaseName($ctx->postString('db_name'), $prefix); $roleName = NamePolicy::normalizeRoleName($ctx->postString('role_name'), $prefix); if (!in_array($dbName, $dbNames, true)) { throw new RuntimeException('Baza nie należy do bieżącego użytkownika DA: ' . $dbName); } if (!in_array($roleName, $roleNames, true)) { throw new RuntimeException('Użytkownik MySQL nie należy do bieżącego konta DA: ' . $roleName); } $privileges = NamePolicy::sanitizePrivileges($ctx->postArray('privileges')); if (empty($privileges)) { $privileges = NamePolicy::defaultPrivileges(); } $ctx->mysql->grantPrivileges($dbName, $roleName, $privileges); $hostEntries = $ctx->mysql->getRoleHostEntries($daUser, $roleName); $hasLocalhost = false; foreach ($hostEntries as $entry) { if ($entry['host'] === 'localhost') { $hasLocalhost = true; break; } } if (!$hasLocalhost) { $hostEntries[] = ['host' => 'localhost', 'note' => '']; $ctx->mysql->replaceRoleHostsWithNotes($daUser, $roleName, $hostEntries); } $sync = $ctx->mysql->syncHostRules(); if (!$sync['ok']) { $errors[] = 'Przypisanie wykonane, ale synchronizacja hostów MySQL nie powiodła się: ' . $sync['output']; } $ok[] = 'Przypisano bazę ' . $dbName . ' do użytkownika ' . $roleName . '.'; } catch (Throwable $e) { $errors[] = $e->getMessage(); } } $dbOptions = ''; foreach ($dbNames as $dbName) { $dbOptions .= ''; } $roleOptions = ''; foreach ($roleNames as $roleName) { $roleOptions .= ''; } $privCheckboxes = ''; foreach (NamePolicy::PRIVILEGE_LABELS as $code => $label) { $checked = ($code === 'ALL') ? ' checked' : ''; $privCheckboxes .= ''; } $body = ''; $body .= '
'; $body .= $ctx->csrfField('assign_database_submit'); $body .= '
'; $body .= '
'; $body .= '
'; $body .= '
'; $body .= '

Uprawnienia

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