Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

require_once __DIR__ . '/../../src/Plugin.php';

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
])
->withPhpVersion(PhpVersion::PHP_82)
->withCache(
cacheDirectory: __DIR__ . '/var/rector',
cacheClass: FileCacheStorage::class,
)
->withRootFiles()
->withParallel(timeoutSeconds: 300)
->withImportNames(removeUnusedImports: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
)
->withPhpSets(php82: true) // apply PHP sets up to PHP 8.2
;
2 changes: 1 addition & 1 deletion src/Controller/GetAuthLdapFilterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __invoke(Request $request): Response
: ''
;
return new JsonResponse([
'filter' => "(& $filter $ldap_condition)",
'filter' => sprintf('(& %s %s)', $filter, $ldap_condition),
]);
}
}
2 changes: 2 additions & 0 deletions src/Controller/LdapDropdownController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function __invoke(Request $request): Response
if ($condition_uuid === "") {
throw new BadRequestHttpException();
}

// We don't control this array
// @phpstan-ignore offsetAccess.nonOffsetAccessible
$condition = $_SESSION['glpicondition'][$condition_uuid] ?? null;
Expand All @@ -78,6 +79,7 @@ public function __invoke(Request $request): Response
if (!isset($condition[$fkey])) {
throw new BadRequestHttpException();
}

$question_id = $condition[$fkey];
$question = Question::getById($question_id);
if (!$question) {
Expand Down
20 changes: 11 additions & 9 deletions src/Model/Dropdown/LdapDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array
if (!$attribute) {
throw new RuntimeException();
}

$attribute = $attribute->fields['value'];
if (!is_string($attribute)) {
throw new LogicException();
Expand All @@ -142,7 +143,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array
}

// Insert search text into filter if specified
if ($search_text != '') {
if ($search_text !== '') {
$ldap_filter = sprintf(
"(& %s (%s))",
$config->getLdapFilter(),
Expand All @@ -154,7 +155,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array

try {
// Transform LDAP warnings into errors
set_error_handler([self::class, 'ldapErrorHandler'], E_WARNING);
set_error_handler(self::ldapErrorHandler(...), E_WARNING);

// Execute search
$ldap_values = $this->executeLdapSearch(
Expand All @@ -164,16 +165,14 @@ public function getDropdownValues(LdapDropdownQuery $query): array
$page_size,
$ldap_filter,
);
} catch (Throwable $e) {
throw new RuntimeException("Failed LDAP query", previous: $e);
} catch (Throwable $throwable) {
throw new RuntimeException("Failed LDAP query", $throwable->getCode(), previous: $throwable);
} finally {
restore_error_handler();
}

// Sort results
usort($ldap_values, function ($a, $b) {
return strnatcmp($a['text'], $b['text']);
});
usort($ldap_values, fn($a, $b) => strnatcmp($a['text'], $b['text']));

// Set expected select2 format
return [
Expand Down Expand Up @@ -226,6 +225,7 @@ private function executeLdapSearch(
if (!$result instanceof Result) {
throw new RuntimeException();
}

ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);

// PHPstan doens't know that this is safe
Expand Down Expand Up @@ -260,11 +260,12 @@ private function executeLdapSearch(
}

$found_count++;
if ($found_count < ((int) $page - 1) * (int) $page_size + 1) {
if ($found_count < ($page - 1) * $page_size + 1) {
// before the requested page
continue;
}
if ($found_count > ((int) $page) * (int) $page_size) {

if ($found_count > ($page) * $page_size) {
// after the requested page
break;
}
Expand All @@ -280,6 +281,7 @@ private function executeLdapSearch(
break;
}
}

// @phpstan-ignore notIdentical.alwaysTrue
} while ($cookie !== null && $cookie != '' && $count < $page_size);

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Dropdown/LdapDropdownQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

use Glpi\Form\Question;

final class LdapDropdownQuery
final readonly class LdapDropdownQuery
{
public function __construct(
private Question $question,
Expand Down
18 changes: 6 additions & 12 deletions src/Model/QuestionType/LdapQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use AuthLDAP;
use Glpi\Application\View\TemplateRenderer;
use Glpi\DBAL\JsonFieldInterface;
use Glpi\Form\Migration\FormQuestionDataConverterInterface;
use Glpi\Form\Question;
use Glpi\Form\QuestionType\AbstractQuestionType;
Expand Down Expand Up @@ -83,7 +84,7 @@ public function renderAdministrationTemplate(Question|null $question): string
{
// Read extra config specific to this question type
$decoded_extra_data = [];
if ($question !== null && is_string($question->fields['extra_data'])) {
if ($question instanceof Question && is_string($question->fields['extra_data'])) {
$decoded_extra_data = json_decode(
$question->fields['extra_data'],
associative: true,
Expand All @@ -94,8 +95,9 @@ public function renderAdministrationTemplate(Question|null $question): string
$decoded_extra_data = [];
}
}

$config = $this->getExtraDataConfig($decoded_extra_data);
if ($config === null) {
if (!$config instanceof JsonFieldInterface) {
$config = new LdapQuestionConfig();
}

Expand All @@ -119,15 +121,7 @@ public function renderAdministrationTemplate(Question|null $question): string
public function validateExtraDataInput(array $input): bool
{
// Check if the itemtype is set
if (
!isset($input[LdapQuestionConfig::AUTHLDAP_ID])
&& !isset($input[LdapQuestionConfig::LDAP_FILTER])
&& !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID])
) {
return false;
}

return true;
return !(!isset($input[LdapQuestionConfig::AUTHLDAP_ID]) && !isset($input[LdapQuestionConfig::LDAP_FILTER]) && !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID]));
}

#[Override]
Expand All @@ -139,7 +133,7 @@ public function getExtraDataConfigClass(): string
#[Override]
public function renderEndUserTemplate(Question|null $question): string
{
if ($question === null) {
if (!$question instanceof Question) {
throw new LogicException();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Model/QuestionType/LdapQuestionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
use Glpi\DBAL\JsonFieldInterface;
use Override;

final class LdapQuestionConfig implements JsonFieldInterface
final readonly class LdapQuestionConfig implements JsonFieldInterface
{
// Unique reference to hardcoded name used for serialization
public const AUTHLDAP_ID = "authldap_id";

public const LDAP_FILTER = "ldap_filter";

public const LDAP_ATTRIBUTE_ID = "ldap_attribute_id";

public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Service/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ public function getEnabledQuestionsTypes(): array

public function hasAtLeastOneQuestionTypeEnabled(): bool
{
return count($this->getEnabledQuestionsTypes()) > 0;
return $this->getEnabledQuestionsTypes() !== [];
}
}
1 change: 1 addition & 0 deletions src/Utils/SafeCommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function getForeignKeyField(string $class): string
if (!is_string($field)) {
throw new RuntimeException();
}

return $field;
}
}