opensupports/server/controllers/user/signup.php

157 lines
5.0 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
2017-04-16 07:35:04 +02:00
/**
* @api {post} /user/signup Sign up
* @apiVersion 4.9.0
2017-04-16 07:35:04 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiName Sign up
2017-04-16 07:35:04 +02:00
*
* @apiGroup User
*
* @apiDescription This path signs up an user on the system.
2017-04-16 07:35:04 +02:00
*
* @apiPermission any
2017-04-16 07:35:04 +02:00
*
* @apiParam {String} name The name of the new user.
* @apiParam {String} email The email of the new user.
* @apiParam {String} password The password of the new user.
* @apiParam {String} apiKey apiKey to sign up an user if the registration system is disabled.
2019-02-03 20:47:29 +01:00
* @apiParam {String} customfield_ Custom field values for this user.
* @apiParam {Boolean} indirectSignUp Indicates if the new User has been created by ticket/create
2017-04-21 08:09:24 +02:00
*
* @apiUse INVALID_NAME
* @apiUse INVALID_EMAIL
* @apiUse INVALID_PASSWORD
* @apiUse INVALID_CAPTCHA
* @apiUse USER_EXISTS
* @apiUse ALREADY_BANNED
* @apiUse NO_PERMISSION
2019-02-03 20:47:29 +01:00
* @apiUse INVALID_CUSTOM_FIELD_OPTION
2017-04-16 07:35:04 +02:00
*
2017-04-22 03:33:17 +02:00
* @apiSuccess {Object} data Information about created user
* @apiSuccess {Number} data.userId Id of the new user
* @apiSuccess {String} data.userEmail Email of the new user
2017-04-16 07:35:04 +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;
2018-08-20 00:17:03 +02:00
2017-01-14 22:19:21 +01:00
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::notBlank()->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' => [
'validation' => DataValidator::notBlank()->length(5, 200),
2016-07-19 07:35:37 +02:00
'error' => ERRORS::INVALID_PASSWORD
]
]
];
2018-08-20 00:17:03 +02:00
2017-01-14 22:19:21 +01:00
if(!$this->csvImported) {
$validations['requestData']['captcha'] = [
'validation' => DataValidator::captcha(APIKey::USER_CREATE_PERMISSION),
2017-01-14 22:19:21 +01:00
'error' => ERRORS::INVALID_CAPTCHA
];
}
2018-08-20 00:17:03 +02:00
2017-01-14 22:19:21 +01:00
return $validations;
}
2016-07-04 09:09:32 +02:00
2016-03-05 00:51:59 +01:00
public function handler() {
2018-08-20 00:17:03 +02:00
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
$user = User::getUser($this->userEmail, 'email');
if (!$user->isNull() && !$user->notRegistered) {
2018-11-20 23:41:00 +01:00
throw new RequestException(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()) {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::ALREADY_BANNED);
2016-12-04 08:08:42 +01:00
}
2018-09-08 20:52:51 +02:00
if (!Setting::getSetting('registration')->value && $apiKey->isNull() && !Controller::isStaffLogged(2) && !$this->csvImported) {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::NO_PERMISSION);
}
$userId = $this->createNewUserAndRetrieveId();
if(MailSender::getInstance()->isConnected()) {
$this->sendRegistrationMail();
}
Response::respondSuccess([
'userId' => $userId,
'userEmail' => $this->userEmail
]);
2018-08-20 00:17:03 +02:00
Log::createLog('SIGNUP', null, User::getDataStore($userId));
2016-07-15 00:06:47 +02:00
}
2018-08-20 00:17:03 +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() {
$user = User::getUser($this->userEmail,'email');
$userInstance = ($user->isNull() ? new User() : $user );
$UserTickets = ($user->isNull() ? 0 : $user->tickets);
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' => $UserTickets,
2016-07-15 00:06:47 +02:00
'email' => $this->userEmail,
'password' => Hashing::hashPassword($this->userPassword),
2019-02-03 20:47:29 +01:00
'verificationToken' => (MailSender::getInstance()->isConnected()) ? $this->verificationToken : null,
'notRegistered' => Controller::request('indirectSignUp') ? true : null,
2019-02-03 20:47:29 +01:00
'xownCustomfieldvalueList' => $this->getCustomFieldValues()
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
}
2018-08-20 00:17:03 +02:00
2016-07-15 00:06:47 +02:00
public function sendRegistrationMail() {
$mailSender = MailSender::getInstance();
2018-08-20 00:17:03 +02:00
2016-07-15 00:06:47 +02:00
$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
]);
2018-08-20 00:17:03 +02:00
if(!Controller::request('indirectSignUp')) $mailSender->send();
2016-07-15 00:06:47 +02:00
}
2016-03-05 00:51:59 +01:00
}