TrackManiaControl/core/Admin/AuthCommands.php

185 lines
5.8 KiB
PHP
Raw Permalink 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>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 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.');
}
/**
2020-05-26 18:15:18 +02:00
* Handle all //add commands
2017-04-07 23:38:18 +02:00
* @internal
2020-05-26 18:15:18 +02:00
* @param array $chatCallback
2014-05-02 17:31:10 +02:00
* @param Player $player
2020-05-26 18:15:18 +02:00
* @param string $targetAuthLevel
*/
2020-05-26 18:15:18 +02:00
private function command_Add(array $chatCallback, Player $player, $targetAuthLevel) {
// $player needs to be at least one AuthLevel higher as the one to be granted
if (!AuthenticationManager::checkRight($player, $targetAuthLevel+1)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
2020-05-26 18:15:18 +02:00
2014-05-02 17:31:10 +02:00
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
2024-11-13 16:21:39 +01:00
if (!array_key_exists(1, $commandParts) || $commandParts[1] === "") {
2020-05-26 18:15:18 +02:00
$message = $this->maniaControl->getChat()->formatMessage(
'Usage Example: %s %s',
$commandParts[0],
'login'
);
$this->maniaControl->getChat()->sendUsageInfo($message, $player);
return;
}
2020-05-26 18:15:18 +02:00
2014-08-13 11:05:52 +02:00
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
// It's not needed to save the player, it will be done by the grantAuthLevel function
$target = new Player($this->maniaControl, false);
$target->login = $commandParts[1];
$target->nickname = $commandParts[1];
}
2020-05-26 18:15:18 +02:00
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, $targetAuthLevel);
if (!$success) {
2020-05-26 18:15:18 +02:00
$this->maniaControl->getChat()->sendError('Error occurred!', $player);
return;
}
2020-05-26 18:15:18 +02:00
$authName = AuthenticationManager::getAuthLevelName($targetAuthLevel);
$message = $this->maniaControl->getChat()->formatMessage(
"%s added %s as {$authName}.",
$player,
$target
);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
}
2014-05-02 17:31:10 +02:00
/**
2020-05-26 18:15:18 +02:00
* Handle //addsuperadmin command
2014-05-02 17:31:10 +02:00
*
2020-05-26 18:15:18 +02:00
* @internal
* @param array $chatCallback
2014-05-02 17:31:10 +02:00
* @param Player $player
*/
2020-05-26 18:15:18 +02:00
public function command_AddSuperAdmin(array $chatCallback, Player $player) {
$this->command_Add($chatCallback, $player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
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) {
2020-05-26 18:15:18 +02:00
$this->command_Add($chatCallback, $player, AuthenticationManager::AUTH_LEVEL_ADMIN);
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) {
2020-05-26 18:15:18 +02:00
$this->command_Add($chatCallback, $player, AuthenticationManager::AUTH_LEVEL_MODERATOR);
}
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) {
$text = $chatCallback[1][2];
$commandParts = explode(' ', $text);
if (!array_key_exists(1, $commandParts)) {
2020-05-26 18:15:18 +02:00
$message = $this->maniaControl->getChat()->formatMessage(
'Usage Example: %s %s',
$commandParts[0],
'login'
);
$this->maniaControl->getChat()->sendUsageInfo($message, $player);
2015-06-19 19:07:39 +02:00
return;
}
2020-05-26 18:15:18 +02:00
2015-06-19 19:07:39 +02:00
$target = $this->maniaControl->getPlayerManager()->getPlayer($commandParts[1]);
if (!$target) {
2020-05-26 18:15:18 +02:00
$message = $this->maniaControl->getChat()->formatMessage(
'Player %s not found!',
$commandParts[1]
);
$this->maniaControl->getChat()->sendError($message, $player);
return;
}
2023-08-14 18:16:58 +02:00
if ($player !== $target && $target->authLevel >= AuthenticationManager::AUTH_LEVEL_MASTERADMIN) {
2020-05-26 18:15:18 +02:00
$this->maniaControl->getChat()->sendError('You cannot remove rights of a MasterAdmin!', $player);
return;
}
if ($target->authLevel <= AuthenticationManager::AUTH_LEVEL_PLAYER) {
$this->maniaControl->getChat()->sendError('Cannot remove rights of a player!', $player);
2015-06-19 19:07:39 +02:00
return;
}
2023-08-14 18:16:58 +02:00
if ($player !== $target && $player->authLevel <= $target->authLevel) {
2020-05-26 18:15:18 +02:00
$this->maniaControl->getChat()->sendError('You cannot remove rights of a higher privileged player!', $player);
2015-06-19 19:07:39 +02:00
return;
}
2020-05-26 18:15:18 +02:00
$targetAuthName = $target->getAuthLevelName();
2015-06-19 19:07:39 +02:00
$success = $this->maniaControl->getAuthenticationManager()->grantAuthLevel($target, AuthenticationManager::AUTH_LEVEL_PLAYER);
if (!$success) {
2020-05-26 18:15:18 +02:00
$this->maniaControl->getChat()->sendError('Error occurred!', $player);
2015-06-19 19:07:39 +02:00
return;
}
2020-05-26 18:15:18 +02:00
$message = $this->maniaControl->getChat()->formatMessage(
"%s removed %s from {$targetAuthName}s.",
$player,
$target
);
$this->maniaControl->getChat()->sendSuccess($message);
2015-06-19 19:07:39 +02:00
}
}