feat: add validators to check values in services
Signed-off-by: Luka Trovic <luka@nextcloud.com>
This commit is contained in:
committed by
backportbot-nextcloud[bot]
parent
f250d9956b
commit
9d09916c17
37
lib/Validators/AssignmentServiceValidator.php
Normal file
37
lib/Validators/AssignmentServiceValidator.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class AssignmentServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'cardId' => ['numeric'],
|
||||
'userId' => ['not_empty', 'not_null', 'not_false', 'max:64'],
|
||||
];
|
||||
}
|
||||
}
|
||||
37
lib/Validators/AttachmentServiceValidator.php
Normal file
37
lib/Validators/AttachmentServiceValidator.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class AttachmentServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'cardId' => ['numeric'],
|
||||
'type' => ['not_empty', 'not_null', 'not_false'],
|
||||
'data' => ['not_empty', 'not_null', 'not_false', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
182
lib/Validators/BaseValidator.php
Normal file
182
lib/Validators/BaseValidator.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
use Exception;
|
||||
use OCA\Deck\BadRequestException;
|
||||
|
||||
abstract class BaseValidator {
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
abstract public function rules();
|
||||
|
||||
/**
|
||||
* Validate given entries
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
private function validate($data) {
|
||||
$rules = $this->rules();
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$field_rule = $rules[$field];
|
||||
|
||||
if (is_array($field_rule)) {
|
||||
foreach ($field_rule as $rule) {
|
||||
// The format for specifying validation rules and parameters follows an
|
||||
// easy {rule}:{parameters} formatting convention. For instance the
|
||||
// rule "Max:3" states that the value may only be three letters.
|
||||
if (strpos($rule, ':') !== false) {
|
||||
[$rule, $parameter] = explode(':', $rule, 2);
|
||||
if (!$this->{$rule}($value, $parameter)) {
|
||||
throw new BadRequestException(
|
||||
$this->getErrorMessage($rule, $field, $parameter));
|
||||
}
|
||||
} else {
|
||||
if (!$this->{$rule}($value)) {
|
||||
throw new BadRequestException(
|
||||
$field . ' must be provided and must be '. str_replace("_", " ", $rule));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_callable($field_rule) && !$field_rule($value)) {
|
||||
throw new BadRequestException($field . ' must be provided');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return void
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function check(array $data) {
|
||||
$this->validate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
private function numeric($value): bool {
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
private function bool($value): bool {
|
||||
return is_bool($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
private function not_false($value): bool {
|
||||
return ($value !== false) && ($value !== 'false');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
private function not_null($value): bool {
|
||||
return !is_null($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
private function not_empty($value): bool {
|
||||
return !empty($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function max($value, $limit): bool {
|
||||
if (!$limit || !is_numeric($limit)) {
|
||||
throw new Exception("Validation rule max requires at least 1 parameter. " . json_encode($limit));
|
||||
}
|
||||
return $this->getSize($value) <= $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function min($value, $limit): bool {
|
||||
if (!$limit || !is_numeric($limit)) {
|
||||
throw new Exception("Validation rule max requires at least 1 parameter.");
|
||||
}
|
||||
return $this->getSize($value) >= $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of an attribute.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return int
|
||||
*/
|
||||
protected function getSize($value): int {
|
||||
// This method will determine if the attribute is a number or string and
|
||||
// return the proper size accordingly. If it is a number, then number itself
|
||||
// is the size.
|
||||
if (is_int($value)) {
|
||||
return $value;
|
||||
} elseif (is_array($value)) {
|
||||
return count($value);
|
||||
}
|
||||
|
||||
return mb_strlen($value ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $rule
|
||||
* @param $field
|
||||
* @param $parameter
|
||||
* @return string
|
||||
*/
|
||||
protected function getErrorMessage($rule, $field, $parameter = null): string {
|
||||
if (in_array($rule, ['max', 'min'])) {
|
||||
return $rule === 'max'
|
||||
? $field . ' cannot be longer than '. $parameter . ' characters '
|
||||
: $field . ' must be at least '. $parameter . ' characters long ';
|
||||
}
|
||||
|
||||
return $field . ' must be provided and must be '. str_replace("_", " ", $rule);
|
||||
}
|
||||
}
|
||||
47
lib/Validators/BoardServiceValidator.php
Normal file
47
lib/Validators/BoardServiceValidator.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class BoardServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'id' => ['numeric'],
|
||||
'boardId' => ['numeric'],
|
||||
'type' => ['numeric'],
|
||||
'mapper' => ['not_empty', 'not_null', 'not_false'],
|
||||
'title' => ['not_empty', 'not_null', 'not_false', 'max:100'],
|
||||
'userId' => ['not_empty', 'not_null', 'not_false', 'max:64'],
|
||||
'color' => ['not_empty', 'not_null', 'not_false', 'max:6'],
|
||||
'participant' => ['not_empty', 'not_null', 'not_false', 'max:64'],
|
||||
'edit' => ['not_null'],
|
||||
'share' => ['not_null'],
|
||||
'manage' => ['not_null'],
|
||||
'archived' => ['bool']
|
||||
];
|
||||
}
|
||||
}
|
||||
44
lib/Validators/CardServiceValidator.php
Normal file
44
lib/Validators/CardServiceValidator.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class CardServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'id' => ['numeric'],
|
||||
'title' => ['not_empty', 'not_null', 'not_false', 'max:255'],
|
||||
'cardId' => ['numeric'],
|
||||
'stackId' => ['numeric'],
|
||||
'boardId' => ['numeric'],
|
||||
'labelId' => ['numeric'],
|
||||
'type' => ['not_empty', 'not_null', 'not_false', 'max:64'],
|
||||
'order' => ['numeric'],
|
||||
'owner' => ['not_empty', 'not_null', 'not_false', 'max:64'],
|
||||
];
|
||||
}
|
||||
}
|
||||
39
lib/Validators/LabelServiceValidator.php
Normal file
39
lib/Validators/LabelServiceValidator.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class LabelServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'id' => ['numeric'],
|
||||
'title' => ['not_empty', 'not_null', 'not_false', 'max:100'],
|
||||
'boardId' => ['numeric', 'not_null'],
|
||||
'color' => ['not_empty', 'not_null', 'not_false', 'max:6']
|
||||
];
|
||||
}
|
||||
}
|
||||
39
lib/Validators/StackServiceValidator.php
Normal file
39
lib/Validators/StackServiceValidator.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
* @author Luka Trovic <luka.trovic@nextcloud.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Validators;
|
||||
|
||||
class StackServiceValidator extends BaseValidator {
|
||||
public function rules() {
|
||||
return [
|
||||
'id' => ['numeric'],
|
||||
'title' => ['not_empty', 'not_null', 'not_false', 'max:100'],
|
||||
'boardId' => ['numeric', 'not_null'],
|
||||
'order' => ['numeric', 'not_null']
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user