fixed and improved authentication level methods
This commit is contained in:
parent
ab375e636e
commit
ef6d621374
@ -19,12 +19,17 @@ 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 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
|
* Public Properties
|
||||||
@ -64,7 +69,7 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
$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);
|
||||||
@ -72,7 +77,7 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
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();
|
||||||
@ -82,8 +87,8 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
$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 (
|
||||||
@ -96,8 +101,8 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$success = true;
|
$success = true;
|
||||||
foreach($masterAdmins as $masterAdmin) {
|
foreach ($masterAdmins as $masterAdmin) {
|
||||||
$login = (string)$masterAdmin;
|
$login = (string) $masterAdmin;
|
||||||
$adminStatement->bind_param('si', $login, $masterAdminLevel);
|
$adminStatement->bind_param('si', $login, $masterAdminLevel);
|
||||||
$adminStatement->execute();
|
$adminStatement->execute();
|
||||||
if ($adminStatement->error) {
|
if ($adminStatement->error) {
|
||||||
@ -113,25 +118,23 @@ 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);
|
||||||
}
|
}
|
||||||
return $admins;
|
return $admins;
|
||||||
@ -141,20 +144,20 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
* 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;
|
$authLevel = (int) $authLevel;
|
||||||
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
|
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
|
||||||
return false;
|
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 (
|
||||||
@ -198,7 +201,7 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
* 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) {
|
||||||
@ -209,7 +212,7 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
* 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) {
|
||||||
@ -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) {
|
|
||||||
return 'MasterAdmin';
|
|
||||||
}
|
}
|
||||||
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
|
if ($authLevelInt === self::AUTH_LEVEL_SUPERADMIN) {
|
||||||
return 'SuperAdmin';
|
return self::AUTH_NAME_SUPERADMIN;
|
||||||
}
|
}
|
||||||
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
|
if ($authLevelInt === self::AUTH_LEVEL_ADMIN) {
|
||||||
return 'Admin';
|
return self::AUTH_NAME_ADMIN;
|
||||||
}
|
}
|
||||||
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
|
if ($authLevelInt === self::AUTH_LEVEL_MODERATOR) {
|
||||||
return 'Moderator';
|
return self::AUTH_NAME_MODERATOR;
|
||||||
}
|
}
|
||||||
return 'Player';
|
return self::AUTH_NAME_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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user