2022-04-16 18:42:24 +02:00
|
|
|
<?php
|
2025-03-29 10:50:03 +00:00
|
|
|
|
2022-04-16 18:42:24 +02:00
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Represents a integration user.
|
2022-04-16 18:42:24 +02:00
|
|
|
*
|
|
|
|
|
* @package NamelessMC\Integrations
|
|
|
|
|
* @author Partydragen
|
|
|
|
|
* @version 2.0.0-pr13
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class IntegrationUser
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
private DB $_db;
|
2022-05-11 23:44:22 -06:00
|
|
|
private IntegrationUserData $_data;
|
2022-04-16 18:42:24 +02:00
|
|
|
private User $_user;
|
|
|
|
|
private IntegrationBase $_integration;
|
|
|
|
|
|
2025-03-29 10:50:03 +00:00
|
|
|
public function __construct(IntegrationBase $integration, ?string $value = null, string $field = 'id', $query_data = null)
|
2024-03-09 03:19:55 -08:00
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
$this->_db = DB::getInstance();
|
|
|
|
|
$this->_integration = $integration;
|
|
|
|
|
|
|
|
|
|
if (!$query_data && $value) {
|
|
|
|
|
$field = preg_replace('/[^A-Za-z_]+/', '', $field);
|
|
|
|
|
|
2022-05-01 23:16:22 -07:00
|
|
|
$data = $this->_db->query("SELECT * FROM nl2_users_integrations WHERE $field = ? AND integration_id = ?", [$value, $integration->data()->id]);
|
2022-04-16 18:42:24 +02:00
|
|
|
if ($data->count()) {
|
2022-05-11 23:44:22 -06:00
|
|
|
$this->_data = new IntegrationUserData($data->first());
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
2024-03-09 03:19:55 -08:00
|
|
|
} elseif ($query_data) {
|
2022-04-16 18:42:24 +02:00
|
|
|
// Load data from existing query.
|
2022-05-11 23:44:22 -06:00
|
|
|
$this->_data = new IntegrationUserData($query_data);
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Get the integration.
|
2022-04-16 18:42:24 +02:00
|
|
|
*
|
|
|
|
|
* @return IntegrationBase Integration type for this user
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getIntegration(): IntegrationBase
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
return $this->_integration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Get the NamelessMC User that belong to this integration user.
|
2022-04-16 18:42:24 +02:00
|
|
|
*
|
|
|
|
|
* @return User NamelessMC User that belong to this integration user
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getUser(): User
|
|
|
|
|
{
|
2022-07-08 22:49:22 -06:00
|
|
|
return $this->_user ??= new User($this->data()->user_id);
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the integration user data.
|
|
|
|
|
*
|
2022-05-11 23:44:22 -06:00
|
|
|
* @return IntegrationUserData This integration user data.
|
2022-04-16 18:42:24 +02:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function data(): IntegrationUserData
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
return $this->_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does this integration user exist?
|
|
|
|
|
*
|
|
|
|
|
* @return bool Whether the user exists (has data) or not.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function exists(): bool
|
|
|
|
|
{
|
|
|
|
|
return !empty($this->_data);
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get if this integration user is verified or not.
|
|
|
|
|
*
|
|
|
|
|
* @return bool Whether this integration user has been verified.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function isVerified(): bool
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
return $this->data()->verified;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update integration user data in the database.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param array $fields Column names and values to update.
|
2022-04-16 18:42:24 +02:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function update(array $fields = []): void
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
if (!$this->_db->update('users_integrations', $this->data()->id, $fields)) {
|
|
|
|
|
throw new RuntimeException('There was a problem updating integration user.');
|
|
|
|
|
}
|
2024-06-08 00:12:09 +02:00
|
|
|
|
|
|
|
|
// Sync username to website username?
|
|
|
|
|
if (isset($fields['username']) && Settings::get('username_sync') == $this->data()->integration_id) {
|
|
|
|
|
if (Settings::get('displaynames') === '1') {
|
|
|
|
|
$this->getUser()->update([
|
|
|
|
|
'username' => $fields['username'],
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->getUser()->update([
|
|
|
|
|
'username' => $fields['username'],
|
|
|
|
|
'nickname' => $fields['username'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save a new user linked to a specific integration.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param User $user The user to link
|
2022-04-16 18:42:24 +02:00
|
|
|
* @param string|null $identifier The id of the integration account
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string|null $username The username of the integration account
|
|
|
|
|
* @param bool $verified Verified the ownership of the integration account
|
|
|
|
|
* @param string|null $code (optional) The verification code to verify the ownership
|
2022-04-16 18:42:24 +02:00
|
|
|
*/
|
2025-03-29 10:50:03 +00:00
|
|
|
public function linkIntegration(User $user, ?string $identifier, ?string $username, bool $verified = false, ?string $code = null): void
|
2024-03-09 03:19:55 -08:00
|
|
|
{
|
2022-05-01 23:16:22 -07:00
|
|
|
$this->_db->query(
|
2024-03-09 03:19:55 -08:00
|
|
|
'INSERT INTO nl2_users_integrations (user_id, integration_id, identifier, username, verified, date, code) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
|
|
|
[
|
2022-04-16 18:42:24 +02:00
|
|
|
$user->data()->id,
|
|
|
|
|
$this->_integration->data()->id,
|
|
|
|
|
Output::getClean($identifier),
|
|
|
|
|
Output::getClean($username),
|
|
|
|
|
$verified ? 1 : 0,
|
|
|
|
|
date('U'),
|
2024-03-09 03:19:55 -08:00
|
|
|
$code,
|
2022-04-16 18:42:24 +02:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2022-07-10 18:59:49 -07:00
|
|
|
// Load the data for this integration from the query we just made
|
|
|
|
|
$this->_data = new IntegrationUserData($this->_db->query('SELECT * FROM nl2_users_integrations WHERE id = ?', [$this->_db->lastId()])->first());
|
|
|
|
|
|
2023-03-01 11:35:10 -08:00
|
|
|
EventHandler::executeEvent(new UserIntegrationLinkedEvent(
|
|
|
|
|
$this,
|
|
|
|
|
));
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Verify user integration.
|
2022-04-16 18:42:24 +02:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function verifyIntegration(): void
|
|
|
|
|
{
|
2022-04-16 18:42:24 +02:00
|
|
|
$this->update([
|
2022-05-31 11:29:47 -06:00
|
|
|
'verified' => true,
|
2024-03-09 03:19:55 -08:00
|
|
|
'code' => null,
|
2022-04-16 18:42:24 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->_integration->onSuccessfulVerification($this);
|
|
|
|
|
|
2023-03-01 11:35:10 -08:00
|
|
|
EventHandler::executeEvent(new UserIntegrationVerifiedEvent(
|
|
|
|
|
$this,
|
|
|
|
|
));
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete integration user data.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function unlinkIntegration(): void
|
|
|
|
|
{
|
2022-05-01 23:16:22 -07:00
|
|
|
$this->_db->query(
|
2024-03-09 03:19:55 -08:00
|
|
|
'DELETE FROM nl2_users_integrations WHERE user_id = ? AND integration_id = ?',
|
|
|
|
|
[
|
2022-04-16 18:42:24 +02:00
|
|
|
$this->data()->user_id,
|
2024-03-09 03:19:55 -08:00
|
|
|
$this->_integration->data()->id,
|
2022-04-16 18:42:24 +02:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-01 11:35:10 -08:00
|
|
|
EventHandler::executeEvent(new UserIntegrationUnlinkedEvent(
|
|
|
|
|
$this,
|
|
|
|
|
));
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
}
|