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 >
* @ copyright 2014 ManiaControl Team
* @ 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
*/
private $maniaControl = null ;
/**
* Create a new AuthCommands instance
*
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
2013-12-03 18:03:16 +01:00
// Register for commands
2014-05-02 17:31:10 +02:00
$this -> maniaControl -> commandManager -> registerCommandListener ( 'addsuperadmin' , $this , 'command_AddSuperAdmin' , true , 'Add Player to the AdminList as SuperAdmin.' );
$this -> maniaControl -> commandManager -> registerCommandListener ( 'addadmin' , $this , 'command_AddAdmin' , true , 'Add Player to the AdminList as Admin.' );
$this -> maniaControl -> commandManager -> registerCommandListener ( 'addmod' , $this , 'command_AddModerator' , true , 'Add Player to the AdminList as Moderator.' );
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 )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
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 ;
}
$target = $this -> maniaControl -> playerManager -> getPlayer ( $commandParts [ 1 ]);
if ( ! $target ) {
$this -> maniaControl -> chat -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player -> login );
return ;
}
2014-03-10 14:01:19 +01:00
$success = $this -> maniaControl -> authenticationManager -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_SUPERADMIN );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2013-12-09 13:45:58 +01:00
$this -> maniaControl -> chat -> sendError ( 'Error occurred.' , $player -> login );
2013-12-03 18:03:16 +01:00
return ;
}
$message = '$<' . $player -> nickname . '$> added $<' . $target -> nickname . '$> as SuperAdmin!' ;
$this -> maniaControl -> chat -> 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' " ;
return $this -> maniaControl -> chat -> sendUsageInfo ( $message , $player -> login );
}
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 )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
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 ;
}
$target = $this -> maniaControl -> playerManager -> getPlayer ( $commandParts [ 1 ]);
if ( ! $target ) {
$this -> maniaControl -> chat -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player -> login );
return ;
}
2014-03-10 14:01:19 +01:00
$success = $this -> maniaControl -> authenticationManager -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_ADMIN );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2013-12-09 13:45:58 +01:00
$this -> maniaControl -> chat -> sendError ( 'Error occurred.' , $player -> login );
2013-12-03 18:03:16 +01:00
return ;
}
$message = '$<' . $player -> nickname . '$> added $<' . $target -> nickname . '$> as Admin!' ;
$this -> maniaControl -> chat -> 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' " ;
return $this -> maniaControl -> chat -> sendUsageInfo ( $message , $player -> login );
}
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 )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
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 ;
}
$target = $this -> maniaControl -> playerManager -> getPlayer ( $commandParts [ 1 ]);
if ( ! $target ) {
$this -> maniaControl -> chat -> sendError ( " Player ' { $commandParts [ 1 ] } ' not found! " , $player -> login );
return ;
}
2014-03-10 14:01:19 +01:00
$success = $this -> maniaControl -> authenticationManager -> grantAuthLevel ( $target , AuthenticationManager :: AUTH_LEVEL_MODERATOR );
2013-12-03 18:03:16 +01:00
if ( ! $success ) {
2013-12-09 13:45:58 +01:00
$this -> maniaControl -> chat -> sendError ( 'Error occurred.' , $player -> login );
2013-12-03 18:03:16 +01:00
return ;
}
2013-12-31 17:17:11 +01:00
$message = '$<' . $player -> nickname . '$> added $<' . $target -> nickname . '$> as Moderator!' ;
2013-12-03 18:03:16 +01:00
$this -> maniaControl -> chat -> sendSuccess ( $message );
}
/**
* Send usage example for //addop command
*
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' " ;
2013-12-09 13:45:58 +01:00
return $this -> maniaControl -> chat -> sendUsageInfo ( $message , $player -> login );
2013-12-03 18:03:16 +01:00
}
}