Skip to content

Commit

Permalink
Added Raw Query
Browse files Browse the repository at this point in the history
  • Loading branch information
luke- committed Jan 1, 2024
1 parent c414033 commit 7bbb32f
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/common/routes/routes.php
Expand Up @@ -46,6 +46,8 @@
Route::methods([Method::GET, Method::POST], '/server-config/refint/edit')->action([RefIntController::class, 'edit'])->name('server-config-refint-edit'),
Route::methods([Method::GET, Method::POST], '/server-config/memberof/edit')->action([\Balemy\LdapCommander\Modules\ServerConfig\MemberOf\Controller::class, 'edit'])->name('server-config-memberof-edit'),

Route::methods([Method::GET, Method::POST], '/raw-query')->action([Balemy\LdapCommander\Modules\RawQuery\RawQueryController::class, 'index'])->name('raw-query'),

//Route::methods([Method::GET, Method::POST], '/schema-editor/')->action([\Balemy\LdapCommander\Schema\Controller::class, 'list'])->name('schema-edit'),
),
Route::methods([Method::GET, Method::POST], '/login')->action([AuthController::class, 'login'])->name('login'),
Expand Down
28 changes: 28 additions & 0 deletions src/Modules/RawQuery/QueryForm.php
@@ -0,0 +1,28 @@
<?php

namespace Balemy\LdapCommander\Modules\RawQuery;

use Yiisoft\FormModel\FormModel;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;


class QueryForm extends FormModel implements RulesProviderInterface
{
public string $query = '';

/**
* @inheritDoc
*/
public function getRules(): iterable
{
$rules = [];
$rules['query'] = [new Required()];
return $rules;
}

public function getFormName(): string
{
return 'RawQueryForm';
}
}
62 changes: 62 additions & 0 deletions src/Modules/RawQuery/RawQueryController.php
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Balemy\LdapCommander\Modules\RawQuery;

use Balemy\LdapCommander\ApplicationParameters;
use Balemy\LdapCommander\Modules\Session\Session;
use Balemy\LdapCommander\Modules\UserManager\UserForm;
use Balemy\LdapCommander\Modules\UserManager\UserFormSchema;
use Balemy\LdapCommander\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Assets\AssetManager;
use Yiisoft\FormModel\FormHydrator;
use Yiisoft\Http\Method;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Session\Flash\FlashInterface;
use Yiisoft\Session\SessionInterface;
use Yiisoft\Validator\ValidatorInterface;
use Yiisoft\Yii\View\ViewRenderer;

final class RawQueryController
{
public function __construct(public ViewRenderer $viewRenderer,
public WebControllerService $webService,
public UrlGeneratorInterface $urlGenerator,
public SessionInterface $session,
public ValidatorInterface $validator,
public FormHydrator $formHydrator,
public AssetManager $assetManager,
public FlashInterface $flash,
public ApplicationParameters $applicationParameters,
)
{
$this->viewRenderer = $viewRenderer->withViewPath(__DIR__ . '/Views/');
}

/**
* @psalm-suppress PossiblyInvalidArgument
*/
public function index(ServerRequestInterface $request, FormHydrator $formHydrator): ResponseInterface
{
$queryForm = new QueryForm();
$results = [];

if ($request->getMethod() === Method::POST &&
$formHydrator->populate($queryForm, $request->getParsedBody()) && $this->validator->validate($queryForm)->isValid()) {

$session = Session::getCurrentSession();
$query = trim($queryForm->query);
$results = $session->lrConnection->query()->rawFilter($query)->select(['dn'])->paginate();
}

return $this->viewRenderer->render('index', [
'urlGenerator' => $this->urlGenerator,
'queryForm' => $queryForm,
'results' => $results
]);
}

}
75 changes: 75 additions & 0 deletions src/Modules/RawQuery/Views/index.php
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* @var \Yiisoft\View\WebView $this
* @var \Balemy\LdapCommander\ApplicationParameters $applicationParameters
* @var \Yiisoft\Router\UrlGeneratorInterface $urlGenerator
* @var \Yiisoft\Router\CurrentRoute $currentRoute
* @var Csrf $csrf
* @var \Balemy\LdapCommander\Modules\RawQuery\QueryForm $queryForm
* @var string[] $results
*/

use Yiisoft\FormModel\Field;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Form;
use Yiisoft\Yii\View\Csrf;

$this->setTitle($applicationParameters->getName());
?>

<div class="row">
<div class="col-md-9">
<h1>Execute LDAP Query</h1>
<p class="lead">
</p>

<br>

<?= Html::form()->post($urlGenerator->generate('raw-query'))->csrf($csrf)->open() ?>

<div class="row">
<div class="col-sm-12">
<?= Field::textarea($queryForm, 'query') ?>
</div>
</div>
<?= Field::submitButton()
->tabindex(3)
->content('Execute') ?>

<?= Form::tag()->close(); ?>


<?php if (!empty($results)): ?>
<br>
<br>
<table class="table">
<tr>
<th>DN</th>
<th>Object Class</th>
</tr>

<?php foreach ($results as $result): ?>
<?php
array_shift($result['objectclass']);
?>
<tr>
<td>
<?= Html::a(Html::encode($result['dn']), $urlGenerator->generate('entity-edit', [], ['dn' => $result['dn']])) ?>
</td>
<td><?= implode(', ', $result['objectclass']) ?></td>
</tr>
<?php endforeach; ?>

</table>


<?php endif; ?>

</div>


</div>

2 changes: 1 addition & 1 deletion src/Modules/Session/LoginForm.php
Expand Up @@ -60,7 +60,7 @@ private function passwordRules(): array
if ($configuredSession) {
try {
if (!$configuredSession->login($this->username, $this->password)) {
$result->addError("Login failed!");
$result->addError('Login failed!');
}
} catch (\Exception $ex) {
$result->addError("Login Error! " . $ex->getMessage());
Expand Down
12 changes: 11 additions & 1 deletion src/Views/layout/main.php
Expand Up @@ -102,6 +102,16 @@
]
],
*/
$menuItems[] = [
'label' => 'More',
'items' => [
[
'label' => 'Raw Query',
'url' => $urlGenerator->generate('raw-query'),
'active' => StringHelper::startsWith($currentRouteName, 'raw-query'),
],
]
];
$menuItems[] = [
'label' => 'Information',
'items' => [
Expand Down Expand Up @@ -140,7 +150,7 @@
<div class="d-flex">
<?= Nav::widget()
->items([[
'label' => 'Logout',
'label' => 'Logout (' . $session?->connectionDetails->dsn . ')',
'url' => $urlGenerator->generate('logout'),
]])->options([
'class' => 'navbar-nav ml-auto'
Expand Down

0 comments on commit 7bbb32f

Please sign in to comment.