2013-12-03 18:03:16 +01:00
< ? php
namespace ManiaControl\Admin ;
use ManiaControl\ManiaControl ;
use ManiaControl\Commands\CommandListener ;
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
*
* @ author steeffeen & kremsy
2014-04-12 12:14:37 +02:00
* @ copyright ManiaControl 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
*
* @ param ManiaControl $maniaControl
*/
public function __construct ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
// Register for commands
2014-05-02 16:13:45 +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
*
* @ param array $chatCallback
* @ param Player $player
*/
public function command_AddSuperAdmin ( array $chatCallback , Player $player ) {
if ( ! AuthenticationManager :: checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_MASTERADMIN )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
return ;
}
$text = $chatCallback [ 1 ][ 2 ];
$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 );
}
/**
* Handle //addadmin command
*
* @ param array $chatCallback
* @ param Player $player
*/
public function command_AddAdmin ( array $chatCallback , Player $player ) {
if ( ! AuthenticationManager :: checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_SUPERADMIN )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
return ;
}
$text = $chatCallback [ 1 ][ 2 ];
$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 );
}
/**
2013-12-31 17:17:11 +01:00
* Handle //addmod command
2013-12-03 18:03:16 +01:00
*
* @ param array $chatCallback
* @ param Player $player
*/
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 ;
}
$text = $chatCallback [ 1 ][ 2 ];
$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 //addsuperadmin command
*
* @ param Player $player
* @ return bool
*/
private function sendAddSuperAdminUsageInfo ( Player $player ) {
$message = " Usage Example: '//addsuperadmin 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
}
/**
* Send usage example for //addadmin command
*
* @ param Player $player
* @ return bool
*/
private function sendAddAdminUsageInfo ( Player $player ) {
$message = " Usage Example: '//addadmin 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
}
/**
* Send usage example for //addop command
*
* @ param Player $player
* @ 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
}
}