diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-01-14 20:18:13 -0500 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-01-14 20:18:13 -0500 |
commit | 805be7d33155478ef32c4bd3643dcf4025d85a05 (patch) | |
tree | 4ee858d96f3e3bf2b630471db148446c3c56b693 /app/Validator | |
parent | dc35a78374e9b091505cfc56eefcd0c631c56e3a (diff) |
Move validator methods
Diffstat (limited to 'app/Validator')
-rw-r--r-- | app/Validator/AuthValidator.php | 130 | ||||
-rw-r--r-- | app/Validator/CategoryValidator.php | 74 | ||||
-rw-r--r-- | app/Validator/CommentValidator.php | 74 | ||||
-rw-r--r-- | app/Validator/CurrencyValidator.php | 35 | ||||
-rw-r--r-- | app/Validator/CustomFilterValidator.php | 74 | ||||
-rw-r--r-- | app/Validator/GroupValidator.php | 71 | ||||
-rw-r--r-- | app/Validator/LinkValidator.php | 59 |
7 files changed, 517 insertions, 0 deletions
diff --git a/app/Validator/AuthValidator.php b/app/Validator/AuthValidator.php new file mode 100644 index 00000000..e77a88c8 --- /dev/null +++ b/app/Validator/AuthValidator.php @@ -0,0 +1,130 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; +use Gregwar\Captcha\CaptchaBuilder; + +/** + * Authentication Validator + * + * @package validator + * @author Frederic Guillot + */ +class AuthValidator extends Base +{ + /** + * Validate user login form + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateForm(array $values) + { + $result = false; + $errors = array(); + + foreach (array('validateFields', 'validateLocking', 'validateCaptcha', 'validateCredentials') as $method) { + list($result, $errors) = $this->$method($values); + + if (! $result) { + break; + } + } + + return array($result, $errors); + } + + /** + * Validate credentials syntax + * + * @access private + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + private function validateFields(array $values) + { + $v = new Validator($values, array( + new Validators\Required('username', t('The username is required')), + new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50), + new Validators\Required('password', t('The password is required')), + )); + + return array( + $v->execute(), + $v->getErrors(), + ); + } + + /** + * Validate user locking + * + * @access private + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + private function validateLocking(array $values) + { + $result = true; + $errors = array(); + + if ($this->userLocking->isLocked($values['username'])) { + $result = false; + $errors['login'] = t('Your account is locked for %d minutes', BRUTEFORCE_LOCKDOWN_DURATION); + $this->logger->error('Account locked: '.$values['username']); + } + + return array($result, $errors); + } + + /** + * Validate password syntax + * + * @access private + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + private function validateCredentials(array $values) + { + $result = true; + $errors = array(); + + if (! $this->authenticationManager->passwordAuthentication($values['username'], $values['password'])) { + $result = false; + $errors['login'] = t('Bad username or password'); + } + + return array($result, $errors); + } + + /** + * Validate captcha + * + * @access private + * @param array $values Form values + * @return boolean + */ + private function validateCaptcha(array $values) + { + $result = true; + $errors = array(); + + if ($this->userLocking->hasCaptcha($values['username'])) { + if (! isset($this->sessionStorage->captcha)) { + $result = false; + } else { + $builder = new CaptchaBuilder; + $builder->setPhrase($this->sessionStorage->captcha); + $result = $builder->testPhrase(isset($values['captcha']) ? $values['captcha'] : ''); + + if (! $result) { + $errors['login'] = t('Invalid captcha'); + } + } + } + + return array($result, $errors);; + } +} diff --git a/app/Validator/CategoryValidator.php b/app/Validator/CategoryValidator.php new file mode 100644 index 00000000..715aed66 --- /dev/null +++ b/app/Validator/CategoryValidator.php @@ -0,0 +1,74 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; + +/** + * Category Validator + * + * @package validator + * @author Frederic Guillot + */ +class CategoryValidator extends Base +{ + /** + * Validate category creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $rules = array( + new Validators\Required('project_id', t('The project id is required')), + new Validators\Required('name', t('The name is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate category modification + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $rules = array( + new Validators\Required('id', t('The id is required')), + new Validators\Required('name', t('The name is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Integer('id', t('The id must be an integer')), + new Validators\Integer('project_id', t('The project id must be an integer')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50) + ); + } +} diff --git a/app/Validator/CommentValidator.php b/app/Validator/CommentValidator.php new file mode 100644 index 00000000..4eb54206 --- /dev/null +++ b/app/Validator/CommentValidator.php @@ -0,0 +1,74 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; + +/** + * Comment Validator + * + * @package validator + * @author Frederic Guillot + */ +class CommentValidator extends Base +{ + /** + * Validate comment creation + * + * @access public + * @param array $values Required parameters to save an action + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $rules = array( + new Validators\Required('task_id', t('This value is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate comment modification + * + * @access public + * @param array $values Required parameters to save an action + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $rules = array( + new Validators\Required('id', t('This value is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Integer('id', t('This value must be an integer')), + new Validators\Integer('task_id', t('This value must be an integer')), + new Validators\Integer('user_id', t('This value must be an integer')), + new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), + new Validators\Required('comment', t('Comment is required')) + ); + } +} diff --git a/app/Validator/CurrencyValidator.php b/app/Validator/CurrencyValidator.php new file mode 100644 index 00000000..a00af738 --- /dev/null +++ b/app/Validator/CurrencyValidator.php @@ -0,0 +1,35 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; + +/** + * Currency Validator + * + * @package validator + * @author Frederic Guillot + */ +class CurrencyValidator extends Base +{ + /** + * Validate + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, array( + new Validators\Required('currency', t('Field required')), + new Validators\Required('rate', t('Field required')), + )); + + return array( + $v->execute(), + $v->getErrors() + ); + } +} diff --git a/app/Validator/CustomFilterValidator.php b/app/Validator/CustomFilterValidator.php new file mode 100644 index 00000000..07f2a1eb --- /dev/null +++ b/app/Validator/CustomFilterValidator.php @@ -0,0 +1,74 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; + +/** + * Custom Filter Validator + * + * @package validator + * @author Frederic Guillot + */ +class CustomFilterValidator extends Base +{ + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Required('project_id', t('Field required')), + new Validators\Required('user_id', t('Field required')), + new Validators\Required('name', t('Field required')), + new Validators\Required('filter', t('Field required')), + new Validators\Integer('user_id', t('This value must be an integer')), + new Validators\Integer('project_id', t('This value must be an integer')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 100), 100), + new Validators\MaxLength('filter', t('The maximum length is %d characters', 100), 100) + ); + } + + /** + * Validate filter creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, $this->commonValidationRules()); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate filter modification + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $rules = array( + new Validators\Required('id', t('Field required')), + new Validators\Integer('id', t('This value must be an integer')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } +} diff --git a/app/Validator/GroupValidator.php b/app/Validator/GroupValidator.php new file mode 100644 index 00000000..2226abd3 --- /dev/null +++ b/app/Validator/GroupValidator.php @@ -0,0 +1,71 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; +use Kanboard\Model\Group; + +/** + * Group Validator + * + * @package validator + * @author Frederic Guillot + */ +class GroupValidator extends Base +{ + /** + * Validate creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, $this->commonValidationRules()); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate modification + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $rules = array( + new Validators\Required('id', t('The id is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Required('name', t('The name is required')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 100), 100), + new Validators\Unique('name', t('The name must be unique'), $this->db->getConnection(), Group::TABLE, 'id'), + new Validators\MaxLength('external_id', t('The maximum length is %d characters', 255), 255), + new Validators\Integer('id', t('This value must be an integer')), + ); + } +} diff --git a/app/Validator/LinkValidator.php b/app/Validator/LinkValidator.php new file mode 100644 index 00000000..10a826da --- /dev/null +++ b/app/Validator/LinkValidator.php @@ -0,0 +1,59 @@ +<?php + +namespace Kanboard\Validator; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; +use Kanboard\Model\Link; + +/** + * Link Validator + * + * @package validator + * @author Frederic Guillot + */ +class LinkValidator extends Base +{ + /** + * Validate creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, array( + new Validators\Required('label', t('Field required')), + new Validators\Unique('label', t('This label must be unique'), $this->db->getConnection(), Link::TABLE), + new Validators\NotEquals('label', 'opposite_label', t('The labels must be different')), + )); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate modification + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $v = new Validator($values, array( + new Validators\Required('id', t('Field required')), + new Validators\Required('opposite_id', t('Field required')), + new Validators\Required('label', t('Field required')), + new Validators\Unique('label', t('This label must be unique'), $this->db->getConnection(), Link::TABLE), + )); + + return array( + $v->execute(), + $v->getErrors() + ); + } +} |