From 882a9ccad12d841829e4adab235f73c50cf05bfd Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Sun, 5 Aug 2018 20:37:31 -0600 Subject: [PATCH 1/2] [add] Asserts --- api-payroll/composer.json | 3 +- api-payroll/composer.lock | 126 +++++++++++++++++- .../src/application/SessionApplication.php | 11 +- api-payroll/src/dependencies.php | 12 +- api-payroll/src/service/Asserts.php | 84 ++++++++++++ 5 files changed, 228 insertions(+), 8 deletions(-) create mode 100644 api-payroll/src/service/Asserts.php diff --git a/api-payroll/composer.json b/api-payroll/composer.json index 86aff1a..a2aa1c6 100644 --- a/api-payroll/composer.json +++ b/api-payroll/composer.json @@ -15,7 +15,8 @@ "php": ">=5.5.0", "slim/slim": "^3.1", "slim/php-view": "^2.0", - "monolog/monolog": "^1.17" + "monolog/monolog": "^1.17", + "respect/validation": "^1.1" }, "require-dev": { "phpunit/phpunit": ">=4.8 < 6.0" diff --git a/api-payroll/composer.lock b/api-payroll/composer.lock index 6a094c8..f615ac9 100644 --- a/api-payroll/composer.lock +++ b/api-payroll/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "9f4397e11cb2603e7754216c4f59c7ad", - "content-hash": "5e16cb7781829836a704bd8767830833", + "hash": "677ee7ce2e986cfa3ab0df77d78e0a4c", + "content-hash": "fddce0c9f8dd9b23d45f6d6e4b4b6310", "packages": [ { "name": "container-interop/container-interop", @@ -358,6 +358,69 @@ ], "time": "2016-10-10 12:19:37" }, + { + "name": "respect/validation", + "version": "1.1.22", + "source": { + "type": "git", + "url": "https://github.com/Respect/Validation.git", + "reference": "19d6ec893994912d21b390c43d287816ab070772" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Validation/zipball/19d6ec893994912d21b390c43d287816ab070772", + "reference": "19d6ec893994912d21b390c43d287816ab070772", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "symfony/polyfill-mbstring": "^1.2" + }, + "require-dev": { + "egulias/email-validator": "~1.2", + "mikey179/vfsstream": "^1.5", + "phpunit/phpunit": "~4.0", + "symfony/validator": "~2.6.9", + "zendframework/zend-validator": "~2.3" + }, + "suggest": { + "egulias/email-validator": "Strict (RFC compliant) email validation", + "ext-bcmath": "Arbitrary Precision Mathematics", + "ext-mbstring": "Multibyte String Functions", + "friendsofphp/php-cs-fixer": "Fix PSR2 and other coding style issues", + "symfony/validator": "Use Symfony validator through Respect\\Validation", + "zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "Respect\\Validation\\": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD Style" + ], + "authors": [ + { + "name": "Respect/Validation Contributors", + "homepage": "https://github.com/Respect/Validation/graphs/contributors" + } + ], + "description": "The most awesome validation engine ever created for PHP", + "homepage": "http://respect.github.io/Validation/", + "keywords": [ + "respect", + "validation", + "validator" + ], + "time": "2018-08-01 13:06:54" + }, { "name": "slim/php-view", "version": "2.2.0", @@ -477,6 +540,65 @@ "router" ], "time": "2018-04-19 19:29:08" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.8.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "3296adf6a6454a050679cde90f95350ad604b171" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/3296adf6a6454a050679cde90f95350ad604b171", + "reference": "3296adf6a6454a050679cde90f95350ad604b171", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2018-04-26 10:06:28" } ], "packages-dev": [ diff --git a/api-payroll/src/application/SessionApplication.php b/api-payroll/src/application/SessionApplication.php index b7091e0..e916970 100644 --- a/api-payroll/src/application/SessionApplication.php +++ b/api-payroll/src/application/SessionApplication.php @@ -1,13 +1,17 @@ cryptographyService = $cryptographyService; $this->pdo = $mysql; + $this->asserts = $asserts; $this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.'; } @@ -39,6 +43,8 @@ class SessionApplication{ * @return mixed */ function getPassword($userName){ + $this->asserts->userName($userName); + $stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName"); $stmt->execute(array(':userName' => $userName)); $results = $stmt->fetchAll(); @@ -56,6 +62,9 @@ class SessionApplication{ * @throws Exception */ function newSession($userName, $password){ + $this->asserts->userName($userName); + $this->asserts->password($password); + $storedPassword = $this->getPassword($userName); // If the credentials don't match anything in the the records diff --git a/api-payroll/src/dependencies.php b/api-payroll/src/dependencies.php index 60ba341..9b14dd6 100644 --- a/api-payroll/src/dependencies.php +++ b/api-payroll/src/dependencies.php @@ -18,7 +18,7 @@ $container['logger'] = function ($c) { return $logger; }; -// Mysql connecrion +// Mysql connection $container['mysql'] = function ($c) { $mysqlSettings = $c->get('settings')['mysql']; @@ -32,8 +32,6 @@ $container['mysql'] = function ($c) { // Generic error messages $databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage']; - $databaseSelectQueryErrorMessage = $mysqlSettings['databaseSelectQueryErrorMessage']; - $databaseInsertQueryErrorMessage = $mysqlSettings['databaseInsertQueryErrorMessage']; // Initiate the connection $dsn = "mysql:host=$host;dbname=$database;charset=$charset"; @@ -53,8 +51,14 @@ $container['cryptographyService'] = function ($c) { return $cryptographyService; }; +// Assert functions +$container['asserts'] = function ($c) { + $asserts = new App\Service\Asserts(); + return $asserts; +}; + // The session application $container['sessionApplication'] = function ($c) { - $sessionApplication = new App\Application\SessionApplication($c['mysql'], $c['cryptographyService']); + $sessionApplication = new App\Application\SessionApplication($c['mysql'], $c['cryptographyService'], $c['asserts']); return $sessionApplication; }; diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php new file mode 100644 index 0000000..94b3023 --- /dev/null +++ b/api-payroll/src/service/Asserts.php @@ -0,0 +1,84 @@ +notEmpty()->length(1, 50)->validate($string); + + if(!$validateFirstName){ + throw new Exception('The user name must be a string between 1 and 50 characters'); + } + } + + /** + * @param $string + * @throws Exception + */ + function password($string){ + $validateFirstName = v::stringType()->notEmpty()->length(1, 50)->validate($string); + + if(!$validateFirstName){ + throw new Exception('The password must be a string between 1 and 50 characters'); + } + } + + /** + * @param $string + * @throws Exception + */ + function firstName($string){ + $validateFirstName = v::stringType()->notEmpty()->length(1, 100)->validate($string); + + if(!$validateFirstName){ + throw new Exception('The first name must be a string between 1 and 100 characters'); + } + } + + /** + * @param $string + * @throws Exception + */ + function middleName($string){ + if(!v::stringType()->notEmpty()->length(1, 100)->validate($string)){ + throw new Exception('The middle name must be a string between 1 and 100 characters'); + } + } + + /** + * @param $string + * @throws Exception + */ + function birthDate($string){ + if(!v::date('Y-m-d')->notEmpty()->validate($string)){ + throw new Exception('The birth date must be in the yyyy-mm-dd format'); + } + } + + /** + * @param $string + * @throws Exception + */ + function email($string){ + if(!v::stringType()->notEmpty()->length(1, 100)->validate($string)){ + throw new Exception('The email must be a string between 1 and 100 characters'); + } + } + + /** + * @param $string + * @throws Exception + */ + function phone($string){ + if(!v::digit()->notEmpty()->length(10, 10)->validate($string)){ + throw new Exception('The phone must be a numeric value of 10 digits'); + } + } +} +?> From fea0587cebb71e9a48be8e1f26a9da40569c9a43 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Sun, 5 Aug 2018 20:42:45 -0600 Subject: [PATCH 2/2] [add] Custom error handling --- api-payroll/public/index.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api-payroll/public/index.php b/api-payroll/public/index.php index 2784ee0..1e2f3f1 100644 --- a/api-payroll/public/index.php +++ b/api-payroll/public/index.php @@ -17,6 +17,20 @@ session_start(); $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); +// Custom error handling +$c = $app->getContainer(); +$c['errorHandler'] = function ($c) { + return function ($request, $response, $exception) use ($c) { + $data = [ + 'status' => 'error', + 'message' => $exception->getMessage() + ]; + return $c['response']->withStatus(500) + ->withHeader('Content-Type', 'application/json') + ->write(json_encode($data)); + }; +}; + // Set up dependencies require __DIR__ . '/../src/dependencies.php';