From 21013cf6ac0225040402052544b801ca28f31dfe Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 21:55:39 -0600 Subject: [PATCH 1/4] [add] Generic asserts with custom errors --- .../src/application/SessionApplication.php | 20 +++++++-- api-payroll/src/service/Asserts.php | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/api-payroll/src/application/SessionApplication.php b/api-payroll/src/application/SessionApplication.php index 62ff2fd..77aa0b6 100644 --- a/api-payroll/src/application/SessionApplication.php +++ b/api-payroll/src/application/SessionApplication.php @@ -43,7 +43,9 @@ class SessionApplication{ * @return mixed */ function getPassword($userName){ - $this->asserts->userName($userName); + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); $stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName"); $stmt->execute(array(':userName' => $userName)); @@ -62,8 +64,12 @@ class SessionApplication{ * @throws Exception */ function newSession($userName, $password){ - $this->asserts->userName($userName); - $this->asserts->password($password); + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); + $this->asserts->isNotEmpty($password, "The password can't be empty"); + $this->asserts->isString($password, "The password must be a string."); + $this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters."); $storedPassword = $this->getPassword($userName); @@ -93,6 +99,14 @@ class SessionApplication{ * @throws Exception */ function login($userName, $password){ + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); + $this->asserts->isNotEmpty($password, "The password can't be empty"); + $this->asserts->isString($password, "The password must be a string."); + $this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters."); + + if($this->newSession($userName, $password)){ return array('status' => 'success', 'message' => 'Logged in successfully.'); } diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index 94b3023..9f9b96d 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -5,6 +5,47 @@ use Exception; use Respect\Validation\Validator as v; class Asserts{ + /** + * @param $string string + * @param $errorMessage string + * @throws Exception + */ + function isString($string, $errorMessage){ + $validation = v::stringType()->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + + /** + * @param $string string + * @param $errorMessage string + * @throws Exception + */ + function isNotEmpty($string, $errorMessage){ + $validation = v::notEmpty()->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + + /** + * @param $string string + * @param $min integer + * @param $max integer + * @param $errorMessage string + * @throws Exception + */ + function betweenLength($string, $min, $max, $errorMessage){ + $validation = v::length($min, $max)->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + /** * @param $string * @throws Exception From 08702b2cdf4e95928a9eb69178eb73762e4d6dcb Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 22:47:19 -0600 Subject: [PATCH 2/4] [add] Generic assers in employee application --- .../src/application/EmployeeApplication.php | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index c2ceebc..01196e7 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -56,11 +56,11 @@ class EmployeeApplication{ * @return integer */ function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){ - $this->asserts->firstName($firstName); - $this->asserts->middleName($middleName); - $this->asserts->birthDate($birthDate); - $this->asserts->email($email); - $this->asserts->phone($phone); + $this->asserts->isNotEmpty($firstName, "The first name can't be empty."); + $this->asserts->isNotEmpty($middleName, "The middle name can't be empty."); + $this->asserts->isNotEmpty($middleName, "The birth date can't be empty."); + $this->asserts->isNotEmpty($email, "The email can't be empty."); + $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); try { $stmt = $this->pdo->prepare("INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone) @@ -115,21 +115,29 @@ class EmployeeApplication{ function saveNewEmployee($requestData){ // Getting and validating the data $firstName = $requestData['firstName']; - $this->asserts->firstName($firstName); + $this->asserts->isNotEmpty($firstName, "The first name can't be empty."); + $this->asserts->isString($firstName, "The first name must be a string."); + $this->asserts->betweenLength($firstName, 1, 50, "The first name must have a length between 1 and 50 characters."); $middleName = $requestData['middleName']; - $this->asserts->middleName($middleName); + $this->asserts->isNotEmpty($middleName, "The middle name can't be empty."); + $this->asserts->isString($middleName, "The middle name must be a string."); + $this->asserts->betweenLength($middleName, 1, 50, "The middle name must have a length between 1 and 50 characters."); - $lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null; + $lastName = isset($requestData['lastName']) + ? $requestData['lastName'] + : null; $birthDate = $requestData['birthDate']; - $this->asserts->birthDate($birthDate); + $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty."); $email = $requestData['email']; - $this->asserts->email($email); + $this->asserts->isNotEmpty($email, "The email can't be empty."); + $this->asserts->betweenLength($email, 1, 100, "The middle name must have a length between 1 and 100 characters."); $phone = $requestData['phone']; - $this->asserts->phone($phone); + $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); + $this->asserts->betweenLength($phone, 10, 10, "The phone number must be 10 digits without special characters."); $idEmployeeType = $requestData{'idEmployeeType'}; $contractType = $requestData{'contractType'}; @@ -189,10 +197,12 @@ class EmployeeApplication{ } /** - * @param $code - * @return mixed + * @param $code string + * @return integer */ function getIdEmployeeTypeByCode($code){ + $this->asserts->isNotEmpty($code, "The code can't be empty."); + $stmt = $this->pdo->prepare("SELECT COALESCE((SELECT et.id FROM @@ -279,6 +289,8 @@ class EmployeeApplication{ * @return array */ function getEmployeeDataByCode($code){ + $this->asserts->isNotEmpty($code, "The code can't be empty."); + $idEmployee = $this->getIdEmployeeTypeByCode($code); return $this->proxyGetEmployeeDataById($idEmployee); @@ -350,24 +362,32 @@ class EmployeeApplication{ // Getting and validating the data $idEmployee = $requestData['idEmployee']; $idPerson = $this->getIdPersonByIdEmployee($idEmployee); + $code = $requestData['code']; + $this->asserts->isNotEmpty($code, "The code can't be empty."); $firstName = $requestData['firstName']; - $this->asserts->firstName($firstName); + $this->asserts->isNotEmpty($firstName, "The first name can't be empty."); + $this->asserts->isString($firstName, "The first name must be a string."); + $this->asserts->betweenLength($firstName, 1, 50, "The first name must have a length between 1 and 50 characters."); $middleName = $requestData['middleName']; - $this->asserts->middleName($middleName); + $this->asserts->isNotEmpty($middleName, "The middle name can't be empty."); + $this->asserts->isString($middleName, "The middle name must be a string."); + $this->asserts->betweenLength($middleName, 1, 50, "The middle name must have a length between 1 and 50 characters."); $lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null; $birthDate = $requestData['birthDate']; - $this->asserts->birthDate($birthDate); + $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty."); $email = $requestData['email']; - $this->asserts->email($email); + $this->asserts->isNotEmpty($email, "The email can't be empty."); + $this->asserts->betweenLength($email, 1, 100, "The middle name must have a length between 1 and 100 characters."); $phone = $requestData['phone']; - $this->asserts->phone($phone); + $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); + $this->asserts->betweenLength($phone, 10, 10, "The phone number must be 10 digits without special characters."); $idEmployeeType = $requestData{'idEmployeeType'}; $contractType = $requestData{'contractType'}; From 112f78c1deb0f5f85df40d0fe2b71f750e8e3432 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 22:48:42 -0600 Subject: [PATCH 3/4] [del] Old asserts --- api-payroll/src/service/Asserts.php | 76 ----------------------------- 1 file changed, 76 deletions(-) diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index 9f9b96d..6ab7d77 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -45,81 +45,5 @@ class Asserts{ throw new Exception($errorMessage); } } - - /** - * @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'); - } - } } ?> From 1500aef977ef9d1668fc2d887a81f28d8409c389 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 23:26:25 -0600 Subject: [PATCH 4/4] [add] Higher than zero asserting --- .../src/application/EmployeeApplication.php | 35 ++++++++++++++++++- api-payroll/src/service/Asserts.php | 11 ++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 01196e7..d17e9e1 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -58,7 +58,7 @@ class EmployeeApplication{ function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){ $this->asserts->isNotEmpty($firstName, "The first name can't be empty."); $this->asserts->isNotEmpty($middleName, "The middle name can't be empty."); - $this->asserts->isNotEmpty($middleName, "The birth date can't be empty."); + $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty."); $this->asserts->isNotEmpty($email, "The email can't be empty."); $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); @@ -89,6 +89,10 @@ class EmployeeApplication{ * @return mixed */ function savePersonAsEmployee($idEmployeeType, $idPerson, $code, $contractType){ + $this->asserts->higherThanZero($idEmployeeType, "idEmployeeType must be higher than 0"); + $this->asserts->higherThanZero($idPerson, "idPerson must be higher than 0"); + $this->asserts->isNotEmpty($code, "The code can't be empty."); + $this->asserts->isNotEmpty($contractType, "The contract type can't be empty."); try { $stmt = $this->pdo->prepare("INSERT INTO employees (idEmployeeType, idPerson, code, contractType) VALUES (:idEmployeeType, :idPerson, :code, :contractType)"); @@ -177,6 +181,8 @@ class EmployeeApplication{ * @return Integer */ function getIdPersonByIdEmployee($idEmployee){ + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + $stmt = $this->pdo->prepare("SELECT COALESCE((SELECT idPerson @@ -229,6 +235,8 @@ class EmployeeApplication{ * @return array */ function getEmployeeDataById($idEmployee){ + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + $stmt = $this->pdo->prepare("SELECT p.id AS idPerson, p.firstName, @@ -263,6 +271,8 @@ class EmployeeApplication{ * @return array */ function proxyGetEmployeeDataById($idEmployee){ + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + $employeeData = $this->getEmployeeDataById($idEmployee); $response = array( @@ -306,6 +316,13 @@ class EmployeeApplication{ * @param $phone string */ function updatePerson($idPerson, $firstName, $middleName, $lastName, $birthDate, $email, $phone){ + $this->asserts->higherThanZero($idPerson, "idPerson must be higher than 0"); + $this->asserts->isNotEmpty($firstName, "The first name can't be empty."); + $this->asserts->isNotEmpty($middleName, "The middle name can't be empty."); + $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty."); + $this->asserts->isNotEmpty($email, "The email can't be empty."); + $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); + try { $stmt = $this->pdo->prepare("UPDATE persons SET @@ -335,6 +352,11 @@ class EmployeeApplication{ * @param $contractType string */ function updateEmployee($idEmployee, $code, $idEmployeeType, $contractType){ + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + $this->asserts->isNotEmpty($code, "The code can't be empty."); + $this->asserts->higherThanZero($idEmployeeType, "idEmployeeType must be higher than 0"); + $this->asserts->isNotEmpty($contractType, "The contract type can't be empty."); + try { $stmt = $this->pdo->prepare("UPDATE employees SET @@ -361,7 +383,10 @@ class EmployeeApplication{ function updateEmployeeData($requestData){ // Getting and validating the data $idEmployee = $requestData['idEmployee']; + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + $idPerson = $this->getIdPersonByIdEmployee($idEmployee); + $this->asserts->higherThanZero($idPerson, "idPerson must be higher than 0"); $code = $requestData['code']; $this->asserts->isNotEmpty($code, "The code can't be empty."); @@ -390,7 +415,10 @@ class EmployeeApplication{ $this->asserts->betweenLength($phone, 10, 10, "The phone number must be 10 digits without special characters."); $idEmployeeType = $requestData{'idEmployeeType'}; + $this->asserts->higherThanZero($idEmployeeType, "idEmployeeType must be higher than 0"); + $contractType = $requestData{'contractType'}; + $this->asserts->isNotEmpty($contractType, "The contract type can't be empty."); // Encrypting the sensitive data $securedFirstName = $this->cryptographyService->encryptString($firstName); @@ -424,6 +452,8 @@ class EmployeeApplication{ } function disableEmployeeRecord($idEmployee){ + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + try { $stmt = $this->pdo->prepare("UPDATE employees SET @@ -467,6 +497,9 @@ class EmployeeApplication{ return $results; } + /** + * @return array + */ function listAllActiveEmployees(){ $ids = $this->getIdEmployeeFromAllActiveEmployees(); diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index 6ab7d77..9e1f1d3 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -45,5 +45,16 @@ class Asserts{ throw new Exception($errorMessage); } } + + /** + * @param $number integer + * @param $errorMessage string + * @throws Exception + */ + function higherThanZero($number, $errorMessage){ + if($number <= 0){ + throw new Exception($errorMessage); + } + } } ?>