mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
fix: API saves refuse null and an empty list for a NOT NULL column instead of a 500 (#8496)
This commit is contained in:
1 parent
949483246f
commit
65730848df
6 files changed
+108
-1
No files matched your search
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
// AppModel::beforeValidate() must refuse null and an empty list for a NOT NULL column. Cake saves
|
||||
// both as NULL, and MySQL answers "Column 'Definition' cannot be null", a 500, so it has to be a
|
||||
// field error instead. A nullable column still takes them as NULL; the primary key, created,
|
||||
// modified, updated, a NOT NULL timestamp (which a server may fill in) and a column the model's
|
||||
// beforeSave() sets itself (serverSetColumns, as Zone does for NumCoords and Area) are left alone.
|
||||
// The Cake model is stubbed, so this runs from a source checkout:
|
||||
// php tests/php/test_api_model_null_not_null.php
|
||||
|
||||
class App { public static function uses($class, $package) {} }
|
||||
class Model {
|
||||
public $alias = 'Thing';
|
||||
public $primaryKey = 'Id';
|
||||
public $data = array();
|
||||
public $invalid = array();
|
||||
public function schema() {
|
||||
return array(
|
||||
'Id' => array('type' => 'integer', 'null' => false),
|
||||
'Height' => array('type' => 'integer', 'null' => false),
|
||||
'Width' => array('type' => 'integer', 'null' => true),
|
||||
'Seen' => array('type' => 'datetime', 'null' => false),
|
||||
'Ended' => array('type' => 'datetime', 'null' => true),
|
||||
'Stamp' => array('type' => 'timestamp', 'null' => false),
|
||||
'Method' => array('type' => "enum('Local','Remote')", 'null' => false),
|
||||
'Name' => array('type' => 'string', 'length' => 5, 'null' => false),
|
||||
'Alias' => array('type' => 'string', 'length' => 5, 'null' => true),
|
||||
'Notes' => array('type' => 'text', 'null' => false),
|
||||
'Enabled' => array('type' => 'boolean', 'null' => false),
|
||||
'Derived' => array('type' => 'integer', 'null' => false),
|
||||
'created' => array('type' => 'datetime', 'null' => false),
|
||||
'modified' => array('type' => 'datetime', 'null' => false),
|
||||
);
|
||||
}
|
||||
public function invalidate($field, $message) { $this->invalid[$field] = $message; }
|
||||
}
|
||||
require_once __DIR__.'/../../web/api/app/Model/AppModel.php';
|
||||
class Computed extends AppModel { public $serverSetColumns = array('Derived'); }
|
||||
|
||||
$passes = 0;
|
||||
$failures = 0;
|
||||
function check($name, $got, $expected) {
|
||||
global $passes, $failures;
|
||||
if ($got === $expected) { $passes++; echo "ok - $name\n"; }
|
||||
else { $failures++; echo "not ok - $name (got ".var_export($got, true)." expected ".var_export($expected, true).")\n"; }
|
||||
}
|
||||
|
||||
function accepted($field, $value, $class = 'AppModel') {
|
||||
$m = new $class();
|
||||
$m->data = array('Thing' => array($field => $value));
|
||||
$m->beforeValidate();
|
||||
return !isset($m->invalid[$field]);
|
||||
}
|
||||
|
||||
foreach (array('Height', 'Seen', 'Method', 'Name', 'Notes', 'Enabled') as $field) {
|
||||
check("null $field (NOT NULL) is refused", accepted($field, null), false);
|
||||
check("empty list $field (NOT NULL) is refused", accepted($field, array()), false);
|
||||
}
|
||||
foreach (array('Width', 'Ended', 'Alias') as $field) {
|
||||
check("null $field (nullable) is accepted", accepted($field, null), true);
|
||||
check("empty list $field (nullable) is accepted", accepted($field, array()), true);
|
||||
}
|
||||
foreach (array('Id', 'created', 'modified', 'Stamp') as $field) {
|
||||
check("null $field (key, Cake date or timestamp) is accepted", accepted($field, null), true);
|
||||
check("empty list $field (key, Cake date or timestamp) is accepted", accepted($field, array()), true);
|
||||
}
|
||||
check('null Derived is refused when the model does not set it', accepted('Derived', null), false);
|
||||
check('null Derived is accepted when the model sets it in beforeSave', accepted('Derived', null, 'Computed'), true);
|
||||
check('empty list Derived is accepted when the model sets it in beforeSave', accepted('Derived', array(), 'Computed'), true);
|
||||
check('Height null is still refused on that model', accepted('Height', null, 'Computed'), false);
|
||||
check('Height 0 is accepted', accepted('Height', 0), true);
|
||||
check('Name "" is still accepted (text)', accepted('Name', ''), true);
|
||||
check('Height as a non-empty list is still refused', accepted('Height', array(1)), false);
|
||||
|
||||
$m = new AppModel();
|
||||
$m->data = array('Thing' => array('Name' => null, 'Height' => 5));
|
||||
$m->beforeValidate();
|
||||
check('only the null NOT NULL field is flagged', array_keys($m->invalid), array('Name'));
|
||||
|
||||
echo "\n$passes passed, $failures failed\n";
|
||||
exit($failures ? 1 : 0);
|
||||
?>
|
||||
@@ -31,18 +31,32 @@ App::uses('Model', 'Model');
|
||||
*/
|
||||
class AppModel extends Model {
|
||||
|
||||
// Columns the model's beforeSave() sets itself, so a null or empty list sent for one is not refused.
|
||||
public $serverSetColumns = array();
|
||||
|
||||
// Check numbers, dates, enums and string lengths against the schema so bad input is a validation error, not a 500.
|
||||
public function beforeValidate($options = array()) {
|
||||
if (empty($this->data[$this->alias])) return true;
|
||||
$bits = array('tinyinteger' => 8, 'smallinteger' => 16, 'integer' => 32);
|
||||
foreach ($this->schema() as $field => $column) {
|
||||
if (!isset($this->data[$this->alias][$field])) continue;
|
||||
if (!is_array($this->data[$this->alias]) or !array_key_exists($field, $this->data[$this->alias])) continue;
|
||||
$value = $this->data[$this->alias][$field];
|
||||
if (is_array($value) and $value) {
|
||||
// Cake would put the word Array in the insert and MySQL would answer with an unknown column.
|
||||
$this->invalidate($field, $field.' must be a single value');
|
||||
continue;
|
||||
}
|
||||
if ($value === null or $value === array()) {
|
||||
// Cake saves null and an empty list as NULL, which a NOT NULL column refuses with a 500.
|
||||
// Cake drops an empty primary key and empty created/modified/updated before the insert, and a
|
||||
// server may fill a NOT NULL timestamp with the current time.
|
||||
if (isset($column['null']) and !$column['null'] and $column['type'] != 'timestamp' and
|
||||
$field != $this->primaryKey and !in_array($field, array('created', 'modified', 'updated')) and
|
||||
!in_array($field, $this->serverSetColumns)) {
|
||||
$this->invalidate($field, $field.' must not be empty');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!is_scalar($value)) continue;
|
||||
$type = $column['type'];
|
||||
if ($value === '') {
|
||||
|
||||
@@ -10,6 +10,9 @@ App::uses('AppModel', 'Model');
|
||||
*/
|
||||
class Event extends AppModel {
|
||||
|
||||
// Set by beforeSave(), so a null sent for them is not refused.
|
||||
public $serverSetColumns = array('StateId');
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
|
||||
@@ -8,6 +8,9 @@ App::uses('AppModel', 'Model');
|
||||
*/
|
||||
class Snapshot extends AppModel {
|
||||
|
||||
// Set by beforeSave(), so a null sent for them is not refused.
|
||||
public $serverSetColumns = array('CreatedBy', 'CreatedOn');
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
|
||||
@@ -6,6 +6,9 @@ App::uses('AppModel', 'Model');
|
||||
*/
|
||||
class User extends AppModel {
|
||||
|
||||
// Set by beforeSave(), so a null sent for them is not refused.
|
||||
public $serverSetColumns = array('TokenMinExpiry');
|
||||
|
||||
public $validate = array(
|
||||
'Username' => array(
|
||||
'required' => array(
|
||||
|
||||
@@ -7,6 +7,9 @@ App::uses('AppModel', 'Model');
|
||||
*/
|
||||
class Zone extends AppModel {
|
||||
|
||||
// Set by beforeSave(), so a null sent for them is not refused.
|
||||
public $serverSetColumns = array('NumCoords', 'Area');
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user