TrackManiaControl/core/Admin/AuthCommands.php

222 lines
7.5 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Admin;
use ManiaControl\Commands\CommandListener;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
2014-05-02 17:31:10 +02:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
/**
* Class offering Commands to grant Authorizations to Players
*
2014-05-02 17:31:10 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2017-02-04 11:49:23 +01:00
* @copyright 2014-2017 ManiaControl Team
2014-05-02 17:31:10 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class AuthCommands implements CommandListener, UsageInformationAble {
use UsageInformationTrait;
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/**
* Construct a new AuthCommands instance
*
2014-05-02 17:31:10 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-05-02 17:31:10 +02:00
// Commands
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCommandManager()->registerCommandListener('addsuperadmin', $this, 'command_AddSuperAdmin', true, 'Add Player to the AdminList as SuperAdmin.');
$this->maniaControl->getCommandManager()->registerCommandListener('addadmin', $this, 'command_AddAdmin', true, 'Add Player to the AdminList as Admin.');
$this->maniaControl->getCommandManager()->registerCommandListener('addmod', $this, 'command_AddModerator', true, 'Add Player to the AdminList as Moderator.');
2015-06-19 19:07:39 +02:00
$this->maniaControl->getCommandManager()->registerCommandListener('removerights', $this, 'command_RemoveRights', true, 'Remove Player from the AdminList.');
}
/**
* Handle //addsuperadmin command
*
2017-04-07 23:38:18 +02:00
* @internal
2014-05-02 17:31:10 +02:00
* @param array $chatCallback
* @param Player $player
*/
public function command_AddSuperAdmin(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
2014-05-02 17:31:10 +02:00
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
if (!array_key_exists(1, $commandParts)) {
$this->sendAddSuperAdminUsageInfo($player);
return;
}
2014-08-13 11:05:52 +02:00
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError("Player '{$commandParts[1]}' not found!", $player);
return;
}
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
if (!$success) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Error occurred.', $player);
return;
}
$message = $player->getEscapedNickname() . ' added ' . $target->getEscapedNickname() . ' as SuperAdmin!';
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
}
2014-05-02 17:31:10 +02:00
/**
* Send usage example for //addsuperadmin command
*
* @param Player $player
* @return bool
*/
private function sendAddSuperAdminUsageInfo(Player $player) {
$message = "Usage Example: '//addsuperadmin login'";
2014-08-13 11:05:52 +02:00
return $this->maniaControl->getChat()->sendUsageInfo($message, $player);
2014-05-02 17:31:10 +02:00
}
/**
* Handle //addadmin command
*
2017-04-07 23:38:18 +02:00
* @internal
2014-05-02 17:31:10 +02:00
* @param array $chatCallback
* @param Player $player
*/
public function command_AddAdmin(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
2014-05-02 17:31:10 +02:00
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
if (!array_key_exists(1, $commandParts)) {
$this->sendAddAdminUsageInfo($player);
return;
}
2014-08-13 11:05:52 +02:00
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError("Player '{$commandParts[1]}' not found!", $player);
return;
}
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, AuthenticationManager::AUTH_LEVEL_ADMIN);
if (!$success) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Error occurred.', $player);
return;
}
$message = $player->getEscapedNickname() . ' added ' . $target->getEscapedNickname() . ' as Admin!';
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
}
2014-05-02 17:31:10 +02:00
/**
* Send usage example for //addadmin command
*
* @param Player $player
* @return bool
*/
private function sendAddAdminUsageInfo(Player $player) {
$message = "Usage Example: '//addadmin login'";
2014-08-13 11:05:52 +02:00
return $this->maniaControl->getChat()->sendUsageInfo($message, $player);
2014-05-02 17:31:10 +02:00
}
/**
* Handle //addmod command
*
2017-04-07 23:38:18 +02:00
* @internal
2014-05-02 17:31:10 +02:00
* @param array $chatCallback
* @param Player $player
*/
public function command_AddModerator(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
2014-05-02 17:31:10 +02:00
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
if (!array_key_exists(1, $commandParts)) {
$this->sendAddModeratorUsageInfo($player);
return;
}
2014-08-13 11:05:52 +02:00
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError("Player '{$commandParts[1]}' not found!", $player);
return;
}
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, AuthenticationManager::AUTH_LEVEL_MODERATOR);
if (!$success) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Error occurred.', $player);
return;
}
$message = $player->getEscapedNickname() . ' added ' . $target->getEscapedNickname() . ' as Moderator!';
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
}
/**
* Send usage example for //addmod command
*
2014-05-02 17:31:10 +02:00
* @param Player $player
* @return bool
*/
private function sendAddModeratorUsageInfo(Player $player) {
$message = "Usage Example: '//addmod login'";
2014-08-13 11:05:52 +02:00
return $this->maniaControl->getChat()->sendUsageInfo($message, $player);
}
2015-06-19 19:07:39 +02:00
/**
* Handle //removerights command
*
2017-04-07 23:38:18 +02:00
* @internal
2015-06-19 19:07:39 +02:00
* @param array $chatCallback
* @param Player $player
*/
public function command_RemoveRights(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
if (!array_key_exists(1, $commandParts)) {
$this->sendRemoveRightsUsageInfo($player);
return;
}
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
$this->maniaControl->getChat()->sendError("Player '{$commandParts[1]}' not found!", $player);
return;
}
if ($target->authLevel == AuthenticationManager::AUTH_LEVEL_MASTERADMIN) {
$this->maniaControl->getChat()->sendError("You can't remove an MasterAdmin from the Adminlists", $player);
return;
}
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, AuthenticationManager::AUTH_LEVEL_PLAYER);
if (!$success) {
$this->maniaControl->getChat()->sendError('Error occurred.', $player);
return;
}
$message = $player->getEscapedNickname() . ' removed ' . $target->getEscapedNickname() . ' from the Adminlists!';
$this->maniaControl->getChat()->sendSuccess($message);
}
/**
* Send usage example for //removerights command
*
* @param Player $player
* @return bool
*/
private function sendRemoveRightsUsageInfo(Player $player) {
$message = "Usage Example: '//addadmin login'";
return $this->maniaControl->getChat()->sendUsageInfo($message, $player);
}
}