Move validator methods
This commit is contained in:
130
app/Validator/AuthValidator.php
Normal file
130
app/Validator/AuthValidator.php
Normal file
@@ -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);;
|
||||
}
|
||||
}
|
||||
74
app/Validator/CategoryValidator.php
Normal file
74
app/Validator/CategoryValidator.php
Normal file
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
74
app/Validator/CommentValidator.php
Normal file
74
app/Validator/CommentValidator.php
Normal file
@@ -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'))
|
||||
);
|
||||
}
|
||||
}
|
||||
35
app/Validator/CurrencyValidator.php
Normal file
35
app/Validator/CurrencyValidator.php
Normal file
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
74
app/Validator/CustomFilterValidator.php
Normal file
74
app/Validator/CustomFilterValidator.php
Normal file
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
71
app/Validator/GroupValidator.php
Normal file
71
app/Validator/GroupValidator.php
Normal file
@@ -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')),
|
||||
);
|
||||
}
|
||||
}
|
||||
59
app/Validator/LinkValidator.php
Normal file
59
app/Validator/LinkValidator.php
Normal file
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user