mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-31 01:35:15 +02:00
Guillermo - csv import [skip ci]
This commit is contained in:
parent
a34429d32a
commit
6eae5f9299
@ -16,6 +16,7 @@ require_once 'system/add-api-key.php';
|
|||||||
require_once 'system/delete-api-key.php';
|
require_once 'system/delete-api-key.php';
|
||||||
require_once 'system/get-all-keys.php';
|
require_once 'system/get-all-keys.php';
|
||||||
require_once 'system/delete-all-users.php';
|
require_once 'system/delete-all-users.php';
|
||||||
|
require_once 'system/csv-import.php';
|
||||||
|
|
||||||
$systemControllerGroup = new ControllerGroup();
|
$systemControllerGroup = new ControllerGroup();
|
||||||
$systemControllerGroup->setGroupPath('/system');
|
$systemControllerGroup->setGroupPath('/system');
|
||||||
@ -37,5 +38,6 @@ $systemControllerGroup->addController(new AddAPIKeyController);
|
|||||||
$systemControllerGroup->addController(new DeleteAPIKeyController);
|
$systemControllerGroup->addController(new DeleteAPIKeyController);
|
||||||
$systemControllerGroup->addController(new GetAllKeyController);
|
$systemControllerGroup->addController(new GetAllKeyController);
|
||||||
$systemControllerGroup->addController(new DeleteAllUsersController);
|
$systemControllerGroup->addController(new DeleteAllUsersController);
|
||||||
|
$systemControllerGroup->addController(new CSVImportController);
|
||||||
|
|
||||||
$systemControllerGroup->finalize();
|
$systemControllerGroup->finalize();
|
46
server/controllers/system/csv-import.php
Normal file
46
server/controllers/system/csv-import.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class CSVImportController extends Controller {
|
||||||
|
const PATH = '/csv-import';
|
||||||
|
|
||||||
|
public function validations() {
|
||||||
|
return [
|
||||||
|
'permission' => '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);
|
||||||
|
}
|
||||||
|
}
|
@ -10,9 +10,14 @@ class SignUpController extends Controller {
|
|||||||
private $userName;
|
private $userName;
|
||||||
private $userPassword;
|
private $userPassword;
|
||||||
private $verificationToken;
|
private $verificationToken;
|
||||||
|
private $csvImported;
|
||||||
|
|
||||||
|
public function __construct($csvImported = false) {
|
||||||
|
$this->csvImported = $csvImported;
|
||||||
|
}
|
||||||
|
|
||||||
public function validations() {
|
public function validations() {
|
||||||
return [
|
$validations = [
|
||||||
'permission' => 'any',
|
'permission' => 'any',
|
||||||
'requestData' => [
|
'requestData' => [
|
||||||
'name' => [
|
'name' => [
|
||||||
@ -26,13 +31,18 @@ class SignUpController extends Controller {
|
|||||||
'password' => [
|
'password' => [
|
||||||
'validation' => DataValidator::length(5, 200),
|
'validation' => DataValidator::length(5, 200),
|
||||||
'error' => ERRORS::INVALID_PASSWORD
|
'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() {
|
public function handler() {
|
||||||
@ -42,19 +52,16 @@ class SignUpController extends Controller {
|
|||||||
$existentUser = User::getUser($this->userEmail, 'email');
|
$existentUser = User::getUser($this->userEmail, 'email');
|
||||||
|
|
||||||
if (!$existentUser->isNull()) {
|
if (!$existentUser->isNull()) {
|
||||||
Response::respondError(ERRORS::USER_EXISTS);
|
throw new Exception(ERRORS::USER_EXISTS);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
$banRow = Ban::getDataStore($this->userEmail,'email');
|
$banRow = Ban::getDataStore($this->userEmail,'email');
|
||||||
|
|
||||||
if (!$banRow->isNull()) {
|
if (!$banRow->isNull()) {
|
||||||
Response::respondError(ERRORS::ALREADY_BANNED);
|
throw new Exception(ERRORS::ALREADY_BANNED);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Setting::getSetting('registration')->value && $apiKey->isNull() ) {
|
if (!Setting::getSetting('registration')->value && $apiKey->isNull() && !$this->csvImported) {
|
||||||
Response::respondError(ERRORS::NO_PERMISSION);
|
throw new Exception(ERRORS::NO_PERMISSION);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$userId = $this->createNewUserAndRetrieveId();
|
$userId = $this->createNewUserAndRetrieveId();
|
||||||
|
@ -19,6 +19,8 @@ include_once 'libs/MailSender.php';
|
|||||||
include_once 'libs/Date.php';
|
include_once 'libs/Date.php';
|
||||||
include_once 'libs/DataStoreList.php';
|
include_once 'libs/DataStoreList.php';
|
||||||
|
|
||||||
|
Controller::init();
|
||||||
|
|
||||||
// LOAD DATA
|
// LOAD DATA
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
$classPath = "data/{$class}.php";
|
$classPath = "data/{$class}.php";
|
||||||
|
@ -3,6 +3,7 @@ require_once 'libs/Validator.php';
|
|||||||
require_once 'models/Session.php';
|
require_once 'models/Session.php';
|
||||||
|
|
||||||
abstract class Controller {
|
abstract class Controller {
|
||||||
|
private static $dataRequester;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instance-related stuff
|
* Instance-related stuff
|
||||||
@ -28,10 +29,20 @@ abstract class Controller {
|
|||||||
$validator->validate($this->validations());
|
$validator->validate($this->validations());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function request($key) {
|
public static function init() {
|
||||||
|
self::$dataRequester = function ($key) {
|
||||||
$app = self::getAppInstance();
|
$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() {
|
public static function getLoggedUser() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user