fixed and improved authentication level methods

This commit is contained in:
Steffen Schröder 2014-04-30 00:05:09 +02:00
parent ab375e636e
commit ef6d621374

View File

@ -19,12 +19,17 @@ 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 AUTH_NAME_PLAYER = 'Player';
const AUTH_NAME_MODERATOR = 'Moderator';
const AUTH_NAME_ADMIN = 'Admin';
const AUTH_NAME_SUPERADMIN = 'SuperAdmin';
const AUTH_NAME_MASTERADMIN = 'MasterAdmin';
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
/*
* Public Properties
@ -64,7 +69,7 @@ class AuthenticationManager implements CallbackListener {
$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);
@ -72,7 +77,7 @@ class AuthenticationManager implements CallbackListener {
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();
@ -82,8 +87,8 @@ class AuthenticationManager implements CallbackListener {
$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 (
@ -96,8 +101,8 @@ class AuthenticationManager implements CallbackListener {
return false;
}
$success = true;
foreach($masterAdmins as $masterAdmin) {
$login = (string)$masterAdmin;
foreach ($masterAdmins as $masterAdmin) {
$login = (string) $masterAdmin;
$adminStatement->bind_param('si', $login, $masterAdminLevel);
$adminStatement->execute();
if ($adminStatement->error) {
@ -113,25 +118,23 @@ class AuthenticationManager implements CallbackListener {
* Get a List of all Admins
*
* @param $authLevel
* @return array|null
* @return array null
*/
public function getAdmins($authLevel = -1) {
$mysqli = $this->maniaControl->database->mysqli;
if ($authLevel == -1) {
if ($authLevel < 0) {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` > 0 ORDER BY `authLevel` DESC;";
} else {
}
else {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` = " . $authLevel . ";";
}
$result = $mysqli->query($query);
if (!$result) {
trigger_error($mysqli->error);
return null;
}
$admins = array();
while($row = $result->fetch_object()) {
while ($row = $result->fetch_object()) {
array_push($admins, $row);
}
return $admins;
@ -141,20 +144,20 @@ class AuthenticationManager implements CallbackListener {
* 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;
$authLevel = (int) $authLevel;
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
return false;
}
$mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
$mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`,
`authLevel`
) VALUES (
@ -198,7 +201,7 @@ class AuthenticationManager implements CallbackListener {
* 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) {
@ -209,7 +212,7 @@ class AuthenticationManager implements CallbackListener {
* Checks the permission by a right name
*
* @param Player $player
* @param $rightName
* @param $rightName
* @return bool
*/
public function checkPermission(Player $player, $rightName) {
@ -234,25 +237,20 @@ class AuthenticationManager implements CallbackListener {
* @return string
*/
public static function getAuthLevelName($authLevelInt) {
//FIXME somehow it fails here (look also AdminList.php)
if ($authLevelInt instanceof Player) {
$authLevelInt = $authLevelInt->authLevel;
} else {
$authLevelInt = (int) $authLevelInt;
}
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MasterAdmin';
$authLevelInt = self::getAuthLevelInt($authLevelInt);
if ($authLevelInt === self::AUTH_LEVEL_MASTERADMIN) {
return self::AUTH_NAME_MASTERADMIN;
}
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SuperAdmin';
if ($authLevelInt === self::AUTH_LEVEL_SUPERADMIN) {
return self::AUTH_NAME_SUPERADMIN;
}
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'Admin';
if ($authLevelInt === self::AUTH_LEVEL_ADMIN) {
return self::AUTH_NAME_ADMIN;
}
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'Moderator';
if ($authLevelInt === self::AUTH_LEVEL_MODERATOR) {
return self::AUTH_NAME_MODERATOR;
}
return 'Player';
return self::AUTH_NAME_PLAYER;
}
/**
@ -262,21 +260,17 @@ class AuthenticationManager implements CallbackListener {
* @return string
*/
public static function getAuthLevelAbbreviation($authLevelInt) {
if ($authLevelInt instanceof Player) {
$authLevelInt = $authLevelInt->authLevel;
} else {
$authLevelInt = (int) $authLevelInt;
}
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
$authLevelInt = self::getAuthLevelInt($authLevelInt);
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 '';
@ -290,18 +284,31 @@ class AuthenticationManager implements CallbackListener {
*/
public static function getAuthLevel($authLevelName) {
$authLevelName = strtolower($authLevelName);
if ($authLevelName == 'MasterAdmin') {
if ($authLevelName === self::AUTH_NAME_MASTERADMIN) {
return self::AUTH_LEVEL_MASTERADMIN;
}
if ($authLevelName == 'SuperAdmin') {
if ($authLevelName === self::AUTH_NAME_SUPERADMIN) {
return self::AUTH_LEVEL_SUPERADMIN;
}
if ($authLevelName == 'Admin') {
if ($authLevelName === self::AUTH_NAME_ADMIN) {
return self::AUTH_LEVEL_ADMIN;
}
if ($authLevelName == 'Moderator') {
if ($authLevelName === self::AUTH_NAME_MODERATOR) {
return self::AUTH_LEVEL_MODERATOR;
}
return self::AUTH_LEVEL_PLAYER;
}
/**
* Get the Authentication Level Int from the given Param
*
* @param mixed $authLevelParam
* @return int
*/
public static function getAuthLevelInt($authLevelParam) {
if (is_object($authLevelParam) && property_exists($authLevelParam, 'authLevel')) {
return (int) $authLevelParam->authLevel;
}
return (int) $authLevelParam;
}
}