From b389b32d88843a9b074d68e06b2b341ab80c1736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Wed, 13 Nov 2013 01:43:12 +0100 Subject: [PATCH] - started adding admin commands ! admin commands begin with // --- application/core/Admin/AdminMenu.php | 5 ++ .../AuthenticationManager.php} | 53 ++++++++++++++++--- application/core/Commands/CommandManager.php | 22 ++++---- application/core/ManiaControl.php | 13 ++--- application/core/Maps/MapCommands.php | 27 +++++----- application/core/chat.php | 13 +++++ application/plugins/DonationPlugin.php | 26 ++++----- 7 files changed, 109 insertions(+), 50 deletions(-) create mode 100644 application/core/Admin/AdminMenu.php rename application/core/{authentication.php => Admin/AuthenticationManager.php} (70%) diff --git a/application/core/Admin/AdminMenu.php b/application/core/Admin/AdminMenu.php new file mode 100644 index 00000000..64387dd7 --- /dev/null +++ b/application/core/Admin/AdminMenu.php @@ -0,0 +1,5 @@ + diff --git a/application/core/authentication.php b/application/core/Admin/AuthenticationManager.php similarity index 70% rename from application/core/authentication.php rename to application/core/Admin/AuthenticationManager.php index ae51bdaa..6359b34a 100644 --- a/application/core/authentication.php +++ b/application/core/Admin/AuthenticationManager.php @@ -1,16 +1,19 @@ maniaControl = $maniaControl; $this->loadConfig(); + + $this->maniaControl->commandManager->registerCommandListener('/addadmin', $this, 'command_AddAdmin'); } /** @@ -45,7 +50,7 @@ class Authentication { $mysqli = $this->maniaControl->database->mysqli; // Remove all XSuperadmins - $adminQuery = "UPDATE `" . Players\PlayerManager::TABLE_PLAYERS . "` + $adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "` SET `authLevel` = ? WHERE `authLevel` = ?;"; $adminStatement = $mysqli->prepare($adminQuery); @@ -64,7 +69,7 @@ class Authentication { // Set XSuperAdmins $xAdmins = $config->xsuperadmins->xpath('login'); - $adminQuery = "INSERT INTO `" . Players\PlayerManager::TABLE_PLAYERS . "` ( + $adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` ( `login`, `authLevel` ) VALUES ( @@ -98,7 +103,7 @@ class Authentication { * @return bool */ public function grantAuthLevel(Player $player, $authLevel) { - if (!$player || $authLevel  >= self::AUTH_LEVEL_XSUPERADMIN) { + if (!$player || !is_int($authLevel) || $authLevel  >= self::AUTH_LEVEL_XSUPERADMIN) { return false; } $mysqli = $this->maniaControl->database->mysqli; @@ -138,6 +143,40 @@ class Authentication { return $this->maniaControl->chat->sendError('You do not have the required rights to perform this command!', $player->login); } + /** + * Handle //addadmin command + * + * @param array $chatCallback + * @param + * \ManiaControl\Players\Player + * @return boolean + */ + public function command_AddAdmin(array $chatCallback, Player $player) { + var_dump($chatCallback); + if (!$this->checkRight($player, self::AUTH_LEVEL_SUPERADMIN)) { + $this->sendNotAllowed($player); + return false; + } + $text = $chatCallback[1][2]; + $commandParts = explode(' ', $text); + if (!array_key_exists(1, $commandParts)) { + $this->sendAddAdminUsageInfo($player); + return false; + } + return true; + } + + /** + * Send usage example for //addadmin command + * + * @param Player $player + * @return bool + */ + private function sendAddAdminUsageInfo(Player $player) { + $message = "Usage Example: '//addadmin login'"; + return $this->maniaControl->chat->sendUsageInfo($message); + } + /** * Check if the player has enough rights * diff --git a/application/core/Commands/CommandManager.php b/application/core/Commands/CommandManager.php index a326c193..5ac684eb 100644 --- a/application/core/Commands/CommandManager.php +++ b/application/core/Commands/CommandManager.php @@ -4,8 +4,8 @@ namespace ManiaControl\Commands; require_once __DIR__ . '/CommandListener.php'; -use ManiaControl\Authentication; use ManiaControl\ManiaControl; +use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Players\Player; @@ -117,8 +117,8 @@ class CommandManager implements CallbackListener, CommandListener { * @return bool */ private function command_systeminfo(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } $systemInfo = $this->maniaControl->server->getSystemInfo(); @@ -135,8 +135,8 @@ class CommandManager implements CallbackListener, CommandListener { * @return bool */ private function command_shutdown(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } return $this->maniaControl->quit("ManiaControl shutdown requested by '{$player->login}'"); @@ -150,8 +150,8 @@ class CommandManager implements CallbackListener, CommandListener { * @return bool */ private function command_shutdownserver(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } // Check for delayed shutdown @@ -190,8 +190,8 @@ class CommandManager implements CallbackListener, CommandListener { * @return bool */ private function command_kick(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } $params = explode(' ', $chat[1][2], 3); @@ -220,8 +220,8 @@ class CommandManager implements CallbackListener, CommandListener { * @return bool */ private function command_setservername(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } $params = explode(' ', $chat[1][2], 2); diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 524938fb..dd38a057 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -2,17 +2,18 @@ namespace ManiaControl; -use ManiaControl\Players\PlayerManager; +use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Commands\CommandManager; use ManiaControl\Manialinks\ManialinkIdHandler; use ManiaControl\Maps\MapManager; +use ManiaControl\Players\PlayerManager; use ManiaControl\Plugins\PluginManager; -require_once __DIR__ . '/Authentication.php'; require_once __DIR__ . '/Callbacks/CallbackManager.php'; -require_once __DIR__ . '/Chat.php'; require_once __DIR__ . '/Commands/CommandManager.php'; +require_once __DIR__ . '/Admin/AuthenticationManager.php'; +require_once __DIR__ . '/Chat.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/FileUtil.php'; require_once __DIR__ . '/Formatter.php'; @@ -51,7 +52,7 @@ class ManiaControl { /** * Public properties */ - public $authentication = null; + public $authenticationManager = null; public $callbackManager = null; public $chat = null; public $client = null; @@ -80,9 +81,9 @@ class ManiaControl { $this->settingManager = new SettingManager($this); $this->chat = new Chat($this); $this->server = new Server($this); - $this->authentication = new Authentication($this); - $this->playerManager = new PlayerManager($this); $this->commandManager = new CommandManager($this); + $this->authenticationManager = new AuthenticationManager($this); + $this->playerManager = new PlayerManager($this); $this->mapManager = new MapManager($this); $this->pluginManager = new PluginManager($this); } diff --git a/application/core/Maps/MapCommands.php b/application/core/Maps/MapCommands.php index ba9d2203..d9e26f6f 100644 --- a/application/core/Maps/MapCommands.php +++ b/application/core/Maps/MapCommands.php @@ -2,8 +2,8 @@ namespace ManiaControl\Maps; -use ManiaControl\Authentication; use ManiaControl\ManiaControl; +use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Commands\CommandListener; use ManiaControl\Players\Player; @@ -26,10 +26,10 @@ class MapCommands implements CommandListener { public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - $this->maniaControl->commandManager->registerCommandListener('nextmap', $this, 'command_NextMap'); - $this->maniaControl->commandManager->registerCommandListener('restartmap', $this, 'command_RestartMap'); - $this->maniaControl->commandManager->registerCommandListener('addmap', $this, 'command_AddMap'); - $this->maniaControl->commandManager->registerCommandListener('removemap', $this, 'command_RemoveMap'); + $this->maniaControl->commandManager->registerCommandListener('/nextmap', $this, 'command_NextMap'); + $this->maniaControl->commandManager->registerCommandListener('/restartmap', $this, 'command_RestartMap'); + $this->maniaControl->commandManager->registerCommandListener('/addmap', $this, 'command_AddMap'); + $this->maniaControl->commandManager->registerCommandListener('/removemap', $this, 'command_RemoveMap'); } /** @@ -40,8 +40,8 @@ class MapCommands implements CommandListener { * @return bool */ public function command_RemoveMap(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } // Get map name @@ -69,10 +69,11 @@ class MapCommands implements CommandListener { * @return bool */ public function command_AddMap(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } + // TODO: mx fetcher nutzen? $params = explode(' ', $chat[1][2], 2); if (count($params) < 2) { // TODO: show usage @@ -158,8 +159,8 @@ class MapCommands implements CommandListener { * @return bool */ public function command_NextMap(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } return $this->maniaControl->client->query('NextMap'); @@ -173,8 +174,8 @@ class MapCommands implements CommandListener { * @return bool */ public function command_RestartMap(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } return $this->maniaControl->client->query('RestartMap'); diff --git a/application/core/chat.php b/application/core/chat.php index aeeef77d..6ae86131 100644 --- a/application/core/chat.php +++ b/application/core/chat.php @@ -97,6 +97,19 @@ class Chat { $format = $this->maniaControl->settingManager->getSetting($this, 'ErrorFormat', '$f00'); return $this->sendChat($format . $message, $login); } + + /** + * Send an usage info message to the given login + * + * @param string $message + * @param string $login + * @param string|bool $prefix + * @return bool + */ + public function sendUsageInfo($message, $login = null, $prefix = false) { + $format = $this->maniaControl->settingManager->getSetting($this, 'UsageInfoFormat', '$f80'); + return $this->sendChat($format . $message, $login); + } } ?> diff --git a/application/plugins/DonationPlugin.php b/application/plugins/DonationPlugin.php index c73daefb..bbc550af 100644 --- a/application/plugins/DonationPlugin.php +++ b/application/plugins/DonationPlugin.php @@ -1,6 +1,6 @@ description = 'DonationPlugin commands like /donate, /pay and /getplanets and a donation widget.'; $this->maniaControl->commandManager->registerCommandListener('donate', $this, 'command_Donate'); - $this->maniaControl->commandManager->registerCommandListener('pay', $this, 'command_Pay'); - $this->maniaControl->commandManager->registerCommandListener('getplanets', $this, 'command_GetPlanets'); + $this->maniaControl->commandManager->registerCommandListener('/pay', $this, 'command_Pay'); + $this->maniaControl->commandManager->registerCommandListener('/getplanets', $this, 'command_GetPlanets'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); } @@ -85,15 +85,15 @@ class DonationPlugin extends Plugin implements CallbackListener, CommandListener } /** - * Handle /pay command + * Handle //pay command * * @param array $chatCallback * @param Player $player * @return bool */ public function command_Pay(array $chatCallback, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } $text = $chatCallback[1][2]; @@ -127,15 +127,15 @@ class DonationPlugin extends Plugin implements CallbackListener, CommandListener } /** - * Handle /getplanets command + * Handle //getplanets command * * @param array $chat * @param Player $player * @return bool */ public function command_GetPlanets(array $chatCallback, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); return false; } if (!$this->maniaControl->client->query('GetServerPlanets')) { @@ -220,8 +220,8 @@ class DonationPlugin extends Plugin implements CallbackListener, CommandListener * @return boolean */ private function sendDonateUsageExample(Player $player) { - $colorCode = '$f80'; - return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/donate 100'", $player->login); + $message = "Usage Example: '/donate 100'"; + return $this->maniaControl->chat->sendChat($message, $player->login); } /** @@ -231,8 +231,8 @@ class DonationPlugin extends Plugin implements CallbackListener, CommandListener * @return boolean */ private function sendPayUsageExample(Player $player) { - $colorCode = '$f80'; - return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/pay 100 login'", $player->login); + $message = "Usage Example: '/pay 100 login'"; + return $this->maniaControl->chat->sendChat($message, $player->login); } }