Move validator methods

This commit is contained in:
Frederic Guillot
2016-01-14 20:18:13 -05:00
parent dc35a78374
commit 805be7d331
33 changed files with 605 additions and 477 deletions

View File

@@ -1,130 +0,0 @@
<?php
namespace Kanboard\Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use Gregwar\Captcha\CaptchaBuilder;
/**
* Authentication model
*
* @package model
* @author Frederic Guillot
*/
class Authentication 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);;
}
}

View File

@@ -2,9 +2,6 @@
namespace Kanboard\Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Category model
*
@@ -212,63 +209,4 @@ class Category extends Base
return true;
}
/**
* 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)
);
}
}

View File

@@ -3,8 +3,6 @@
namespace Kanboard\Model;
use Kanboard\Event\CommentEvent;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Comment model
@@ -152,63 +150,4 @@ class Comment extends Base
{
return $this->db->table(self::TABLE)->eq('id', $comment_id)->remove();
}
/**
* 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'))
);
}
}

View File

@@ -2,9 +2,6 @@
namespace Kanboard\Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Currency
*
@@ -83,24 +80,4 @@ class Currency extends Base
{
return $this->db->table(self::TABLE)->eq('currency', $currency)->update(array('rate' => $rate));
}
/**
* Validate
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validate(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()
);
}
}

View File

@@ -2,9 +2,6 @@
namespace Kanboard\Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Custom Filter model
*
@@ -102,63 +99,4 @@ class CustomFilter extends Base
{
return $this->db->table(self::TABLE)->eq('id', $filter_id)->remove();
}
/**
* 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()
);
}
}

View File

@@ -117,59 +117,4 @@ class Group extends Base
{
return $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values);
}
/**
* 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(), self::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')),
);
}
}

View File

@@ -3,8 +3,6 @@
namespace Kanboard\Model;
use PDO;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Link model
@@ -176,47 +174,4 @@ class Link extends Base
$this->db->table(self::TABLE)->eq('opposite_id', $link_id)->update(array('opposite_id' => 0));
return $this->db->table(self::TABLE)->eq('id', $link_id)->remove();
}
/**
* 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(), self::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(), self::TABLE),
));
return array(
$v->execute(),
$v->getErrors()
);
}
}