opensupports/server/controllers/user/signup.php

118 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2016-07-19 07:35:37 +02:00
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2016-07-19 07:35:37 +02:00
2016-03-05 00:51:59 +01:00
class SignUpController extends Controller {
const PATH = '/signup';
const METHOD = 'POST';
2016-07-15 00:06:47 +02:00
private $userEmail;
2016-07-27 05:50:20 +02:00
private $userName;
2016-07-15 00:06:47 +02:00
private $userPassword;
private $verificationToken;
2017-01-14 22:19:21 +01:00
private $csvImported;
public function __construct($csvImported = false) {
$this->csvImported = $csvImported;
}
public function validations() {
2017-01-14 22:19:21 +01:00
$validations = [
'permission' => 'any',
2016-07-19 07:35:37 +02:00
'requestData' => [
'name' => [
'validation' => DataValidator::length(2, 55),
2016-07-19 07:35:37 +02:00
'error' => ERRORS::INVALID_NAME
],
'email' => [
2016-07-19 21:41:04 +02:00
'validation' => DataValidator::email(),
2016-07-19 07:35:37 +02:00
'error' => ERRORS::INVALID_EMAIL
],
'password' => [
2016-07-19 21:41:04 +02:00
'validation' => DataValidator::length(5, 200),
2016-07-19 07:35:37 +02:00
'error' => ERRORS::INVALID_PASSWORD
]
]
];
2017-01-14 22:19:21 +01:00
if(!$this->csvImported) {
$validations['requestData']['captcha'] = [
'validation' => DataValidator::captcha(),
'error' => ERRORS::INVALID_CAPTCHA
];
}
return $validations;
}
2016-07-04 09:09:32 +02:00
2016-03-05 00:51:59 +01:00
public function handler() {
if(!Controller::isUserSystemEnabled()) {
throw new Exception(ERRORS::USER_SYSTEM_DISABLED);
}
2016-07-27 05:50:20 +02:00
$this->storeRequestData();
$apiKey = APIKey::getDataStore(Controller::request('apiKey'), 'token');
2016-03-05 00:51:59 +01:00
$existentUser = User::getUser($this->userEmail, 'email');
2016-03-05 00:51:59 +01:00
if (!$existentUser->isNull()) {
2017-01-14 22:19:21 +01:00
throw new Exception(ERRORS::USER_EXISTS);
2016-07-15 00:06:47 +02:00
}
2016-12-04 08:08:42 +01:00
$banRow = Ban::getDataStore($this->userEmail,'email');
if (!$banRow->isNull()) {
2017-01-14 22:19:21 +01:00
throw new Exception(ERRORS::ALREADY_BANNED);
2016-12-04 08:08:42 +01:00
}
2017-01-14 22:19:21 +01:00
if (!Setting::getSetting('registration')->value && $apiKey->isNull() && !$this->csvImported) {
throw new Exception(ERRORS::NO_PERMISSION);
}
$userId = $this->createNewUserAndRetrieveId();
$this->sendRegistrationMail();
Response::respondSuccess([
'userId' => $userId,
'userEmail' => $this->userEmail
]);
Log::createLog('SIGNUP', null, User::getDataStore($userId));
2016-07-15 00:06:47 +02:00
}
2016-07-27 05:50:20 +02:00
public function storeRequestData() {
$this->userName = Controller::request('name');
2016-07-15 00:06:47 +02:00
$this->userEmail = Controller::request('email');
$this->userPassword = Controller::request('password');
$this->verificationToken = Hashing::generateRandomToken();
}
2016-07-15 00:06:47 +02:00
public function createNewUserAndRetrieveId() {
2016-03-05 00:51:59 +01:00
$userInstance = new User();
2016-07-15 00:06:47 +02:00
$userInstance->setProperties([
2016-07-27 05:50:20 +02:00
'name' => $this->userName,
2016-11-30 07:33:46 +01:00
'signupDate' => Date::getCurrentDate(),
'tickets' => 0,
2016-07-15 00:06:47 +02:00
'email' => $this->userEmail,
'password' => Hashing::hashPassword($this->userPassword),
'verificationToken' => $this->verificationToken
2016-07-15 00:06:47 +02:00
]);
2016-03-05 00:51:59 +01:00
return $userInstance->store();
2016-03-05 00:51:59 +01:00
}
2016-07-15 00:06:47 +02:00
public function sendRegistrationMail() {
$mailSender = new MailSender();
$mailSender->setTemplate(MailTemplate::USER_SIGNUP, [
2016-07-27 05:50:20 +02:00
'to' => $this->userEmail,
'name' => $this->userName,
2017-03-15 21:28:23 +01:00
'url' => Setting::getSetting('url')->getValue(),
'verificationToken' => $this->verificationToken
2016-07-15 00:06:47 +02:00
]);
$mailSender->send();
}
2016-03-05 00:51:59 +01:00
}