From 6eae5f929989032fce606f048b52cac559845269 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Sat, 14 Jan 2017 18:19:21 -0300 Subject: [PATCH 1/3] Guillermo - csv import [skip ci] --- server/controllers/system.php | 2 ++ server/controllers/system/csv-import.php | 46 ++++++++++++++++++++++++ server/controllers/user/signup.php | 31 +++++++++------- server/index.php | 2 ++ server/libs/Controller.php | 17 +++++++-- 5 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 server/controllers/system/csv-import.php diff --git a/server/controllers/system.php b/server/controllers/system.php index 2f2c65c6..78163f11 100644 --- a/server/controllers/system.php +++ b/server/controllers/system.php @@ -16,6 +16,7 @@ require_once 'system/add-api-key.php'; require_once 'system/delete-api-key.php'; require_once 'system/get-all-keys.php'; require_once 'system/delete-all-users.php'; +require_once 'system/csv-import.php'; $systemControllerGroup = new ControllerGroup(); $systemControllerGroup->setGroupPath('/system'); @@ -37,5 +38,6 @@ $systemControllerGroup->addController(new AddAPIKeyController); $systemControllerGroup->addController(new DeleteAPIKeyController); $systemControllerGroup->addController(new GetAllKeyController); $systemControllerGroup->addController(new DeleteAllUsersController); +$systemControllerGroup->addController(new CSVImportController); $systemControllerGroup->finalize(); \ No newline at end of file diff --git a/server/controllers/system/csv-import.php b/server/controllers/system/csv-import.php new file mode 100644 index 00000000..daa0a99a --- /dev/null +++ b/server/controllers/system/csv-import.php @@ -0,0 +1,46 @@ + 'staff_3', + 'requestData' => [] + ]; + } + + public function handler() { + $file = fopen("Hoja1.csv","r"); + $errors = []; + + while(!feof($file)) { + $userList = fgetcsv($file); + + Controller::setDataRequester(function ($key) use ($userList) { + switch ($key) { + case 'email': + return $userList[0]; + case 'password': + return $userList[1]; + case 'name': + return $userList[2]; + } + + return null; + }); + + $signupController = new SignUpController(true); + + try { + $signupController->validate(); + $signupController->handler(); + } catch (\Exception $exception) { + $errors[] = $exception->getMessage() . ' in email ' . $userList[0]; + } + } + + fclose($file); + Response::respondSuccess($errors); + } +} \ No newline at end of file diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index cee72f1b..567c7b21 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -10,9 +10,14 @@ class SignUpController extends Controller { private $userName; private $userPassword; private $verificationToken; + private $csvImported; + + public function __construct($csvImported = false) { + $this->csvImported = $csvImported; + } public function validations() { - return [ + $validations = [ 'permission' => 'any', 'requestData' => [ 'name' => [ @@ -26,13 +31,18 @@ class SignUpController extends Controller { 'password' => [ 'validation' => DataValidator::length(5, 200), 'error' => ERRORS::INVALID_PASSWORD - ], - 'captcha' => [ - 'validation' => DataValidator::captcha(), - 'error' => ERRORS::INVALID_CAPTCHA ] ] ]; + + if(!$this->csvImported) { + $validations['requestData']['captcha'] = [ + 'validation' => DataValidator::captcha(), + 'error' => ERRORS::INVALID_CAPTCHA + ]; + } + + return $validations; } public function handler() { @@ -42,19 +52,16 @@ class SignUpController extends Controller { $existentUser = User::getUser($this->userEmail, 'email'); if (!$existentUser->isNull()) { - Response::respondError(ERRORS::USER_EXISTS); - return; + throw new Exception(ERRORS::USER_EXISTS); } $banRow = Ban::getDataStore($this->userEmail,'email'); if (!$banRow->isNull()) { - Response::respondError(ERRORS::ALREADY_BANNED); - return; + throw new Exception(ERRORS::ALREADY_BANNED); } - if (!Setting::getSetting('registration')->value && $apiKey->isNull() ) { - Response::respondError(ERRORS::NO_PERMISSION); - return; + if (!Setting::getSetting('registration')->value && $apiKey->isNull() && !$this->csvImported) { + throw new Exception(ERRORS::NO_PERMISSION); } $userId = $this->createNewUserAndRetrieveId(); diff --git a/server/index.php b/server/index.php index a9056e1e..040d3680 100644 --- a/server/index.php +++ b/server/index.php @@ -19,6 +19,8 @@ include_once 'libs/MailSender.php'; include_once 'libs/Date.php'; include_once 'libs/DataStoreList.php'; +Controller::init(); + // LOAD DATA spl_autoload_register(function ($class) { $classPath = "data/{$class}.php"; diff --git a/server/libs/Controller.php b/server/libs/Controller.php index 3c8a3ca9..b3bfdef8 100644 --- a/server/libs/Controller.php +++ b/server/libs/Controller.php @@ -3,6 +3,7 @@ require_once 'libs/Validator.php'; require_once 'models/Session.php'; abstract class Controller { + private static $dataRequester; /** * Instance-related stuff @@ -28,10 +29,20 @@ abstract class Controller { $validator->validate($this->validations()); } - public static function request($key) { - $app = self::getAppInstance(); + public static function init() { + self::$dataRequester = function ($key) { + $app = self::getAppInstance(); - return $app->request()->post($key); + return $app->request()->post($key); + }; + } + + public static function setDataRequester($dataRequester) { + self::$dataRequester = $dataRequester; + } + + public static function request($key) { + return call_user_func(self::$dataRequester, $key); } public static function getLoggedUser() { From 565c60c1988ee8c6e384580a79c10bf00b682176 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Sun, 15 Jan 2017 17:33:33 -0300 Subject: [PATCH 2/3] Guillermo - csv import [skip ci] --- server/controllers/system/csv-import.php | 7 ++++++- server/controllers/ticket/comment.php | 2 +- server/controllers/ticket/create.php | 2 +- server/libs/Controller.php | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/server/controllers/system/csv-import.php b/server/controllers/system/csv-import.php index daa0a99a..e415c6f1 100644 --- a/server/controllers/system/csv-import.php +++ b/server/controllers/system/csv-import.php @@ -11,7 +11,9 @@ class CSVImportController extends Controller { } public function handler() { - $file = fopen("Hoja1.csv","r"); + $fileUploader = $this->uploadFile(); + + $file = fopen($fileUploader->getFullFilePath(),'r'); $errors = []; while(!feof($file)) { @@ -41,6 +43,9 @@ class CSVImportController extends Controller { } fclose($file); + + unlink($fileUploader->getFullFilePath()); + Response::respondSuccess($errors); } } \ No newline at end of file diff --git a/server/controllers/ticket/comment.php b/server/controllers/ticket/comment.php index 6a043757..2b5c90b9 100644 --- a/server/controllers/ticket/comment.php +++ b/server/controllers/ticket/comment.php @@ -50,7 +50,7 @@ class CommentController extends Controller { $comment = Ticketevent::getEvent(Ticketevent::COMMENT); $comment->setProperties(array( 'content' => $this->content, - 'file' => $this->uploadFile(), + 'file' => $this->uploadFile()->getFileName(), 'date' => Date::getCurrentDate() )); diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index fbcd9203..1a07a0cb 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -60,7 +60,7 @@ class CreateController extends Controller { 'language' => $this->language, 'author' => $author, 'department' => $department, - 'file' => $this->uploadFile(), + 'file' => $this->uploadFile()->getFileName(), 'date' => Date::getCurrentDate(), 'unread' => false, 'unreadStaff' => true, diff --git a/server/libs/Controller.php b/server/libs/Controller.php index d24cb1af..632d758d 100644 --- a/server/libs/Controller.php +++ b/server/libs/Controller.php @@ -88,7 +88,7 @@ abstract class Controller { $fileQuantity->value++; $fileQuantity->store(); - return $fileUploader->getFileName(); + return $fileUploader; } else { throw new Exception(ERRORS::INVALID_FILE); } From c4b63aab4347bc026069fd27189b23a5a7217073 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Mon, 16 Jan 2017 18:04:26 -0300 Subject: [PATCH 3/3] Guillermo - disable-user-system [skip ci] --- server/controllers/system/csv-import.php | 4 ++++ server/controllers/ticket/comment.php | 4 +++- server/controllers/ticket/create.php | 4 +++- tests/init.rb | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server/controllers/system/csv-import.php b/server/controllers/system/csv-import.php index e415c6f1..ea3093b8 100644 --- a/server/controllers/system/csv-import.php +++ b/server/controllers/system/csv-import.php @@ -13,6 +13,10 @@ class CSVImportController extends Controller { public function handler() { $fileUploader = $this->uploadFile(); + if(!$fileUploader instanceof FileUploader) { + throw new Exception(ERRORS::INVALID_FILE); + } + $file = fopen($fileUploader->getFullFilePath(),'r'); $errors = []; diff --git a/server/controllers/ticket/comment.php b/server/controllers/ticket/comment.php index 2b5c90b9..9c27dc17 100644 --- a/server/controllers/ticket/comment.php +++ b/server/controllers/ticket/comment.php @@ -47,10 +47,12 @@ class CommentController extends Controller { } private function storeComment() { + $fileUploader = $this->uploadFile(); + $comment = Ticketevent::getEvent(Ticketevent::COMMENT); $comment->setProperties(array( 'content' => $this->content, - 'file' => $this->uploadFile()->getFileName(), + 'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null, 'date' => Date::getCurrentDate() )); diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 1a07a0cb..4b761dc5 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -53,6 +53,8 @@ class CreateController extends Controller { $department = Department::getDataStore($this->departmentId); $author = Controller::getLoggedUser(); + $fileUploader = $this->uploadFile(); + $ticket = new Ticket(); $ticket->setProperties(array( 'title' => $this->title, @@ -60,7 +62,7 @@ class CreateController extends Controller { 'language' => $this->language, 'author' => $author, 'department' => $department, - 'file' => $this->uploadFile()->getFileName(), + 'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null, 'date' => Date::getCurrentDate(), 'unread' => false, 'unreadStaff' => true, diff --git a/tests/init.rb b/tests/init.rb index 1ecc936f..dad08d9a 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -59,3 +59,4 @@ require './system/add-api-key.rb' require './system/delete-api-key.rb' require './system/get-all-keys.rb' require './system/file-upload-download.rb' +require './system/csv-import.rb'