From 38ee4d11d66fc839d5dfb364e65b1ea3472d36ce Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Mon, 2 Nov 2026 12:09:36 -0600 Subject: [PATCH] LDAP: add the five LDAPServers columns a 1.5 table lacks A server upgraded from 1.5 could not add or edit an LDAP server: save() failed with "Unknown column 'lsGroupNamAttr'" (forum topic 18300). Every released 1.5.10 LDAPServers table stops at lsEnableNestedGroup. createSql() is CREATE TABLE IF NOT EXISTS, so it leaves that table as it is, and no schema() step added lsGroupNamAttr, lsDisplayNameEnabled, lsDisplayNameAttr, lsIsLDAPs or lsAllowAPI. Reads hid the gap because getColumns() drops an absent column. save() names every declared column. Step 29 adds each column only when it is absent. Only then does it backfill the rows to the 1.5 behavior: group-name attribute 'name' (1.5 hard-coded (name=%s)), LDAPS for ports 636/686/3269/7636, display name and API sign-in off. An install that already has a column is unchanged. The new test pins the 1.5.10.2534 column list. It fails on the old code with exactly these five columns, and passes on this change. Co-Authored-By: Claude Opus 5.5 --- ldap/src/Managers/LDAPManager.php | 89 +++++++++++++ tests/ldap-upgrade-from-15-columns.test.php | 131 ++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 tests/ldap-upgrade-from-15-columns.test.php diff --git a/ldap/src/Managers/LDAPManager.php b/ldap/src/Managers/LDAPManager.php index 7431cd7..daa056b 100644 --- a/ldap/src/Managers/LDAPManager.php +++ b/ldap/src/Managers/LDAPManager.php @@ -965,7 +965,96 @@ function () { } return \FOG\Db\SchemaReconciler::applyConstraints('ldap'); }, + // 29 - the five columns a 1.5 table never had (forum 18300). + // CREATE TABLE IF NOT EXISTS left a 1.5 table as it was, and + // save() names every declared column, so each add or edit of a + // server failed with "Unknown column 'lsGroupNamAttr'". + function () { + return $this->_addMissing15Columns(); + }, + ]; + } + /** + * Adds the LDAPServers columns that a table created by 1.5 lacks, and + * backfills the existing rows to what 1.5 did without the column. + * + * A column that is already present is left alone, rows and all. + * + * @return bool|string true, or the database error + */ + private function _addMissing15Columns() + { + $columns = [ + // 1.5 hard-coded (name=%s) as the group-name filter. + 'lsGroupNamAttr' => [ + 'definition' => "VARCHAR(255) NOT NULL DEFAULT ''", + 'backfill' => "UPDATE `LDAPServers` SET `lsGroupNamAttr` = 'name'", + ], + // 1.5 had no display-name feature. + 'lsDisplayNameEnabled' => [ + 'definition' => 'TINYINT(1) NOT NULL DEFAULT 0', + 'backfill' => null, + ], + 'lsDisplayNameAttr' => [ + 'definition' => "VARCHAR(255) NOT NULL DEFAULT ''", + 'backfill' => null, + ], + // 1.5 chose LDAPS by port, with this exact list. + 'lsIsLDAPs' => [ + 'definition' => 'TINYINT(1) NOT NULL DEFAULT 0', + 'backfill' => 'UPDATE `LDAPServers` SET `lsIsLDAPs` = 1 ' + . 'WHERE `lsPort` IN (636, 686, 3269, 7636)', + ], + // 1.5 had no API sign-in through LDAP; keep it off. + 'lsAllowAPI' => [ + 'definition' => 'TINYINT(1) NOT NULL DEFAULT 0', + 'backfill' => null, + ], ]; + $rows = self::$DB->query( + "SELECT `COLUMN_NAME` AS `c` " + . "FROM `information_schema`.`COLUMNS` " + . "WHERE `TABLE_SCHEMA` = DATABASE() " + . "AND LOWER(`TABLE_NAME`) = 'ldapservers'" + )->fetch(\PDO::FETCH_ASSOC, 'fetch_all')->get(); + if (self::$DB->error) { + // A failed read must not record the step as applied: it would + // never run again. + return self::$DB->error; + } + $have = array_map( + function ($row) { + return strtolower((string)($row['c'] ?? '')); + }, + (array)$rows + ); + if (!count($have)) { + // Step 0 creates the table, so this is not expected. Change + // nothing rather than guess. + return true; + } + foreach ($columns as $column => $spec) { + if (in_array(strtolower($column), $have, true)) { + continue; + } + $statements = [ + sprintf( + 'ALTER TABLE `LDAPServers` ADD COLUMN `%s` %s', + $column, + $spec['definition'] + ), + ]; + if ($spec['backfill']) { + $statements[] = $spec['backfill']; + } + foreach ($statements as $sql) { + self::$DB->query($sql); + if (self::$DB->error) { + return self::$DB->error; + } + } + } + return true; } /** * Installs the plugin database non-destructively (create-if-absent + diff --git a/tests/ldap-upgrade-from-15-columns.test.php b/tests/ldap-upgrade-from-15-columns.test.php new file mode 100644 index 0000000..7d30227 --- /dev/null +++ b/tests/ldap-upgrade-from-15-columns.test.php @@ -0,0 +1,131 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ + +$root = dirname(__DIR__); +$file = $root . '/ldap/src/Managers/LDAPManager.php'; +$failures = []; + +// LDAPServers as 1.5.10.2534's LDAPManager::install() created it. +$released15 = [ + 'lsID', 'lsName', 'lsDesc', 'lsCreatedBy', 'lsAddress', 'lsCreatedTime', + 'lsUserSearchDN', 'lsPort', 'lsUserNamAttr', 'lsGrpMemberAttr', + 'lsAdminGroup', 'lsUserGroup', 'lsSearchScope', 'lsBindDN', 'lsBindPwd', + 'lsGrpSearchDN', 'lsUseGroupMatch', 'lsUserFilter', 'lsEnableNestedGroup', +]; + +/** + * Lifts one method's body out of the source by matching braces. + * + * @param string $src the source, comments already stripped + * @param string $name the method name + * + * @return string + */ +function luMethodBody($src, $name) +{ + $at = strpos($src, 'function ' . $name . '('); + if (false === $at) { + return ''; + } + $open = strpos($src, '{', $at); + $depth = 0; + for ($i = $open, $n = strlen($src); $i < $n; $i++) { + if ($src[$i] === '{') { + $depth++; + } + if ($src[$i] === '}' && --$depth === 0) { + return substr($src, $open, $i - $open + 1); + } + } + + return ''; +} + +// Strip comments, so a column named only in prose counts for nothing. +$src = ''; +foreach (token_get_all(file_get_contents($file)) as $token) { + if (is_array($token) && in_array($token[0], [T_COMMENT, T_DOC_COMMENT], true)) { + continue; + } + $src .= is_array($token) ? $token[1] : $token; +} + +// createSql()'s column list: the first [...] after the "true," argument. +$create = luMethodBody($src, 'createSql'); +if (!preg_match('/true\s*,\s*\[(.*?)\]/s', $create, $m)) { + fwrite(STDERR, "FAIL: could not read the column list in createSql()\n"); + exit(1); +} +preg_match_all("/'(ls[A-Za-z]+)'/", $m[1], $mm); +$declared = $mm[1]; +if (count($declared) < count($released15)) { + fwrite(STDERR, "FAIL: createSql() lists fewer columns than 1.5 had\n"); + exit(1); +} + +$schema = luMethodBody($src, 'schema'); +$helper = luMethodBody($src, '_addMissing15Columns'); +$helperRuns = $helper !== '' + && false !== strpos($schema, '_addMissing15Columns()'); + +$new = array_diff($declared, $released15); +foreach ($new as $column) { + $byAlter = (bool)preg_match( + '/ADD COLUMN `' . preg_quote($column, '/') . '`/', + $schema + ); + $byHelper = $helperRuns && (bool)preg_match( + "/'" . preg_quote($column, '/') . "'\s*=>/", + $helper + ); + if (!$byAlter && !$byHelper) { + $failures[] = sprintf( + '%s is in createSql() but no schema() step adds it to a table ' + . 'created by 1.5, so save() fails there with Unknown column', + $column + ); + } +} + +if ($failures) { + foreach ($failures as $f) { + fwrite(STDERR, "FAIL: $f\n"); + } + exit(1); +} +printf( + "PASS: %d LDAPServers columns added since 1.5 all reach an upgraded table\n", + count($new) +); +exit(0);