2022-09-30 17:52:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $integration The Integration Name
|
|
|
|
|
* @param string $identifier The id of the integration account
|
|
|
|
|
* @param string $username The username of the integration account
|
|
|
|
|
* @param bool $verified Is the integration account verified
|
|
|
|
|
*
|
|
|
|
|
* @return string JSON Array
|
|
|
|
|
*/
|
2022-09-30 19:22:45 +02:00
|
|
|
class LinkIntegrationEndpoint extends KeyAuthEndpoint {
|
2022-09-30 17:52:14 +02:00
|
|
|
|
|
|
|
|
public function __construct() {
|
2022-09-30 19:22:45 +02:00
|
|
|
$this->_route = 'users/{user}/integrations/link';
|
2022-09-30 17:52:14 +02:00
|
|
|
$this->_module = 'Core';
|
|
|
|
|
$this->_description = 'Link a integration to user';
|
|
|
|
|
$this->_method = 'POST';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute(Nameless2API $api, User $user): void {
|
|
|
|
|
$api->validateParams($_POST, ['integration', 'identifier', 'username']);
|
|
|
|
|
|
|
|
|
|
$integration = Integrations::getInstance()->getIntegration($_POST['integration']);
|
|
|
|
|
if ($integration === null) {
|
|
|
|
|
$api->throwError(CoreApiErrors::ERROR_INVALID_INTEGRATION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$integration_user = $user->getIntegration($integration->getName());
|
|
|
|
|
if ($integration_user != null) {
|
|
|
|
|
$api->throwError(CoreApiErrors::ERROR_INTEGRATION_ALREADY_LINKED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate username and make sure username is unique
|
|
|
|
|
if (!$integration->validateUsername($_POST['username'])) {
|
|
|
|
|
$api->throwError(CoreApiErrors::ERROR_INTEGRATION_USERNAME_ERRORS, $integration->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate identifier and make sure identifier is unique
|
|
|
|
|
if (!$integration->validateIdentifier($_POST['identifier'])) {
|
|
|
|
|
$api->throwError(CoreApiErrors::ERROR_INTEGRATION_IDENTIFIER_ERRORS, $integration->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$integrationUser = new IntegrationUser($integration);
|
|
|
|
|
$integrationUser->linkIntegration($user, $_POST['identifier'], $_POST['username'], $_POST['verified'] === true);
|
|
|
|
|
if ($_POST['verified'] === true) {
|
|
|
|
|
$integrationUser->verifyIntegration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$api->returnArray(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
}
|