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

@ -24,6 +24,11 @@ class AuthenticationManager implements CallbackListener {
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 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'; const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
/* /*
@ -113,23 +118,21 @@ class AuthenticationManager implements CallbackListener {
* Get a List of all Admins * Get a List of all Admins
* *
* @param $authLevel * @param $authLevel
* @return array|null * @return array null
*/ */
public function getAdmins($authLevel = -1) { public function getAdmins($authLevel = -1) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
if ($authLevel < 0) {
if ($authLevel == -1) {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` > 0 ORDER BY `authLevel` DESC;"; $query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` > 0 ORDER BY `authLevel` DESC;";
} else { }
else {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` = " . $authLevel . ";"; $query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` = " . $authLevel . ";";
} }
$result = $mysqli->query($query); $result = $mysqli->query($query);
if (!$result) { if (!$result) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return null; return null;
} }
$admins = array(); $admins = array();
while ($row = $result->fetch_object()) { while ($row = $result->fetch_object()) {
array_push($admins, $row); array_push($admins, $row);
@ -234,25 +237,20 @@ class AuthenticationManager implements CallbackListener {
* @return string * @return string
*/ */
public static function getAuthLevelName($authLevelInt) { public static function getAuthLevelName($authLevelInt) {
//FIXME somehow it fails here (look also AdminList.php) $authLevelInt = self::getAuthLevelInt($authLevelInt);
if ($authLevelInt instanceof Player) { if ($authLevelInt === self::AUTH_LEVEL_MASTERADMIN) {
$authLevelInt = $authLevelInt->authLevel; return self::AUTH_NAME_MASTERADMIN;
} else {
$authLevelInt = (int) $authLevelInt;
} }
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) { if ($authLevelInt === self::AUTH_LEVEL_SUPERADMIN) {
return 'MasterAdmin'; return self::AUTH_NAME_SUPERADMIN;
} }
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) { if ($authLevelInt === self::AUTH_LEVEL_ADMIN) {
return 'SuperAdmin'; return self::AUTH_NAME_ADMIN;
} }
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) { if ($authLevelInt === self::AUTH_LEVEL_MODERATOR) {
return 'Admin'; return self::AUTH_NAME_MODERATOR;
} }
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) { return self::AUTH_NAME_PLAYER;
return 'Moderator';
}
return 'Player';
} }
/** /**
@ -262,21 +260,17 @@ class AuthenticationManager implements CallbackListener {
* @return string * @return string
*/ */
public static function getAuthLevelAbbreviation($authLevelInt) { public static function getAuthLevelAbbreviation($authLevelInt) {
if ($authLevelInt instanceof Player) { $authLevelInt = self::getAuthLevelInt($authLevelInt);
$authLevelInt = $authLevelInt->authLevel; if ($authLevelInt === self::AUTH_LEVEL_MASTERADMIN) {
} else {
$authLevelInt = (int) $authLevelInt;
}
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 ''; return '';
@ -290,18 +284,31 @@ class AuthenticationManager implements CallbackListener {
*/ */
public static function getAuthLevel($authLevelName) { public static function getAuthLevel($authLevelName) {
$authLevelName = strtolower($authLevelName); $authLevelName = strtolower($authLevelName);
if ($authLevelName == 'MasterAdmin') { if ($authLevelName === self::AUTH_NAME_MASTERADMIN) {
return self::AUTH_LEVEL_MASTERADMIN; return self::AUTH_LEVEL_MASTERADMIN;
} }
if ($authLevelName == 'SuperAdmin') { if ($authLevelName === self::AUTH_NAME_SUPERADMIN) {
return self::AUTH_LEVEL_SUPERADMIN; return self::AUTH_LEVEL_SUPERADMIN;
} }
if ($authLevelName == 'Admin') { if ($authLevelName === self::AUTH_NAME_ADMIN) {
return self::AUTH_LEVEL_ADMIN; return self::AUTH_LEVEL_ADMIN;
} }
if ($authLevelName == 'Moderator') { if ($authLevelName === self::AUTH_NAME_MODERATOR) {
return self::AUTH_LEVEL_MODERATOR; return self::AUTH_LEVEL_MODERATOR;
} }
return self::AUTH_LEVEL_PLAYER; 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;
}
} }