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\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
require_once __DIR__ . '/AuthCommands.php';
@ -13,69 +15,72 @@ require_once __DIR__ . '/AuthCommands.php';
*
* @author steeffeen & kremsy
*/
class AuthenticationManager {
class AuthenticationManager implements CallbackListener {
/**
* Constants
*/
const AUTH_LEVEL_PLAYER = 0;
const AUTH_LEVEL_MODERATOR = 1;
const AUTH_LEVEL_ADMIN = 2;
const AUTH_LEVEL_SUPERADMIN = 3;
const AUTH_LEVEL_PLAYER = 0;
const AUTH_LEVEL_MODERATOR = 1;
const AUTH_LEVEL_ADMIN = 2;
const AUTH_LEVEL_SUPERADMIN = 3;
const AUTH_LEVEL_MASTERADMIN = 4;
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
/**
* Public Properties
*/
public $authCommands = null;
/**
* Private Properties
*/
private $maniaControl = null;
/**
* Construct authentication manager
* Construct a new Authentication Manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->updateMasterAdmins();
$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
*/
private function updateMasterAdmins() {
$mysqli = $this->maniaControl->database->mysqli;
// Remove all MasterAdmins
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
SET `authLevel` = ?
WHERE `authLevel` = ?;";
$adminStatement = $mysqli->prepare($adminQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$adminLevel = self::AUTH_LEVEL_SUPERADMIN;
$adminLevel = self::AUTH_LEVEL_SUPERADMIN;
$masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN;
$adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel);
$adminStatement->execute();
if($adminStatement->error) {
if ($adminStatement->error) {
trigger_error($adminStatement->error);
}
$adminStatement->close();
// Set MasterAdmins
$masterAdmins = $this->maniaControl->config->masteradmins->xpath('login');
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
$masterAdmins = $this->maniaControl->config->masteradmins->xpath('login');
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`,
`authLevel`
) VALUES (
@ -83,16 +88,16 @@ class AuthenticationManager {
) ON DUPLICATE KEY UPDATE
`authLevel` = VALUES(`authLevel`);";
$adminStatement = $mysqli->prepare($adminQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$adminStatement->bind_param('si', $login, $masterAdminLevel);
$success = true;
foreach($masterAdmins as $masterAdmin) {
$login = (string)$masterAdmin;
foreach ($masterAdmins as $masterAdmin) {
$login = (string) $masterAdmin;
$adminStatement->execute();
if($adminStatement->error) {
if ($adminStatement->error) {
trigger_error($adminStatement->error);
$success = false;
}
@ -105,20 +110,16 @@ class AuthenticationManager {
* Grant the Auth Level to the Player
*
* @param Player $player
* @param int $authLevel
* @param int $authLevel
* @return bool
*/
public function grantAuthLevel(Player &$player, $authLevel) {
if(!$player || !is_numeric($authLevel)) {
return false;
}
$authLevel = (int)$authLevel;
if($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
return false;
}
$mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
if (!$player || !is_numeric($authLevel)) return false;
$authLevel = (int) $authLevel;
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) return false;
$mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`,
`authLevel`
) VALUES (
@ -126,22 +127,22 @@ class AuthenticationManager {
) ON DUPLICATE KEY UPDATE
`authLevel` = VALUES(`authLevel`);";
$authStatement = $mysqli->prepare($authQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$authStatement->bind_param('si', $player->login, $authLevel);
$authStatement->execute();
if($authStatement->error) {
if ($authStatement->error) {
trigger_error($authStatement->error);
$authStatement->close();
return false;
}
$authStatement->close();
$player->authLevel = $authLevel;
$this->maniaControl->callbackManager->triggerCallback(self::CB_AUTH_LEVEL_CHANGED, array(self::CB_AUTH_LEVEL_CHANGED, $player));
return true;
}
@ -152,9 +153,7 @@ class AuthenticationManager {
* @return bool
*/
public function sendNotAllowed(Player $player) {
if(!$player) {
return false;
}
if (!$player) return false;
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
*
* @param Player $player
* @param int $neededAuthLevel
* @param int $neededAuthLevel
* @return bool
*/
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 $rightName
* @param $rightName
* @return bool
*/
public function checkPermission(Player $player, $rightName) {
@ -191,7 +190,6 @@ class AuthenticationManager {
$this->maniaControl->settingManager->initSetting($this, $rightName, $authLevelNeeded);
}
/**
* Get Name of the Authentication Level from Level Int
*
@ -199,16 +197,16 @@ class AuthenticationManager {
* @return string
*/
public static function getAuthLevelName($authLevelInt) {
if($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MasterAdmin';
}
if($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SuperAdmin';
}
if($authLevelInt == self::AUTH_LEVEL_ADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'Admin';
}
if($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'Moderator';
}
return 'Player';
@ -221,16 +219,16 @@ class AuthenticationManager {
* @return string
*/
public static function getAuthLevelAbbreviation($authLevelInt) {
if($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MA';
}
if($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SA';
}
if($authLevelInt == self::AUTH_LEVEL_ADMIN) {
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'AD';
}
if($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'MOD';
}
return 'PL';
@ -244,16 +242,16 @@ class AuthenticationManager {
*/
public static function getAuthLevel($authLevelName) {
$authLevelName = strtolower($authLevelName);
if($authLevelName == 'MasterAdmin') {
if ($authLevelName == 'MasterAdmin') {
return self::AUTH_LEVEL_MASTERADMIN;
}
if($authLevelName == 'SuperAdmin') {
if ($authLevelName == 'SuperAdmin') {
return self::AUTH_LEVEL_SUPERADMIN;
}
if($authLevelName == 'Admin') {
if ($authLevelName == 'Admin') {
return self::AUTH_LEVEL_ADMIN;
}
if($authLevelName == 'Moderator') {
if ($authLevelName == 'Moderator') {
return self::AUTH_LEVEL_MODERATOR;
}
return self::AUTH_LEVEL_PLAYER;