fixed authentication manager

This commit is contained in:
Steffen Schröder 2014-01-09 22:13:25 +01:00
parent f9041748f6
commit 161ef44508

View File

@ -5,6 +5,8 @@ namespace ManiaControl\Admin;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
require_once __DIR__ . '/AuthCommands.php'; require_once __DIR__ . '/AuthCommands.php';
@ -13,16 +15,16 @@ require_once __DIR__ . '/AuthCommands.php';
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
class AuthenticationManager { class AuthenticationManager implements CallbackListener {
/** /**
* Constants * Constants
*/ */
const AUTH_LEVEL_PLAYER = 0; const AUTH_LEVEL_PLAYER = 0;
const AUTH_LEVEL_MODERATOR = 1; const AUTH_LEVEL_MODERATOR = 1;
const AUTH_LEVEL_ADMIN = 2; const AUTH_LEVEL_ADMIN = 2;
const AUTH_LEVEL_SUPERADMIN = 3; const AUTH_LEVEL_SUPERADMIN = 3;
const AUTH_LEVEL_MASTERADMIN = 4; const AUTH_LEVEL_MASTERADMIN = 4;
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged'; const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
/** /**
* Public Properties * Public Properties
@ -35,20 +37,23 @@ class AuthenticationManager {
private $maniaControl = null; private $maniaControl = null;
/** /**
* Construct authentication manager * Construct a new Authentication Manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->updateMasterAdmins();
$this->authCommands = new AuthCommands($maniaControl); $this->authCommands = new AuthCommands($maniaControl);
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
} }
public function handleOnInit(array $callback) {
$this->updateMasterAdmins();
}
/** /**
* Set MasterAdmins * Update MasterAdmins based on config
* *
* @return bool * @return bool
*/ */
@ -56,26 +61,26 @@ class AuthenticationManager {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
// Remove all MasterAdmins // Remove all MasterAdmins
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "` $adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
SET `authLevel` = ? SET `authLevel` = ?
WHERE `authLevel` = ?;"; WHERE `authLevel` = ?;";
$adminStatement = $mysqli->prepare($adminQuery); $adminStatement = $mysqli->prepare($adminQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$adminLevel = self::AUTH_LEVEL_SUPERADMIN; $adminLevel = self::AUTH_LEVEL_SUPERADMIN;
$masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN; $masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN;
$adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel); $adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel);
$adminStatement->execute(); $adminStatement->execute();
if($adminStatement->error) { if ($adminStatement->error) {
trigger_error($adminStatement->error); trigger_error($adminStatement->error);
} }
$adminStatement->close(); $adminStatement->close();
// Set MasterAdmins // Set MasterAdmins
$masterAdmins = $this->maniaControl->config->masteradmins->xpath('login'); $masterAdmins = $this->maniaControl->config->masteradmins->xpath('login');
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` ( $adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`, `login`,
`authLevel` `authLevel`
) VALUES ( ) VALUES (
@ -83,16 +88,16 @@ class AuthenticationManager {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`authLevel` = VALUES(`authLevel`);"; `authLevel` = VALUES(`authLevel`);";
$adminStatement = $mysqli->prepare($adminQuery); $adminStatement = $mysqli->prepare($adminQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$adminStatement->bind_param('si', $login, $masterAdminLevel); $adminStatement->bind_param('si', $login, $masterAdminLevel);
$success = true; $success = true;
foreach($masterAdmins as $masterAdmin) { foreach ($masterAdmins as $masterAdmin) {
$login = (string)$masterAdmin; $login = (string) $masterAdmin;
$adminStatement->execute(); $adminStatement->execute();
if($adminStatement->error) { if ($adminStatement->error) {
trigger_error($adminStatement->error); trigger_error($adminStatement->error);
$success = false; $success = false;
} }
@ -105,20 +110,16 @@ class AuthenticationManager {
* Grant the Auth Level to the Player * Grant the Auth Level to the Player
* *
* @param Player $player * @param Player $player
* @param int $authLevel * @param int $authLevel
* @return bool * @return bool
*/ */
public function grantAuthLevel(Player &$player, $authLevel) { public function grantAuthLevel(Player &$player, $authLevel) {
if(!$player || !is_numeric($authLevel)) { if (!$player || !is_numeric($authLevel)) return false;
return false; $authLevel = (int) $authLevel;
} if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) return false;
$authLevel = (int)$authLevel;
if($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
return false;
}
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` ( $authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`, `login`,
`authLevel` `authLevel`
) VALUES ( ) VALUES (
@ -126,13 +127,13 @@ class AuthenticationManager {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`authLevel` = VALUES(`authLevel`);"; `authLevel` = VALUES(`authLevel`);";
$authStatement = $mysqli->prepare($authQuery); $authStatement = $mysqli->prepare($authQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$authStatement->bind_param('si', $player->login, $authLevel); $authStatement->bind_param('si', $player->login, $authLevel);
$authStatement->execute(); $authStatement->execute();
if($authStatement->error) { if ($authStatement->error) {
trigger_error($authStatement->error); trigger_error($authStatement->error);
$authStatement->close(); $authStatement->close();
return false; return false;
@ -152,9 +153,7 @@ class AuthenticationManager {
* @return bool * @return bool
*/ */
public function sendNotAllowed(Player $player) { public function sendNotAllowed(Player $player) {
if(!$player) { if (!$player) return false;
return false;
}
return $this->maniaControl->chat->sendError('You do not have the required Rights to perform this Command!', $player->login); return $this->maniaControl->chat->sendError('You do not have the required Rights to perform this Command!', $player->login);
} }
@ -162,7 +161,7 @@ class AuthenticationManager {
* Check if the Player has enough Rights * Check if the Player has enough Rights
* *
* @param Player $player * @param Player $player
* @param int $neededAuthLevel * @param int $neededAuthLevel
* @return bool * @return bool
*/ */
public static function checkRight(Player $player, $neededAuthLevel) { public static function checkRight(Player $player, $neededAuthLevel) {
@ -170,10 +169,10 @@ class AuthenticationManager {
} }
/** /**
* Checks the permission by a right name * Checks the permission by a right name
* *
* @param Player $player * @param Player $player
* @param $rightName * @param $rightName
* @return bool * @return bool
*/ */
public function checkPermission(Player $player, $rightName) { public function checkPermission(Player $player, $rightName) {
@ -191,7 +190,6 @@ class AuthenticationManager {
$this->maniaControl->settingManager->initSetting($this, $rightName, $authLevelNeeded); $this->maniaControl->settingManager->initSetting($this, $rightName, $authLevelNeeded);
} }
/** /**
* Get Name of the Authentication Level from Level Int * Get Name of the Authentication Level from Level Int
* *
@ -199,16 +197,16 @@ class AuthenticationManager {
* @return string * @return string
*/ */
public static function getAuthLevelName($authLevelInt) { public static function getAuthLevelName($authLevelInt) {
if($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) { if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MasterAdmin'; return 'MasterAdmin';
} }
if($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) { if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SuperAdmin'; return 'SuperAdmin';
} }
if($authLevelInt == self::AUTH_LEVEL_ADMIN) { if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'Admin'; return 'Admin';
} }
if($authLevelInt == self::AUTH_LEVEL_MODERATOR) { if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'Moderator'; return 'Moderator';
} }
return 'Player'; return 'Player';
@ -221,16 +219,16 @@ class AuthenticationManager {
* @return string * @return string
*/ */
public static function getAuthLevelAbbreviation($authLevelInt) { public static function getAuthLevelAbbreviation($authLevelInt) {
if($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) { if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MA'; return 'MA';
} }
if($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) { if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SA'; return 'SA';
} }
if($authLevelInt == self::AUTH_LEVEL_ADMIN) { if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'AD'; return 'AD';
} }
if($authLevelInt == self::AUTH_LEVEL_MODERATOR) { if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'MOD'; return 'MOD';
} }
return 'PL'; return 'PL';
@ -244,16 +242,16 @@ class AuthenticationManager {
*/ */
public static function getAuthLevel($authLevelName) { public static function getAuthLevel($authLevelName) {
$authLevelName = strtolower($authLevelName); $authLevelName = strtolower($authLevelName);
if($authLevelName == 'MasterAdmin') { if ($authLevelName == 'MasterAdmin') {
return self::AUTH_LEVEL_MASTERADMIN; return self::AUTH_LEVEL_MASTERADMIN;
} }
if($authLevelName == 'SuperAdmin') { if ($authLevelName == 'SuperAdmin') {
return self::AUTH_LEVEL_SUPERADMIN; return self::AUTH_LEVEL_SUPERADMIN;
} }
if($authLevelName == 'Admin') { if ($authLevelName == 'Admin') {
return self::AUTH_LEVEL_ADMIN; return self::AUTH_LEVEL_ADMIN;
} }
if($authLevelName == 'Moderator') { if ($authLevelName == 'Moderator') {
return self::AUTH_LEVEL_MODERATOR; return self::AUTH_LEVEL_MODERATOR;
} }
return self::AUTH_LEVEL_PLAYER; return self::AUTH_LEVEL_PLAYER;