opensupports/server/controllers/user/signup.php

94 lines
2.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';
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;
public function validations() {
return [
'permission' => 'any',
2016-07-19 07:35:37 +02:00
'requestData' => [
'name' => [
2016-07-19 21:41:04 +02:00
'validation' => DataValidator::length(2, 55)->alpha(),
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
],
'captcha' => [
'validation' => DataValidator::captcha(),
'error' => ERRORS::INVALID_CAPTCHA
2016-07-19 07:35:37 +02:00
]
]
];
}
2016-07-04 09:09:32 +02:00
2016-03-05 00:51:59 +01:00
public function handler() {
2016-07-27 05:50:20 +02:00
$this->storeRequestData();
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()) {
Response::respondError(ERRORS::USER_EXISTS);
return;
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()) {
Response::respondError(ERRORS::ALREADY_BANNED);
return;
}
$userId = $this->createNewUserAndRetrieveId();
$this->sendRegistrationMail();
Response::respondSuccess([
'userId' => $userId,
'userEmail' => $this->userEmail
]);
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');
}
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)
]);
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
2016-07-15 00:06:47 +02:00
]);
$mailSender->send();
}
2016-03-05 00:51:59 +01:00
}