fixed and improved authentication level methods
This commit is contained in:
parent
ab375e636e
commit
ef6d621374
@ -10,7 +10,7 @@ use ManiaControl\Players\PlayerManager;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class managing Authentication Levels
|
* Class managing Authentication Levels
|
||||||
*
|
*
|
||||||
* @author steeffeen & kremsy
|
* @author steeffeen & kremsy
|
||||||
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
||||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
@ -19,18 +19,23 @@ 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
|
||||||
*/
|
*/
|
||||||
public $authCommands = null;
|
public $authCommands = null;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Private Properties
|
* Private Properties
|
||||||
*/
|
*/
|
||||||
@ -38,13 +43,13 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 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->authCommands = new AuthCommands($maniaControl);
|
$this->authCommands = new AuthCommands($maniaControl);
|
||||||
|
|
||||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'handleOnInit');
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'handleOnInit');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,14 +62,14 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update MasterAdmins based on config
|
* Update MasterAdmins based on config
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function updateMasterAdmins() {
|
private function updateMasterAdmins() {
|
||||||
$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();
|
||||||
@ -80,10 +85,10 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
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 (
|
||||||
@ -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) {
|
||||||
@ -111,27 +116,25 @@ 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;
|
||||||
@ -139,22 +142,22 @@ 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 (
|
||||||
@ -174,16 +177,16 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$authStatement->close();
|
$authStatement->close();
|
||||||
|
|
||||||
$player->authLevel = $authLevel;
|
$player->authLevel = $authLevel;
|
||||||
$this->maniaControl->callbackManager->triggerCallback(self::CB_AUTH_LEVEL_CHANGED, $player);
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_AUTH_LEVEL_CHANGED, $player);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send an Error Message to the Player
|
* Send an Error Message to the Player
|
||||||
*
|
*
|
||||||
* @param Player $player
|
* @param Player $player
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -196,9 +199,9 @@ 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) {
|
||||||
@ -207,9 +210,9 @@ 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) {
|
||||||
@ -219,7 +222,7 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a Minimum Right Level needed for an action
|
* Defines a Minimum Right Level needed for an action
|
||||||
*
|
*
|
||||||
* @param $rightName
|
* @param $rightName
|
||||||
* @param $authLevelNeeded
|
* @param $authLevelNeeded
|
||||||
*/
|
*/
|
||||||
@ -229,54 +232,45 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Name of the Authentication Level from Level Int
|
* Get Name of the Authentication Level from Level Int
|
||||||
*
|
*
|
||||||
* @param mixed $authLevelInt
|
* @param mixed $authLevelInt
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Abbreviation of the Authentication Level from Level Int
|
* Get the Abbreviation of the Authentication Level from Level Int
|
||||||
*
|
*
|
||||||
* @param mixed $authLevelInt
|
* @param mixed $authLevelInt
|
||||||
* @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 '';
|
||||||
@ -284,24 +278,37 @@ class AuthenticationManager implements CallbackListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Authentication Level Int from Level Name
|
* Get Authentication Level Int from Level Name
|
||||||
*
|
*
|
||||||
* @param string $authLevelName
|
* @param string $authLevelName
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
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