Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Integrations settings system & Setting to turn off mc username requirement #3109

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/classes/Integrations/IntegrationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @package NamelessMC\Integrations
* @author Partydragen
* @version 2.0.0-pr13
* @version 2.1.0
* @license MIT
*/

Expand All @@ -15,6 +15,7 @@ abstract class IntegrationBase {
protected string $_icon;
private array $_errors = [];
protected Language $_language;
protected ?string $_settings = null;

protected string $_name;
protected ?int $_order;
Expand Down Expand Up @@ -59,6 +60,15 @@ public function getIcon(): string {
return $this->_icon;
}

/**
* Get the settings path for this integration.
*
* @return string Integration settings path.
*/
public function getSettings(): ?string {
return $this->_settings;
}

/**
* Get if this integration is enabled
*
Expand Down
2 changes: 2 additions & 0 deletions custom/languages/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@
"admin/trusted_proxies_not_configured": "{{linkStart}}Trusted proxies{{linkEnd}} are not configured, IP bans may not work properly.",
"admin/https_port_80": "You appear as if you are using HTTPS on port 80, which is almost definitely not true. If you use Cloudflare, make sure to set TLS encryption mode to Full (strict). If you run your own proxy, please configure it to send the X-Forwarded-Port header.",
"admin/debugging_enabled": "Debugging is enabled, sensitive information may be exposed to unauthorised visitors. Debugging should only be used for development, not on public sites.",
"admin/require_minecraft_username_on_registration": "Require Minecraft Username on registration?",
"admin/integration_settings_does_not_exist": "Integration settings file for integration {{integration}} does not exist",
"api/account_validated": "Account validated successfully",
"api/finish_registration_email": "Please check your emails to complete registration.",
"api/finish_registration_link": "Please click on the following link to complete registration:",
Expand Down
9 changes: 9 additions & 0 deletions custom/panel_templates/Default/core/integrations_edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@
</div>
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="hidden" name="action" value="general_settings">
<input type="submit" value="{$SUBMIT}" class="btn btn-primary">
</div>
</form>
</div>
</div>

{if isset($SETTINGS_TEMPLATE)}
<div class="card shadow mb-4">
<div class="card-body">
{include file=$SETTINGS_TEMPLATE}
</div>
</div>
{/if}

<!-- Spacing -->
<div style="height:1rem;"></div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@
<input class="form-control" type="number" name="discord_guild_id"
id="inputDiscordId" value="{$DISCORD_GUILD_ID_VALUE}">
</div>
<div class="form-group">
<label for="inputIntegrationLinkMethod">{$INTEGRATION_LINK_METHOD}</label>
<select name="integration_link_method" class="form-control" id="inputIntegrationLinkMethod">
<option value="bot" {if $INTEGRATION_LINK_METHOD_VALUE eq "bot" }
selected{/if}>{$BOT}</option>
<option value="oauth" {if $INTEGRATION_LINK_METHOD_VALUE eq "oauth" }
selected{/if}>{$OAUTH}</option>
</select>
</div>
<div type="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="submit" class="btn btn-primary" value="{$SUBMIT}">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form action="" method="post">
<div class="form-group">
<label for="inputLinkMethod">{$LINK_METHOD}</label>
<select name="link_method" class="form-control" id="inputLinkMethod">
<option value="bot" {if $LINK_METHOD_VALUE eq "bot" }
selected{/if}>{$DISCORD_BOT}</option>
<option value="oauth" {if $LINK_METHOD_VALUE eq "oauth" }
selected{/if}>{$OAUTH}</option>
</select>
</div>
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="hidden" name="action" value="integration_settings">
<input type="submit" value="{$SUBMIT}" class="btn btn-primary">
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<form action="" method="post">
<div class="form-group custom-control custom-switch">
<input id="inputPremiumAccounts" name="premium_account" type="checkbox" class="custom-control-input js-check-change" {if $PREMIUM_ACCOUNTS_VALUE} checked{/if}/>
<label for="inputPremiumAccounts" class="custom-control-label">
{$PREMIUM_ACCOUNTS}
</label>
</div>
<div class="form-group custom-control custom-switch">
<input id="inputUsernameRegistration" name="username_registration" type="checkbox" class="custom-control-input js-check-change" {if $REQUIRE_USERNAME_REGISTRATION_VALUE} checked{/if}/>
<label for="inputUsernameRegistration" class="custom-control-label">
{$REQUIRE_USERNAME_REGISTRATION}
</label>
</div>
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="hidden" name="action" value="integration_settings">
<input type="submit" value="{$SUBMIT}" class="btn btn-primary">
</div>
</form>
15 changes: 14 additions & 1 deletion modules/Core/classes/Integrations/MinecraftIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @package Modules\Core\Integrations
* @author Partydragen
* @version 2.0.0-pr13
* @version 2.1.0
* @license MIT
*/
class MinecraftIntegration extends IntegrationBase {
Expand All @@ -16,6 +16,7 @@ public function __construct(Language $language) {
$this->_name = 'Minecraft';
$this->_icon = 'fas fa-cubes';
$this->_language = $language;
$this->_settings = ROOT_PATH . '/modules/Core/includes/admin_integrations/minecraft.php';

parent::__construct();
}
Expand Down Expand Up @@ -134,6 +135,10 @@ public function validateIdentifier(string $identifier, int $integration_user_id
}

public function onRegistrationPageLoad(Fields $fields) {
if (Util::getSetting('mc_username_registration', '1', 'Minecraft Integration') != '1') {
return;
}

$username_value = ((isset($_POST['username']) && $_POST['username']) ? Output::getClean(Input::get('username')) : '');

$fields->add('username', Fields::TEXT, $this->_language->get('user', 'minecraft_username'), true, $username_value, null, null, 1);
Expand All @@ -144,6 +149,10 @@ public function beforeRegistrationValidation(Validate $validate) {
}

public function afterRegistrationValidation() {
if (Util::getSetting('mc_username_registration', '1', 'Minecraft Integration') != '1') {
return;
}

$username = Input::get('username');

// Validate username
Expand All @@ -165,6 +174,10 @@ public function afterRegistrationValidation() {
}

public function successfulRegistration(User $user) {
if (Util::getSetting('mc_username_registration', '1', 'Minecraft Integration') != '1') {
return;
}

$code = SecureRandom::alphanumeric();

$integrationUser = new IntegrationUser($this);
Expand Down
25 changes: 25 additions & 0 deletions modules/Core/includes/admin_integrations/minecraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
if (Input::exists()) {
if (Token::check()) {
if (Input::get('action') === 'integration_settings') {
$premium_account = isset($_POST['premium_account']) && $_POST['premium_account'] == 'on' ? '1' : '0';
Util::setSetting('uuid_linking', $premium_account);

$username_registration = isset($_POST['username_registration']) && $_POST['username_registration'] == 'on' ? '1' : '0';
Util::setSetting('mc_username_registration', $username_registration, 'Minecraft Integration');

Session::flash('integrations_success', $language->get('admin', 'integration_updated_successfully'));
Redirect::to(URL::build('/panel/core/integrations/', 'integration=' . $integration->getName()));
}
} else {
$errors[] = $language->get('general', 'invalid_token');
}
}

$smarty->assign([
'PREMIUM_ACCOUNTS' => $language->get('admin', 'force_premium_accounts'),
'PREMIUM_ACCOUNTS_VALUE' => Util::getSetting('uuid_linking'),
'REQUIRE_USERNAME_REGISTRATION' => $language->get('admin', 'require_minecraft_username_on_registration'),
'REQUIRE_USERNAME_REGISTRATION_VALUE' => Util::getSetting('mc_username_registration', '1', 'Minecraft Integration'),
'SETTINGS_TEMPLATE' => 'integrations/minecraft/integration_settings.tpl'
]);
32 changes: 22 additions & 10 deletions modules/Core/pages/panel/integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Partydragen
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr13
* NamelessMC version 2.1.0
*
* License: MIT
*
Expand Down Expand Up @@ -56,21 +56,33 @@
$errors = [];

if (Token::check()) {
DB::getInstance()->update('integrations', $integration->data()->id, [
'enabled' => Output::getClean(Input::get('enabled')),
'can_unlink' => Output::getClean(Input::get('can_unlink')),
'required' => Output::getClean(Input::get('required'))
]);

Session::flash('integrations_success', $language->get('admin', 'integration_updated_successfully'));
Redirect::to(URL::build('/panel/core/integrations/', 'integration=' . $integration->getName()));
if (Input::get('action') === 'general_settings') {
DB::getInstance()->update('integrations', $integration->data()->id, [
'enabled' => Output::getClean(Input::get('enabled')),
'can_unlink' => Output::getClean(Input::get('can_unlink')),
'required' => Output::getClean(Input::get('required'))
]);

Session::flash('integrations_success', $language->get('admin', 'integration_updated_successfully'));
Redirect::to(URL::build('/panel/core/integrations/', 'integration=' . $integration->getName()));
}
} else {
$errors[] = $language->get('general', 'invalid_token');
}
}

if ($integration->getSettings() !== null) {
if (file_exists($integration->getSettings())) {
require_once($integration->getSettings());
} else {
$errors[] = $language->get('admin', 'integration_settings_does_not_exist', [
'integration' => Output::getClean($integration->getName())
]);
}
}

$smarty->assign([
'EDITING_INTEGRATION' => $language->get('admin', 'editing_integration_x', ['integration' => $integration->getName()]),
'EDITING_INTEGRATION' => $language->get('admin', 'editing_integration_x', ['integration' => Output::getClean($integration->getName())]),
'BACK' => $language->get('general', 'back'),
'BACK_LINK' => URL::build('/panel/core/integrations'),
'ENABLED' => $language->get('admin', 'enabled'),
Expand Down
3 changes: 2 additions & 1 deletion modules/Discord Integration/classes/DiscordIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @package Modules\Core\Integrations
* @author Partydragen
* @version 2.0.2
* @version 2.1.0
* @license MIT
*/
class DiscordIntegration extends IntegrationBase {
Expand All @@ -15,6 +15,7 @@ public function __construct(Language $language) {
$this->_name = 'Discord';
$this->_icon = 'fab fa-discord';
$this->_language = $language;
$this->_settings = ROOT_PATH . '/modules/Discord Integration/includes/admin_integrations/discord.php';

parent::__construct();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
if (Input::exists()) {
if (Token::check()) {
if (Input::get('action') === 'integration_settings') {
Util::setSetting('integration_link_method', Input::get('link_method'), 'Discord Integration');

Session::flash('integrations_success', $language->get('admin', 'integration_updated_successfully'));
Redirect::to(URL::build('/panel/core/integrations/', 'integration=' . $integration->getName()));
}
} else {
$errors[] = $language->get('general', 'invalid_token');
}
}

$smarty->assign([
'OAUTH' => $language->get('admin', 'oauth'),
'DISCORD_BOT' => Discord::getLanguageTerm('discord_bot'),
'LINK_METHOD' => Discord::getLanguageTerm('link_method'),
'LINK_METHOD_VALUE' => Util::getSetting('integration_link_method', 'bot', 'Discord Integration'),
'SETTINGS_TEMPLATE' => 'integrations/discord/integration_settings.tpl'
]);
2 changes: 1 addition & 1 deletion modules/Discord Integration/language/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
"discord_integration\/unable_to_update_discord_roles": "Unable to update Discord roles list.",
"discord_integration\/unable_to_update_discord_username": "Unable to update Discord username.",
"discord_integration\/discord_bot": "Discord Bot",
"discord_integration\/integration_link_method": "Discord integration link method"
"discord_integration\/link_method": "Link method"
}
5 changes: 0 additions & 5 deletions modules/Discord Integration/pages/panel/discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
if ($validation->passed()) {
Util::setSetting('discord', Input::get('discord_guild_id'));

Util::setSetting('integration_link_method', Input::get('integration_link_method'), 'Discord Integration');
$success = Discord::getLanguageTerm('discord_settings_updated');

} else {
Expand Down Expand Up @@ -135,10 +134,6 @@
'linkStart' => '<a href="https://support.discord.com/hc/en-us/articles/206346498" target="_blank">',
'linkEnd' => '</a>',
]),
'OAUTH' => $language->get('admin', 'oauth'),
'BOT' => Discord::getLanguageTerm('discord_bot'),
'INTEGRATION_LINK_METHOD' => Discord::getLanguageTerm('integration_link_method'),
'INTEGRATION_LINK_METHOD_VALUE' => Util::getSetting('integration_link_method', 'bot', 'Discord Integration'),
]);

$template->onPageLoad();
Expand Down