2016-03-05 00:36:11 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 07:35:37 +02:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
2016-08-20 23:24:22 +02:00
|
|
|
DataValidator::with('CustomValidations', true);
|
2016-07-19 07:35:37 +02:00
|
|
|
|
2017-04-16 07:35:04 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /user/signup Sign up
|
2021-01-08 20:27:24 +01:00
|
|
|
* @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
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path signs up an user on the system.
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission any
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +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.
|
2020-10-14 20:08:14 +02:00
|
|
|
* @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.
|
2020-05-13 00:22:51 +02:00
|
|
|
* @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';
|
2017-02-08 19:09:15 +01:00
|
|
|
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;
|
2016-12-23 05:56:01 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-03-05 00:36:11 +01:00
|
|
|
|
2016-07-04 20:57:00 +02:00
|
|
|
public function validations() {
|
2017-01-14 22:19:21 +01:00
|
|
|
$validations = [
|
2016-07-04 20:57:00 +02:00
|
|
|
'permission' => 'any',
|
2016-07-19 07:35:37 +02:00
|
|
|
'requestData' => [
|
|
|
|
'name' => [
|
2020-01-31 18:19:48 +01:00
|
|
|
'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' => [
|
2020-01-31 18:19:48 +01:00
|
|
|
'validation' => DataValidator::notBlank()->length(5, 200),
|
2016-07-19 07:35:37 +02:00
|
|
|
'error' => ERRORS::INVALID_PASSWORD
|
|
|
|
]
|
|
|
|
]
|
2016-07-04 20:57:00 +02:00
|
|
|
];
|
2018-08-20 00:17:03 +02:00
|
|
|
|
2017-01-14 22:19:21 +01:00
|
|
|
if(!$this->csvImported) {
|
|
|
|
$validations['requestData']['captcha'] = [
|
2020-10-14 20:08:14 +02:00
|
|
|
'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 20:57:00 +02:00
|
|
|
}
|
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();
|
2017-01-13 21:06:49 +01:00
|
|
|
$apiKey = APIKey::getDataStore(Controller::request('apiKey'), 'token');
|
2016-03-05 00:51:59 +01:00
|
|
|
|
2020-05-13 00:22:51 +02: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
|
|
|
}
|
2017-01-11 07:50:47 +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);
|
2017-01-10 20:04:25 +01:00
|
|
|
}
|
2020-05-13 00:22:51 +02:00
|
|
|
|
2016-09-09 07:24:12 +02:00
|
|
|
$userId = $this->createNewUserAndRetrieveId();
|
2017-06-05 16:41:52 +02:00
|
|
|
|
|
|
|
if(MailSender::getInstance()->isConnected()) {
|
|
|
|
$this->sendRegistrationMail();
|
|
|
|
}
|
2016-09-09 07:24:12 +02:00
|
|
|
|
|
|
|
Response::respondSuccess([
|
|
|
|
'userId' => $userId,
|
|
|
|
'userEmail' => $this->userEmail
|
|
|
|
]);
|
2018-08-20 00:17:03 +02:00
|
|
|
|
2016-12-29 01:55:00 +01: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');
|
2016-12-23 05:56:01 +01:00
|
|
|
$this->verificationToken = Hashing::generateRandomToken();
|
2016-03-20 19:45:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 00:06:47 +02:00
|
|
|
public function createNewUserAndRetrieveId() {
|
2020-05-13 00:22:51 +02:00
|
|
|
$user = User::getUser($this->userEmail,'email');
|
|
|
|
|
|
|
|
$userInstance = ($user->isNull() ? new User() : $user );
|
|
|
|
$UserTickets = ($user->isNull() ? 0 : $user->tickets);
|
2016-12-22 07:07:06 +01:00
|
|
|
|
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(),
|
2020-05-13 00:22:51 +02:00
|
|
|
'tickets' => $UserTickets,
|
2016-07-15 00:06:47 +02:00
|
|
|
'email' => $this->userEmail,
|
2016-12-22 07:07:06 +01:00
|
|
|
'password' => Hashing::hashPassword($this->userPassword),
|
2019-02-03 20:47:29 +01:00
|
|
|
'verificationToken' => (MailSender::getInstance()->isConnected()) ? $this->verificationToken : null,
|
2020-05-13 00:22:51 +02:00
|
|
|
'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
|
|
|
|
2016-03-20 19:45:40 +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() {
|
2017-06-05 16:41:52 +02:00
|
|
|
$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,
|
2016-12-23 05:56:01 +01:00
|
|
|
'name' => $this->userName,
|
2017-03-15 21:28:23 +01:00
|
|
|
'url' => Setting::getSetting('url')->getValue(),
|
2016-12-23 05:56:01 +01:00
|
|
|
'verificationToken' => $this->verificationToken
|
2016-07-15 00:06:47 +02:00
|
|
|
]);
|
2018-08-20 00:17:03 +02:00
|
|
|
|
2020-05-13 00:22:51 +02:00
|
|
|
if(!Controller::request('indirectSignUp')) $mailSender->send();
|
2016-07-15 00:06:47 +02:00
|
|
|
}
|
2016-03-05 00:51:59 +01:00
|
|
|
}
|