2013-11-09 17:24:03 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
2013-11-13 01:43:12 +01:00
|
|
|
|
namespace ManiaControl\Admin;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
2013-11-13 01:43:12 +01:00
|
|
|
|
use ManiaControl\FileUtil;
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
use ManiaControl\Commands\CommandListener;
|
2013-11-12 15:48:25 +01:00
|
|
|
|
use ManiaControl\Players\Player;
|
2013-11-12 19:33:25 +01:00
|
|
|
|
use ManiaControl\Players\PlayerManager;
|
2013-11-12 15:48:25 +01:00
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
|
/**
|
2013-11-13 01:43:12 +01:00
|
|
|
|
* Class managing authentication levels
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*/
|
2013-11-13 01:43:12 +01:00
|
|
|
|
class AuthenticationManager implements CommandListener {
|
2013-11-09 17:24:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* Constants
|
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
const AUTH_LEVEL_PLAYER = 0;
|
|
|
|
|
const AUTH_LEVEL_OPERATOR = 1;
|
|
|
|
|
const AUTH_LEVEL_ADMIN = 2;
|
|
|
|
|
const AUTH_LEVEL_SUPERADMIN = 3;
|
|
|
|
|
const AUTH_LEVEL_XSUPERADMIN = 4;
|
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* Private properties
|
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
private $maniaControl = null;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct authentication manager
|
2013-11-10 19:30:14 +01:00
|
|
|
|
*
|
2013-11-13 01:43:12 +01:00
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
$this->loadConfig();
|
2013-11-13 01:43:12 +01:00
|
|
|
|
|
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('/addadmin', $this, 'command_AddAdmin');
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* Load config and initialize strong superadmins
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
private function loadConfig() {
|
|
|
|
|
$config = FileUtil::loadConfig('authentication.xml');
|
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
|
|
|
|
|
// Remove all XSuperadmins
|
2013-11-13 01:43:12 +01:00
|
|
|
|
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
|
2013-11-10 19:30:14 +01:00
|
|
|
|
SET `authLevel` = ?
|
|
|
|
|
WHERE `authLevel` = ?;";
|
|
|
|
|
$adminStatement = $mysqli->prepare($adminQuery);
|
|
|
|
|
if ($mysqli->error) {
|
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$adminLevel = self::AUTH_LEVEL_SUPERADMIN;
|
|
|
|
|
$xAdminLevel = self::AUTH_LEVEL_XSUPERADMIN;
|
|
|
|
|
$adminStatement->bind_param('ii', $adminLevel, $xAdminLevel);
|
|
|
|
|
$adminStatement->execute();
|
|
|
|
|
if ($adminStatement->error) {
|
|
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
|
}
|
|
|
|
|
$adminStatement->close();
|
|
|
|
|
|
|
|
|
|
// Set XSuperAdmins
|
|
|
|
|
$xAdmins = $config->xsuperadmins->xpath('login');
|
2013-11-13 01:43:12 +01:00
|
|
|
|
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
|
2013-11-10 19:30:14 +01:00
|
|
|
|
`login`,
|
|
|
|
|
`authLevel`
|
|
|
|
|
) VALUES (
|
|
|
|
|
?, ?
|
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
|
`authLevel` = VALUES(`authLevel`);";
|
|
|
|
|
$adminStatement = $mysqli->prepare($adminQuery);
|
|
|
|
|
if ($mysqli->error) {
|
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$adminStatement->bind_param('si', $login, $xAdminLevel);
|
|
|
|
|
$success = true;
|
|
|
|
|
foreach ($xAdmins as $xAdmin) {
|
|
|
|
|
$login = (string) $xAdmin;
|
|
|
|
|
$adminStatement->execute();
|
|
|
|
|
if ($adminStatement->error) {
|
|
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
|
$success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$adminStatement->close();
|
|
|
|
|
return $success;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* Grant the auth level to the player
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* @param Player $player
|
|
|
|
|
* @param int $authLevel
|
2013-11-09 17:24:03 +01:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
public function grantAuthLevel(Player $player, $authLevel) {
|
2013-11-13 01:43:12 +01:00
|
|
|
|
if (!$player || !is_int($authLevel) || $authLevel >= self::AUTH_LEVEL_XSUPERADMIN) {
|
2013-11-09 17:24:03 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-11-12 19:33:25 +01:00
|
|
|
|
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
|
2013-11-10 19:30:14 +01:00
|
|
|
|
`login`,
|
|
|
|
|
`authLevel`
|
|
|
|
|
) VALUES (
|
|
|
|
|
?, ?
|
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
|
`authLevel` = VALUES(`authLevel`);";
|
|
|
|
|
$authStatement = $mysqli->prepare($authQuery);
|
|
|
|
|
if ($mysqli->error) {
|
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
2013-11-09 17:24:03 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
|
$authStatement->bind_param('si', $player->login, $authLevel);
|
|
|
|
|
$authStatement->execute();
|
|
|
|
|
if ($authStatement->error) {
|
|
|
|
|
trigger_error($authStatement->error);
|
|
|
|
|
$authStatement->close();
|
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
|
$authStatement->close();
|
|
|
|
|
return $success;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* Sends an error message to the login
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $login
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
public function sendNotAllowed(Player $player) {
|
|
|
|
|
if (!$player) {
|
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
|
return $this->maniaControl->chat->sendError('You do not have the required rights to perform this command!', $player->login);
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-13 01:43:12 +01:00
|
|
|
|
/**
|
|
|
|
|
* Handle //addadmin command
|
|
|
|
|
*
|
|
|
|
|
* @param array $chatCallback
|
|
|
|
|
* @param
|
|
|
|
|
* \ManiaControl\Players\Player
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function command_AddAdmin(array $chatCallback, Player $player) {
|
|
|
|
|
var_dump($chatCallback);
|
|
|
|
|
if (!$this->checkRight($player, self::AUTH_LEVEL_SUPERADMIN)) {
|
|
|
|
|
$this->sendNotAllowed($player);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$text = $chatCallback[1][2];
|
|
|
|
|
$commandParts = explode(' ', $text);
|
|
|
|
|
if (!array_key_exists(1, $commandParts)) {
|
|
|
|
|
$this->sendAddAdminUsageInfo($player);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
|
/**
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* Check if the player has enough rights
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*
|
2013-11-12 15:48:25 +01:00
|
|
|
|
* @param \ManiaControl\Players\Player $login
|
2013-11-10 19:30:14 +01:00
|
|
|
|
* @param int $neededAuthLevel
|
|
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
|
public static function checkRight(Player $player, $neededAuthLevel) {
|
|
|
|
|
if (!$player) {
|
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
|
return ($player->authLevel >= $neededAuthLevel);
|
2013-11-09 17:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|