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;
|
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-12-03 18:03:16 +01:00
|
|
|
require_once __DIR__ . '/AuthCommands.php';
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +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-12-03 18:03:16 +01:00
|
|
|
class AuthenticationManager {
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
const AUTH_LEVEL_PLAYER = 0;
|
2013-12-31 17:17:11 +01:00
|
|
|
const AUTH_LEVEL_MODERATOR = 1;
|
2013-11-10 19:30:14 +01:00
|
|
|
const AUTH_LEVEL_ADMIN = 2;
|
|
|
|
const AUTH_LEVEL_SUPERADMIN = 3;
|
2013-11-24 23:55:54 +01:00
|
|
|
const AUTH_LEVEL_MASTERADMIN = 4;
|
2013-12-31 17:17:11 +01:00
|
|
|
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
|
2014-01-06 17:48:40 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Public Properties
|
|
|
|
*/
|
|
|
|
public $authCommands = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private Properties
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
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-12-31 17:17:11 +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;
|
2014-01-06 17:48:40 +01:00
|
|
|
$this->updateMasterAdmins();
|
2013-11-13 01:43:12 +01:00
|
|
|
|
2013-12-03 18:03:16 +01:00
|
|
|
$this->authCommands = new AuthCommands($maniaControl);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Set MasterAdmins
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-06 17:48:40 +01:00
|
|
|
private function updateMasterAdmins() {
|
2013-11-10 19:30:14 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
2013-11-24 23:55:54 +01:00
|
|
|
// Remove all MasterAdmins
|
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;
|
2014-01-06 14:22:48 +01:00
|
|
|
$masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN;
|
|
|
|
$adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel);
|
2013-11-10 19:30:14 +01:00
|
|
|
$adminStatement->execute();
|
|
|
|
if ($adminStatement->error) {
|
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
}
|
|
|
|
$adminStatement->close();
|
|
|
|
|
2013-11-24 23:55:54 +01:00
|
|
|
// Set MasterAdmins
|
2014-01-06 14:22:48 +01:00
|
|
|
$masterAdmins = $this->maniaControl->config->masteradmins->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;
|
|
|
|
}
|
2014-01-06 14:22:48 +01:00
|
|
|
$adminStatement->bind_param('si', $login, $masterAdminLevel);
|
2013-11-10 19:30:14 +01:00
|
|
|
$success = true;
|
2014-01-06 14:22:48 +01:00
|
|
|
foreach ($masterAdmins as $masterAdmin) {
|
|
|
|
$login = (string) $masterAdmin;
|
2013-11-10 19:30:14 +01:00
|
|
|
$adminStatement->execute();
|
|
|
|
if ($adminStatement->error) {
|
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$adminStatement->close();
|
|
|
|
return $success;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Grant the Auth Level to the Player
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param Player $player
|
|
|
|
* @param int $authLevel
|
2013-11-09 17:24:03 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-06 17:48:40 +01:00
|
|
|
public function grantAuthLevel(Player &$player, $authLevel) {
|
|
|
|
if (!$player || !is_numeric($authLevel)) return false;
|
|
|
|
$authLevel = (int) $authLevel;
|
|
|
|
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) 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);
|
2014-01-06 17:48:40 +01:00
|
|
|
$authStatement->execute();
|
2013-11-10 19:30:14 +01:00
|
|
|
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();
|
2013-12-31 17:17:11 +01:00
|
|
|
|
2014-01-06 17:48:40 +01:00
|
|
|
$player->authLevel = $authLevel;
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_AUTH_LEVEL_CHANGED, array(self::CB_AUTH_LEVEL_CHANGED, $player));
|
2013-12-31 17:17:11 +01:00
|
|
|
|
2014-01-06 17:48:40 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Send an Error Message to the Player
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-09 13:45:58 +01:00
|
|
|
* @param Player $player
|
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) {
|
2014-01-06 17:48:40 +01:00
|
|
|
if (!$player) return false;
|
2013-12-03 18:03:16 +01:00
|
|
|
return $this->maniaControl->chat->sendError('You do not have the required Rights to perform this Command!', $player->login);
|
2013-11-13 01:43:12 +01:00
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Check if the Player has enough Rights
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-09 13:45:58 +01:00
|
|
|
* @param Player $player
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param int $neededAuthLevel
|
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 static function checkRight(Player $player, $neededAuthLevel) {
|
|
|
|
return ($player->authLevel >= $neededAuthLevel);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-25 00:19:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Name of the Authentication Level from Level Int
|
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param int $authLevelInt
|
2013-11-25 00:19:37 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getAuthLevelName($authLevelInt) {
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
|
|
|
|
return 'MasterAdmin';
|
|
|
|
}
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
|
|
|
|
return 'SuperAdmin';
|
|
|
|
}
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
|
|
|
|
return 'Admin';
|
|
|
|
}
|
2013-12-31 17:17:11 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
|
|
|
|
return 'Moderator';
|
2013-11-25 00:19:37 +01:00
|
|
|
}
|
|
|
|
return 'Player';
|
|
|
|
}
|
|
|
|
|
2013-12-31 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Get the Abbreviation of the Authentication Level from Level Int
|
|
|
|
*
|
|
|
|
* @param int $authLevelInt
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getAuthLevelAbbreviation($authLevelInt) {
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
|
|
|
|
return 'MA';
|
|
|
|
}
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
|
|
|
|
return 'SA';
|
|
|
|
}
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
|
|
|
|
return 'AD';
|
|
|
|
}
|
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
|
|
|
|
return 'MOD';
|
|
|
|
}
|
|
|
|
return 'PL';
|
|
|
|
}
|
|
|
|
|
2013-11-25 00:19:37 +01:00
|
|
|
/**
|
|
|
|
* Get Authentication Level Int from Level Name
|
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param string $authLevelName
|
2013-11-25 00:19:37 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getAuthLevel($authLevelName) {
|
|
|
|
$authLevelName = strtolower($authLevelName);
|
|
|
|
if ($authLevelName == 'MasterAdmin') {
|
|
|
|
return self::AUTH_LEVEL_MASTERADMIN;
|
|
|
|
}
|
|
|
|
if ($authLevelName == 'SuperAdmin') {
|
|
|
|
return self::AUTH_LEVEL_SUPERADMIN;
|
|
|
|
}
|
|
|
|
if ($authLevelName == 'Admin') {
|
|
|
|
return self::AUTH_LEVEL_ADMIN;
|
|
|
|
}
|
2013-12-31 17:17:11 +01:00
|
|
|
if ($authLevelName == 'Moderator') {
|
|
|
|
return self::AUTH_LEVEL_MODERATOR;
|
2013-11-25 00:19:37 +01:00
|
|
|
}
|
|
|
|
return self::AUTH_LEVEL_PLAYER;
|
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|