Moved validation to services

This commit is contained in:
Marcin Kurczewski 2014-08-31 14:07:46 +02:00
parent cb82416f27
commit c83c609b51
5 changed files with 60 additions and 7 deletions

View file

@ -5,3 +5,8 @@ name = booru-dev
[security] [security]
secret = change secret = change
minPasswordLength = 5
[users]
minUserNameLength = 1
maxUserNameLength = 32

View file

@ -4,15 +4,20 @@ namespace Szurubooru\Controllers;
final class AuthController extends AbstractController final class AuthController extends AbstractController
{ {
private $authService; private $authService;
private $userService;
private $passwordService;
private $inputReader; private $inputReader;
public function __construct( public function __construct(
\Szurubooru\Services\AuthService $authService, \Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\UserService $userService,
\Szurubooru\Services\PasswordService $passwordService,
\Szurubooru\Helpers\InputReader $inputReader) \Szurubooru\Helpers\InputReader $inputReader)
{ {
$this->authService = $authService; $this->authService = $authService;
$this->userService = $userService;
$this->passwordService = $passwordService;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
} }
public function registerRoutes(\Szurubooru\Router $router) public function registerRoutes(\Szurubooru\Router $router)
@ -25,10 +30,8 @@ final class AuthController extends AbstractController
{ {
if (isset($this->inputReader->userName) and isset($this->inputReader->password)) if (isset($this->inputReader->userName) and isset($this->inputReader->password))
{ {
if (!$this->inputReader->userName) $this->userService->validateUserName($this->inputReader->userName);
throw new \DomainException('User name cannot be empty.'); $this->passwordService->validatePassword($this->inputReader->password);
else if (!$this->inputReader->password)
throw new \DomainException('Password cannot be empty.');
$this->authService->loginFromCredentials($this->inputReader->userName, $this->inputReader->password); $this->authService->loginFromCredentials($this->inputReader->userName, $this->inputReader->password);
} }

View file

@ -4,10 +4,17 @@ namespace Szurubooru\Controllers;
final class UserController extends AbstractController final class UserController extends AbstractController
{ {
private $userService; private $userService;
private $passwordService;
private $inputReader;
public function __construct(\Szurubooru\Services\UserService $userService) public function __construct(
\Szurubooru\Services\UserService $userService,
\Szurubooru\Services\PasswordService $passwordService,
\Szurubooru\Helpers\InputReader $inputReader)
{ {
$this->inputReader = $inputReader;
$this->userService = $userService; $this->userService = $userService;
$this->passwordService = $passwordService;
} }
public function registerRoutes(\Szurubooru\Router $router) public function registerRoutes(\Szurubooru\Router $router)
@ -21,6 +28,9 @@ final class UserController extends AbstractController
public function create() public function create()
{ {
$this->userService->validateUserName($this->inputReader->userName);
$this->passwordService->validatePassword($this->inputReader->password);
throw new \BadMethodCallException('Not implemented'); throw new \BadMethodCallException('Not implemented');
} }

View file

@ -10,6 +10,24 @@ class PasswordService
$this->config = $config; $this->config = $config;
} }
public function validatePassword($password)
{
if (!$password)
throw new \DomainException('Password cannot be empty.');
$minPasswordLength = intval($this->config->security->minPasswordLength);
if (strlen($password) < $minPasswordLength)
throw new \DomainException('Password must have at least ' . $minPasswordLength . ' character(s).');
if (preg_match('/[^\x20-\x7f]/', $password))
{
throw new \DomainException(
'Password should contain only characters from ASCII range to avoid potential problems with encoding.');
}
return true;
}
public function getHash($password) public function getHash($password)
{ {
return hash('sha256', $this->config->security->secret . '/' . $password); return hash('sha256', $this->config->security->secret . '/' . $password);

View file

@ -4,10 +4,14 @@ namespace Szurubooru\Services;
class UserService class UserService
{ {
private $userDao; private $userDao;
private $config;
public function __construct(\Szurubooru\Dao\UserDao $userDao) public function __construct(
\Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Config $config)
{ {
$this->userDao = $userDao; $this->userDao = $userDao;
$this->config = $config;
} }
public function getById($userId) public function getById($userId)
@ -25,6 +29,19 @@ class UserService
return $this->userDao->save($user); return $this->userDao->save($user);
} }
public function validateUserName($userName)
{
if (!$userName)
throw new \DomainException('User name cannot be empty.');
$minUserNameLength = intval($this->config->users->minUserNameLength);
$maxUserNameLength = intval($this->config->users->maxserNameLength);
if (strlen($userName) < $minUserNameLength)
throw new \DomainException('User name must have at least ' . $minUserNameLength . ' character(s).');
if (strlen($userName) < $maxUserNameLength)
throw new \DomainException('User name must have at most ' . $minUserNameLength . ' character(s).');
}
public function getAnonymousUser() public function getAnonymousUser()
{ {
$user = new \Szurubooru\Entities\User(); $user = new \Szurubooru\Entities\User();