2013-12-03 18:03:16 +01:00
< ? php
namespace ManiaControl\Admin ;
use ManiaControl\Commands\CommandListener ;
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 >
2016-05-04 09:57:31 +02:00
* @ copyright 2014 - 2016 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
*/
2013-12-03 18:03:16 +01:00
class AuthCommands implements CommandListener {
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
}
/**
* Handle //addsuperadmin command
*
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_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 );
2013-12-03 18:03:16 +01:00
return ;
}
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 )) {
$this -> sendAddSuperAdminUsageInfo ( $player );
return ;
}
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 ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-13 11:05:52 +02:00
$success = $this -> maniaControl -> getAuthenticationManager () -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_SUPERADMIN );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Error occurred.' , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-03 01:34:18 +02:00
$message = $player -> getEscapedNickname () . ' added ' . $target -> getEscapedNickname () . ' as SuperAdmin!' ;
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
/**
* 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
}
2013-12-03 18:03:16 +01:00
/**
* Handle //addadmin command
*
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 ) {
if ( ! AuthenticationManager :: checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_SUPERADMIN )) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getAuthenticationManager () -> sendNotAllowed ( $player );
2013-12-03 18:03:16 +01:00
return ;
}
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 )) {
$this -> sendAddAdminUsageInfo ( $player );
return ;
}
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 ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-13 11:05:52 +02:00
$success = $this -> maniaControl -> getAuthenticationManager () -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_ADMIN );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Error occurred.' , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-03 01:34:18 +02:00
$message = $player -> getEscapedNickname () . ' added ' . $target -> getEscapedNickname () . ' as Admin!' ;
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
/**
* 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
}
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
*
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 ) {
2013-12-03 18:03:16 +01:00
if ( ! AuthenticationManager :: checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_ADMIN )) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getAuthenticationManager () -> sendNotAllowed ( $player );
2013-12-03 18:03:16 +01:00
return ;
}
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 )) {
2013-12-31 17:17:11 +01:00
$this -> sendAddModeratorUsageInfo ( $player );
2013-12-03 18:03:16 +01:00
return ;
}
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 ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-13 11:05:52 +02:00
$success = $this -> maniaControl -> getAuthenticationManager () -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_MODERATOR );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Error occurred.' , $player );
2013-12-03 18:03:16 +01:00
return ;
}
2014-08-03 01:34:18 +02:00
$message = $player -> getEscapedNickname () . ' added ' . $target -> getEscapedNickname () . ' as Moderator!' ;
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getChat () -> sendSuccess ( $message );
2013-12-03 18:03:16 +01:00
}
/**
2014-05-20 14:59:17 +02:00
* Send usage example for //addmod command
2013-12-03 18:03:16 +01:00
*
2014-05-02 17:31:10 +02:00
* @ param Player $player
2013-12-03 18:03:16 +01:00
* @ return bool
*/
2013-12-31 17:17:11 +01:00
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 );
2013-12-03 18:03:16 +01:00
}
2015-06-19 19:07:39 +02:00
/**
* Handle //removerights command
*
* @ 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 );
}
2013-12-03 18:03:16 +01:00
}