diff --git a/application/core/Admin/AuthCommands.php b/application/core/Admin/AuthCommands.php new file mode 100644 index 00000000..5bf4a73d --- /dev/null +++ b/application/core/Admin/AuthCommands.php @@ -0,0 +1,156 @@ +maniaControl = $maniaControl; + + // Register for commands + $this->maniaControl->commandManager->registerCommandListener('/addsuperadmin', $this, 'command_AddSuperAdmin'); + $this->maniaControl->commandManager->registerCommandListener('/addadmin', $this, 'command_AddAdmin'); + $this->maniaControl->commandManager->registerCommandListener('/addop', $this, 'command_AddOperator'); + } + + /** + * Handle //addsuperadmin command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_AddSuperAdmin(array $chatCallback, Player $player) { + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $text = $chatCallback[1][2]; + $commandParts = explode(' ', $text); + if (!array_key_exists(1, $commandParts)) { + $this->sendAddSuperAdminUsageInfo($player); + return; + } + $target = $this->maniaControl->playerManager->getPlayer($commandParts[1]); + if (!$target) { + $this->maniaControl->chat->sendError("Player '{$commandParts[1]}' not found!", $player->login); + return; + } + $success = $this->maniaControl->authenticationManager->grantAuthLevel($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured.', $player->login); + return; + } + $message = '$<' . $player->nickname . '$> added $<' . $target->nickname . '$> as SuperAdmin!'; + $this->maniaControl->chat->sendSuccess($message); + } + + /** + * Handle //addadmin command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_AddAdmin(array $chatCallback, Player $player) { + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $text = $chatCallback[1][2]; + $commandParts = explode(' ', $text); + if (!array_key_exists(1, $commandParts)) { + $this->sendAddAdminUsageInfo($player); + return; + } + $target = $this->maniaControl->playerManager->getPlayer($commandParts[1]); + if (!$target) { + $this->maniaControl->chat->sendError("Player '{$commandParts[1]}' not found!", $player->login); + return; + } + $success = $this->maniaControl->authenticationManager->grantAuthLevel($player, AuthenticationManager::AUTH_LEVEL_ADMIN); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured.', $player->login); + return; + } + $message = '$<' . $player->nickname . '$> added $<' . $target->nickname . '$> as Admin!'; + $this->maniaControl->chat->sendSuccess($message); + } + + /** + * Handle //addop command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_AddOperator(array $chatCallback, Player $player) { + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $text = $chatCallback[1][2]; + $commandParts = explode(' ', $text); + if (!array_key_exists(1, $commandParts)) { + $this->sendAddOperatorUsageInfo($player); + return; + } + $target = $this->maniaControl->playerManager->getPlayer($commandParts[1]); + if (!$target) { + $this->maniaControl->chat->sendError("Player '{$commandParts[1]}' not found!", $player->login); + return; + } + $success = $this->maniaControl->authenticationManager->grantAuthLevel($player, AuthenticationManager::AUTH_LEVEL_OPERATOR); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured.', $player->login); + return; + } + $message = '$<' . $player->nickname . '$> added $<' . $target->nickname . '$> as Operator!'; + $this->maniaControl->chat->sendSuccess($message); + } + + /** + * Send usage example for //addsuperadmin command + * + * @param Player $player + * @return bool + */ + private function sendAddSuperAdminUsageInfo(Player $player) { + $message = "Usage Example: '//addsuperadmin login'"; + return $this->maniaControl->chat->sendUsageInfo($message); + } + + /** + * 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); + } + + /** + * Send usage example for //addop command + * + * @param Player $player + * @return bool + */ + private function sendAddOperatorUsageInfo(Player $player) { + $message = "Usage Example: '//addop login'"; + return $this->maniaControl->chat->sendUsageInfo($message); + } +} + +?> diff --git a/application/core/Admin/AuthenticationManager.php b/application/core/Admin/AuthenticationManager.php index 0843c0a6..3dac54a9 100644 --- a/application/core/Admin/AuthenticationManager.php +++ b/application/core/Admin/AuthenticationManager.php @@ -4,16 +4,17 @@ namespace ManiaControl\Admin; use ManiaControl\FileUtil; use ManiaControl\ManiaControl; -use ManiaControl\Commands\CommandListener; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; +require_once __DIR__ . '/AuthCommands.php'; + /** * Class managing authentication levels * * @author steeffeen & kremsy */ -class AuthenticationManager implements CommandListener { +class AuthenticationManager { /** * Constants */ @@ -27,6 +28,7 @@ class AuthenticationManager implements CommandListener { * Private properties */ private $maniaControl = null; + private $authCommands = null; /** * Construct authentication manager @@ -37,7 +39,7 @@ class AuthenticationManager implements CommandListener { $this->maniaControl = $maniaControl; $this->loadConfig(); - $this->maniaControl->commandManager->registerCommandListener('/addadmin', $this, 'command_AddAdmin'); + $this->authCommands = new AuthCommands($maniaControl); } /** @@ -103,7 +105,7 @@ class AuthenticationManager implements CommandListener { * @return bool */ public function grantAuthLevel(Player $player, $authLevel) { - if (!$player || !is_int($authLevel) || $authLevel  >= self::AUTH_LEVEL_MASTERADMIN) { + if (!$player || !is_int($authLevel) || $authLevel >= self::AUTH_LEVEL_MASTERADMIN) { return false; } $mysqli = $this->maniaControl->database->mysqli; @@ -120,7 +122,7 @@ class AuthenticationManager implements CommandListener { return false; } $authStatement->bind_param('si', $player->login, $authLevel); - $authStatement->execute(); + $success = $authStatement->execute(); if ($authStatement->error) { trigger_error($authStatement->error); $authStatement->close(); @@ -140,41 +142,7 @@ class AuthenticationManager implements CommandListener { if (!$player) { return false; } - 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); + return $this->maniaControl->chat->sendError('You do not have the required Rights to perform this Command!', $player->login); } /** diff --git a/application/core/chat.php b/application/core/chat.php index 6ae86131..ae0e34cd 100644 --- a/application/core/chat.php +++ b/application/core/chat.php @@ -8,6 +8,14 @@ namespace ManiaControl; * @author steeffeen & kremsy */ class Chat { + /** + * Constants + */ + const SETTING_PREFIX = 'Messages Prefix'; + const SETTING_FORMAT_INFORMATION = 'Information Format'; + const SETTING_FORMAT_SUCCESS = 'Success Format'; + const SETTING_FORMAT_ERROR = 'Error Format'; + const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format'; /** * Private properties @@ -21,6 +29,13 @@ class Chat { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; + + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_PREFIX, 'MC» '); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_INFORMATION, '$fff'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_SUCCESS, '$0f0'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_ERROR, '$f00'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_USAGEINFO, '$f80'); } /** @@ -34,7 +49,7 @@ class Chat { return $prefix; } if ($prefix === true) { - return $this->maniaControl->settingManager->getSetting($this, 'DefaultPrefix', 'ManiaControl>'); + return $this->maniaControl->settingManager->getSetting($this, self::SETTING_PREFIX); } return ''; } @@ -47,12 +62,12 @@ class Chat { * @param string|bool $prefix * @return bool */ - public function sendChat($message, $login = null, $prefix = false) { + public function sendChat($message, $login = null, $prefix = true) { if (!$this->maniaControl->client) { return false; } $client = $this->maniaControl->client; - $chatMessage = '$z' . $this->getPrefix($prefix) . $message . '$z'; + $chatMessage = '$z$<' . $this->getPrefix($prefix) . $message . '$>$z'; if ($login === null) { return $client->query('ChatSendServerMessage', $chatMessage); } @@ -67,8 +82,8 @@ class Chat { * @param string|bool $prefix * @return bool */ - public function sendInformation($message, $login = null, $prefix = false) { - $format = $this->maniaControl->settingManager->getSetting($this, 'InformationFormat', '$fff'); + public function sendInformation($message, $login = null, $prefix = true) { + $format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_INFORMATION); return $this->sendChat($format . $message, $login); } @@ -80,8 +95,8 @@ class Chat { * @param string|bool $prefix * @return bool */ - public function sendSuccess($message, $login = null, $prefix = false) { - $format = $this->maniaControl->settingManager->getSetting($this, 'SuccessFormat', '$0f0'); + public function sendSuccess($message, $login = null, $prefix = true) { + $format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_SUCCESS); return $this->sendChat($format . $message, $login); } @@ -93,8 +108,8 @@ class Chat { * @param string|bool $prefix * @return bool */ - public function sendError($message, $login = null, $prefix = false) { - $format = $this->maniaControl->settingManager->getSetting($this, 'ErrorFormat', '$f00'); + public function sendError($message, $login = null, $prefix = true) { + $format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_ERROR); return $this->sendChat($format . $message, $login); } @@ -107,7 +122,7 @@ class Chat { * @return bool */ public function sendUsageInfo($message, $login = null, $prefix = false) { - $format = $this->maniaControl->settingManager->getSetting($this, 'UsageInfoFormat', '$f80'); + $format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_USAGEINFO); return $this->sendChat($format . $message, $login); } }