From 6905a9d6af7e3e86223815047d08ddf8e3b2ca05 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Tue, 30 Jan 2018 15:50:24 +0100 Subject: [PATCH] [REFACTORING] Session is not welcome on src --- .../history/controllers/HistoryController.php | 4 +- .../status/controllers/StatusController.php | 70 ++++++++----------- src/app/status/models/StatusModelAbstract.php | 28 ++++---- 3 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/app/history/controllers/HistoryController.php b/src/app/history/controllers/HistoryController.php index e2246ab254d..db259474ac3 100644 --- a/src/app/history/controllers/HistoryController.php +++ b/src/app/history/controllers/HistoryController.php @@ -44,7 +44,7 @@ class HistoryController 'WHERE' => $aArgs['tableName'], 'ID' => $aArgs['recordId'], 'HOW' => $aArgs['eventType'], - 'USER' => $_SESSION['user']['UserId'], + 'USER' => $GLOBALS['userId'], 'WHAT' => $aArgs['eventId'], 'ID_MODULE' => $aArgs['moduleId'], 'REMOTE_IP' => $_SERVER['REMOTE_ADDR'], @@ -75,7 +75,7 @@ class HistoryController 'tableName' => $aArgs['tableName'], 'recordId' => $aArgs['recordId'], 'eventType' => $aArgs['eventType'], - 'userId' => $_SESSION['user']['UserId'], + 'userId' => $GLOBALS['userId'], 'info' => $aArgs['info'], 'moduleId' => $aArgs['moduleId'], 'eventId' => $aArgs['eventId'], diff --git a/src/app/status/controllers/StatusController.php b/src/app/status/controllers/StatusController.php index eff070cca45..dbd8f09b054 100755 --- a/src/app/status/controllers/StatusController.php +++ b/src/app/status/controllers/StatusController.php @@ -27,7 +27,7 @@ class StatusController { public function get(Request $request, Response $response) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -38,7 +38,7 @@ class StatusController public function getNewInformations(Request $request, Response $response) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -49,7 +49,7 @@ class StatusController public function getByIdentifier(Request $request, Response $response, $aArgs) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -71,7 +71,7 @@ class StatusController public function create(Request $request, Response $response) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -83,26 +83,24 @@ class StatusController return $response->withStatus(500)->withJson(['errors' => $errors]); } - if (StatusModel::create($aArgs)) { - $return['status'] = StatusModel::getById(['id' => $aArgs['id']])[0]; + StatusModel::create($aArgs); - HistoryController::add([ - 'tableName' => 'status', - 'recordId' => $return['status']['id'], - 'eventType' => 'ADD', - 'eventId' => 'statusup', - 'info' => _STATUS_ADDED . ' : ' . $return['status']['id'] - ]); + $return['status'] = StatusModel::getById(['id' => $aArgs['id']])[0]; - return $response->withJson($return); - } else { - return $response->withStatus(500)->withJson(['errors' => _NOT_CREATE]); - } + HistoryController::add([ + 'tableName' => 'status', + 'recordId' => $return['status']['id'], + 'eventType' => 'ADD', + 'eventId' => 'statusup', + 'info' => _STATUS_ADDED . ' : ' . $return['status']['id'] + ]); + + return $response->withJson($return); } public function update(Request $request, Response $response, $aArgs) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -116,28 +114,24 @@ class StatusController return $response->withStatus(500)->withJson(['errors' => $errors]); } - if (StatusModel::update($aArgs)) { - $return['status'] = StatusModel::getByIdentifier(['identifier' => $aArgs['identifier']])[0]; + StatusModel::update($aArgs); - HistoryController::add([ - 'tableName' => 'status', - 'recordId' => $return['status']['id'], - 'eventType' => 'UP', - 'eventId' => 'statusup', - 'info' => _MODIFY_STATUS . ' : ' . $return['status']['id'] - ]); - - return $response->withJson($return); - } else { - return $response - ->withStatus(500) - ->withJson(['errors' => _NOT_UPDATE]); - } + $return['status'] = StatusModel::getByIdentifier(['identifier' => $aArgs['identifier']])[0]; + + HistoryController::add([ + 'tableName' => 'status', + 'recordId' => $return['status']['id'], + 'eventType' => 'UP', + 'eventId' => 'statusup', + 'info' => _MODIFY_STATUS . ' : ' . $return['status']['id'] + ]); + + return $response->withJson($return); } public function delete(Request $request, Response $response, $aArgs) { - if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -159,11 +153,7 @@ class StatusController ->withJson(['errors' => 'identifier not valid']); } - return $response->withJson( - [ - 'statuses' => StatusModel::get() - ] - ); + return $response->withJson(['statuses' => StatusModel::get()]); } protected function manageValue($request) diff --git a/src/app/status/models/StatusModelAbstract.php b/src/app/status/models/StatusModelAbstract.php index a810a93fc41..80cae28a1bd 100755 --- a/src/app/status/models/StatusModelAbstract.php +++ b/src/app/status/models/StatusModelAbstract.php @@ -61,20 +61,20 @@ class StatusModelAbstract return $aReturn; } - public static function create(array $aArgs = []) + public static function create(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['id', 'label_status']); ValidatorModel::stringType($aArgs, ['id', 'label_status']); - $aReturn = DatabaseModel::insert([ + DatabaseModel::insert([ 'table' => 'status', 'columnsValues' => $aArgs - ]); + ]); - return $aReturn; + return true; } - public static function update(array $aArgs = []) + public static function update(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['label_status', 'identifier']); ValidatorModel::intVal($aArgs, ['identifier']); @@ -83,27 +83,27 @@ class StatusModelAbstract unset($aArgs['id']); unset($aArgs['identifier']); - $aReturn = DatabaseModel::update([ + DatabaseModel::update([ 'table' => 'status', 'set' => $aArgs, 'where' => ['identifier = ?'], 'data' => [$where['identifier']] ]); - return $aReturn; + return true; } - public static function delete(array $aArgs = []) + public static function delete(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['identifier']); ValidatorModel::intVal($aArgs, ['identifier']); - $aReturn = DatabaseModel::delete([ - 'table' => 'status', - 'where' => ['identifier = ?'], - 'data' => [$aArgs['identifier']] - ]); + DatabaseModel::delete([ + 'table' => 'status', + 'where' => ['identifier = ?'], + 'data' => [$aArgs['identifier']] + ]); - return $aReturn; + return true; } } -- GitLab