Compare commits
5 Commits
loginServi
...
cors
| Author | SHA1 | Date | |
|---|---|---|---|
| 120e07b315 | |||
| 59472e5650 | |||
| fea0587ceb | |||
| 882a9ccad1 | |||
| e033e1ce58 |
@@ -15,7 +15,9 @@
|
|||||||
"php": ">=5.5.0",
|
"php": ">=5.5.0",
|
||||||
"slim/slim": "^3.1",
|
"slim/slim": "^3.1",
|
||||||
"slim/php-view": "^2.0",
|
"slim/php-view": "^2.0",
|
||||||
"monolog/monolog": "^1.17"
|
"monolog/monolog": "^1.17",
|
||||||
|
"respect/validation": "^1.1",
|
||||||
|
"tuupola/cors-middleware": "^0.5.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": ">=4.8 < 6.0"
|
"phpunit/phpunit": ">=4.8 < 6.0"
|
||||||
|
|||||||
231
api-payroll/composer.lock
generated
231
api-payroll/composer.lock
generated
@@ -4,8 +4,8 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"hash": "9f4397e11cb2603e7754216c4f59c7ad",
|
"hash": "93a9656f4e6eb0e25be1bad59ac6f487",
|
||||||
"content-hash": "5e16cb7781829836a704bd8767830833",
|
"content-hash": "a3fc18885cc45d2733b77fa2081bdc72",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "container-interop/container-interop",
|
"name": "container-interop/container-interop",
|
||||||
@@ -116,6 +116,61 @@
|
|||||||
],
|
],
|
||||||
"time": "2017-06-19 01:22:40"
|
"time": "2017-06-19 01:22:40"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "neomerx/cors-psr7",
|
||||||
|
"version": "v1.0.13",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/neomerx/cors-psr7.git",
|
||||||
|
"reference": "2556e2013f16a55532c95928455257d5b6bbc6e2"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/neomerx/cors-psr7/zipball/2556e2013f16a55532c95928455257d5b6bbc6e2",
|
||||||
|
"reference": "2556e2013f16a55532c95928455257d5b6bbc6e2",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.6.0",
|
||||||
|
"psr/http-message": "^1.0",
|
||||||
|
"psr/log": "^1.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"mockery/mockery": "^1.0",
|
||||||
|
"phpunit/phpunit": "^5.7",
|
||||||
|
"scrutinizer/ocular": "^1.1",
|
||||||
|
"squizlabs/php_codesniffer": "^3.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Neomerx\\Cors\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"Apache-2.0"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "neomerx",
|
||||||
|
"email": "info@neomerx.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Framework agnostic (PSR-7) CORS implementation (www.w3.org/TR/cors/)",
|
||||||
|
"homepage": "https://github.com/neomerx/cors-psr7",
|
||||||
|
"keywords": [
|
||||||
|
"Cross Origin Resource Sharing",
|
||||||
|
"Cross-Origin Resource Sharing",
|
||||||
|
"cors",
|
||||||
|
"neomerx",
|
||||||
|
"psr-7",
|
||||||
|
"psr7",
|
||||||
|
"w3.org",
|
||||||
|
"www.w3.org"
|
||||||
|
],
|
||||||
|
"time": "2018-05-23 16:10:11"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "nikic/fast-route",
|
"name": "nikic/fast-route",
|
||||||
"version": "v1.3.0",
|
"version": "v1.3.0",
|
||||||
@@ -358,6 +413,69 @@
|
|||||||
],
|
],
|
||||||
"time": "2016-10-10 12:19:37"
|
"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",
|
"name": "slim/php-view",
|
||||||
"version": "2.2.0",
|
"version": "2.2.0",
|
||||||
@@ -477,6 +595,115 @@
|
|||||||
"router"
|
"router"
|
||||||
],
|
],
|
||||||
"time": "2018-04-19 19:29:08"
|
"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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tuupola/cors-middleware",
|
||||||
|
"version": "0.5.2",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/tuupola/cors-middleware.git",
|
||||||
|
"reference": "db69d8e67b99570b16e8cd5f78c423ed1167cb21"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/tuupola/cors-middleware/zipball/db69d8e67b99570b16e8cd5f78c423ed1167cb21",
|
||||||
|
"reference": "db69d8e67b99570b16e8cd5f78c423ed1167cb21",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"neomerx/cors-psr7": "^1.0",
|
||||||
|
"php": "^5.5 || ^7.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^4.8",
|
||||||
|
"squizlabs/php_codesniffer": "^2.5",
|
||||||
|
"zendframework/zend-diactoros": "^1.3"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Tuupola\\Middleware\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Mika Tuupola",
|
||||||
|
"email": "tuupola@appelsiini.net",
|
||||||
|
"homepage": "http://www.appelsiini.net/",
|
||||||
|
"role": "Developer"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PSR-7 CORS Middleware",
|
||||||
|
"homepage": "https://github.com/tuupola/cors-middleware",
|
||||||
|
"keywords": [
|
||||||
|
"cors",
|
||||||
|
"middleware",
|
||||||
|
"slim"
|
||||||
|
],
|
||||||
|
"time": "2016-08-12 13:12:58"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
|
|||||||
@@ -17,6 +17,20 @@ session_start();
|
|||||||
$settings = require __DIR__ . '/../src/settings.php';
|
$settings = require __DIR__ . '/../src/settings.php';
|
||||||
$app = new \Slim\App($settings);
|
$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
|
// Set up dependencies
|
||||||
require __DIR__ . '/../src/dependencies.php';
|
require __DIR__ . '/../src/dependencies.php';
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Application;
|
namespace App\Application;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class SessionApplication{
|
class SessionApplication{
|
||||||
private $pdo;
|
private $pdo;
|
||||||
private $cryptographyService;
|
private $cryptographyService;
|
||||||
|
private $asserts;
|
||||||
|
|
||||||
function __construct($mysql, $cryptographyService){
|
function __construct($mysql, $cryptographyService, $asserts){
|
||||||
$this->cryptographyService = $cryptographyService;
|
$this->cryptographyService = $cryptographyService;
|
||||||
$this->pdo = $mysql;
|
$this->pdo = $mysql;
|
||||||
|
$this->asserts = $asserts;
|
||||||
|
|
||||||
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
|
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
|
||||||
}
|
}
|
||||||
@@ -39,6 +43,8 @@ class SessionApplication{
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function getPassword($userName){
|
function getPassword($userName){
|
||||||
|
$this->asserts->userName($userName);
|
||||||
|
|
||||||
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
|
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
|
||||||
$stmt->execute(array(':userName' => $userName));
|
$stmt->execute(array(':userName' => $userName));
|
||||||
$results = $stmt->fetchAll();
|
$results = $stmt->fetchAll();
|
||||||
@@ -56,6 +62,9 @@ class SessionApplication{
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
function newSession($userName, $password){
|
function newSession($userName, $password){
|
||||||
|
$this->asserts->userName($userName);
|
||||||
|
$this->asserts->password($password);
|
||||||
|
|
||||||
$storedPassword = $this->getPassword($userName);
|
$storedPassword = $this->getPassword($userName);
|
||||||
|
|
||||||
// If the credentials don't match anything in the the records
|
// If the credentials don't match anything in the the records
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ $container['logger'] = function ($c) {
|
|||||||
return $logger;
|
return $logger;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mysql connecrion
|
// Mysql connection
|
||||||
$container['mysql'] = function ($c) {
|
$container['mysql'] = function ($c) {
|
||||||
$mysqlSettings = $c->get('settings')['mysql'];
|
$mysqlSettings = $c->get('settings')['mysql'];
|
||||||
|
|
||||||
@@ -32,8 +32,6 @@ $container['mysql'] = function ($c) {
|
|||||||
|
|
||||||
// Generic error messages
|
// Generic error messages
|
||||||
$databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage'];
|
$databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage'];
|
||||||
$databaseSelectQueryErrorMessage = $mysqlSettings['databaseSelectQueryErrorMessage'];
|
|
||||||
$databaseInsertQueryErrorMessage = $mysqlSettings['databaseInsertQueryErrorMessage'];
|
|
||||||
|
|
||||||
// Initiate the connection
|
// Initiate the connection
|
||||||
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
|
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
|
||||||
@@ -53,8 +51,14 @@ $container['cryptographyService'] = function ($c) {
|
|||||||
return $cryptographyService;
|
return $cryptographyService;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Assert functions
|
||||||
|
$container['asserts'] = function ($c) {
|
||||||
|
$asserts = new App\Service\Asserts();
|
||||||
|
return $asserts;
|
||||||
|
};
|
||||||
|
|
||||||
// The session application
|
// The session application
|
||||||
$container['sessionApplication'] = function ($c) {
|
$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;
|
return $sessionApplication;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,3 +2,21 @@
|
|||||||
// Application middleware
|
// Application middleware
|
||||||
|
|
||||||
// e.g: $app->add(new \Slim\Csrf\Guard);
|
// e.g: $app->add(new \Slim\Csrf\Guard);
|
||||||
|
|
||||||
|
// Enable cors
|
||||||
|
$app->add(new \Tuupola\Middleware\Cors([
|
||||||
|
"origin" => ["*"],
|
||||||
|
"methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
|
||||||
|
"headers.allow" => ["Accept", "Content-Type"],
|
||||||
|
"headers.expose" => [],
|
||||||
|
"credentials" => false,
|
||||||
|
"cache" => 0,
|
||||||
|
"logger" => $container['logger'],
|
||||||
|
"error" => function ($request, $response, $arguments) {
|
||||||
|
$data["status"] = "error";
|
||||||
|
$data["message"] = $arguments["message"];
|
||||||
|
return $response
|
||||||
|
->withHeader("Content-Type", "application/json")
|
||||||
|
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
|
||||||
|
}
|
||||||
|
]));
|
||||||
84
api-payroll/src/service/Asserts.php
Normal file
84
api-payroll/src/service/Asserts.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
|
class Asserts{
|
||||||
|
/**
|
||||||
|
* @param $string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
function userName($string){
|
||||||
|
$validateFirstName = v::stringType()->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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user