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);