2013-12-03 18:03:16 +01:00
< ? php
namespace ManiaControl\Admin ;
use ManiaControl\Commands\CommandListener ;
2017-03-26 19:44:55 +02:00
use ManiaControl\General\UsageInformationAble ;
use ManiaControl\General\UsageInformationTrait ;
2014-05-02 17:31:10 +02:00
use ManiaControl\ManiaControl ;
2013-12-03 18:03:16 +01:00
use ManiaControl\Players\Player ;
2013-12-09 13:45:58 +01:00
/**
2014-04-12 12:14:37 +02:00
* Class offering Commands to grant Authorizations to Players
2013-12-09 13:45:58 +01:00
*
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
2013-12-09 13:45:58 +01:00
*/
2017-03-26 19:44:55 +02:00
class AuthCommands implements CommandListener , UsageInformationAble {
use UsageInformationTrait ;
2014-04-12 12:14:37 +02:00
/*
2013-12-03 18:03:16 +01:00
* Private properties
*/
2014-08-02 22:31:46 +02:00
/** @var ManiaControl $maniaControl */
2013-12-03 18:03:16 +01:00
private $maniaControl = null ;
/**
2014-08-03 01:34:18 +02:00
* Construct a new AuthCommands instance
2013-12-03 18:03:16 +01:00
*
2014-05-02 17:31:10 +02:00
* @ param ManiaControl $maniaControl
2013-12-03 18:03:16 +01:00
*/
public function __construct ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
2014-05-02 17:31:10 +02:00
2014-08-03 01:34:18 +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.' );
2013-12-03 18:03:16 +01:00
}
/**
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
2013-12-03 18:03:16 +01:00
*/
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 );
2013-12-03 18:03:16 +01:00
return ;
}
2020-05-26 18:15:18 +02:00
2014-05-02 17:31:10 +02:00
$text = $chatCallback [ 1 ][ 2 ];
2013-12-03 18:03:16 +01:00
$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 );
2013-12-03 18:03:16 +01:00
return ;
}
2020-05-26 18:15:18 +02:00
2014-08-13 11:05:52 +02:00
$target = $this -> maniaControl -> getPlayerManager () -> getPlayer ( $commandParts [ 1 ]);
2013-12-03 18:03:16 +01:00
if ( ! $target ) {
2024-08-28 18:26:55 +02:00
// 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 ];
2013-12-03 18:03:16 +01:00
}
2020-05-26 18:15:18 +02:00
$success = $this -> maniaControl -> getAuthenticationManager () -> grantAuthLevel ( $target , $targetAuthLevel );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2020-05-26 18:15:18 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Error occurred!' , $player );
2013-12-03 18:03:16 +01:00
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 );
2013-12-03 18:03:16 +01:00
}
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
}
2013-12-03 18:03:16 +01: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
2013-12-03 18:03:16 +01:00
*/
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
}
2013-12-03 18:03:16 +01:00
/**
2013-12-31 17:17:11 +01:00
* Handle //addmod command
2013-12-03 18:03:16 +01:00
*
2017-04-07 23:38:18 +02:00
* @ internal
2014-05-02 17:31:10 +02:00
* @ param array $chatCallback
* @ param Player $player
2013-12-03 18:03:16 +01:00
*/
2013-12-31 17:17:11 +01:00
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 );
2013-12-03 18:03:16 +01:00
}
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
}
2013-12-03 18:03:16 +01:00
}