2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-13 01:43:12 +01:00
|
|
|
namespace ManiaControl\Admin;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
2014-01-31 13:40:07 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-11-13 01:43:12 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-12 15:48:25 +01:00
|
|
|
use ManiaControl\Players\Player;
|
2013-11-12 19:33:25 +01:00
|
|
|
use ManiaControl\Players\PlayerManager;
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Class managing Authentication Levels
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-11-10 19:30:14 +01:00
|
|
|
* @author steeffeen & kremsy
|
2014-04-12 12:14:37 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2014-01-09 22:13:25 +01:00
|
|
|
class AuthenticationManager implements CallbackListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-09 17:24:03 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-31 13:40:07 +01:00
|
|
|
const AUTH_LEVEL_PLAYER = 0;
|
|
|
|
const AUTH_LEVEL_MODERATOR = 1;
|
|
|
|
const AUTH_LEVEL_ADMIN = 2;
|
|
|
|
const AUTH_LEVEL_SUPERADMIN = 3;
|
2013-11-24 23:55:54 +01:00
|
|
|
const AUTH_LEVEL_MASTERADMIN = 4;
|
2014-01-31 13:40:07 +01:00
|
|
|
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-06 17:48:40 +01:00
|
|
|
* Public Properties
|
|
|
|
*/
|
|
|
|
public $authCommands = null;
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-06 17:48:40 +01:00
|
|
|
* Private Properties
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-09 22:13:25 +01:00
|
|
|
* Construct a new Authentication Manager
|
2013-11-10 19:30:14 +01:00
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2013-12-03 18:03:16 +01:00
|
|
|
$this->authCommands = new AuthCommands($maniaControl);
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'handleOnInit');
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:44:00 +01:00
|
|
|
/**
|
|
|
|
* Handle ManiaControl OnInit Callback
|
|
|
|
*/
|
|
|
|
public function handleOnInit() {
|
2014-01-09 22:13:25 +01:00
|
|
|
$this->updateMasterAdmins();
|
|
|
|
}
|
2014-01-09 18:45:39 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-09 22:13:25 +01:00
|
|
|
* Update MasterAdmins based on config
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-06 17:48:40 +01:00
|
|
|
private function updateMasterAdmins() {
|
2013-11-10 19:30:14 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2013-11-24 23:55:54 +01:00
|
|
|
// Remove all MasterAdmins
|
2014-01-31 13:40:07 +01:00
|
|
|
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
|
2013-11-10 19:30:14 +01:00
|
|
|
SET `authLevel` = ?
|
|
|
|
WHERE `authLevel` = ?;";
|
|
|
|
$adminStatement = $mysqli->prepare($adminQuery);
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-31 13:40:07 +01:00
|
|
|
$adminLevel = self::AUTH_LEVEL_SUPERADMIN;
|
2014-01-06 14:22:48 +01:00
|
|
|
$masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN;
|
|
|
|
$adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel);
|
2013-11-10 19:30:14 +01:00
|
|
|
$adminStatement->execute();
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($adminStatement->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
}
|
|
|
|
$adminStatement->close();
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2013-11-24 23:55:54 +01:00
|
|
|
// Set MasterAdmins
|
2014-01-31 13:40:07 +01:00
|
|
|
$masterAdmins = $this->maniaControl->config->masteradmins->xpath('login');
|
|
|
|
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
|
2013-11-10 19:30:14 +01:00
|
|
|
`login`,
|
|
|
|
`authLevel`
|
|
|
|
) VALUES (
|
|
|
|
?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`authLevel` = VALUES(`authLevel`);";
|
|
|
|
$adminStatement = $mysqli->prepare($adminQuery);
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$success = true;
|
2014-01-31 13:40:07 +01:00
|
|
|
foreach($masterAdmins as $masterAdmin) {
|
|
|
|
$login = (string)$masterAdmin;
|
2014-02-13 00:46:41 +01:00
|
|
|
$adminStatement->bind_param('si', $login, $masterAdminLevel);
|
2013-11-10 19:30:14 +01:00
|
|
|
$adminStatement->execute();
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($adminStatement->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($adminStatement->error);
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$adminStatement->close();
|
|
|
|
return $success;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 13:40:07 +01:00
|
|
|
/**
|
|
|
|
* Get a List of all Admins
|
|
|
|
*
|
|
|
|
* @param $authLevel
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getAdmins($authLevel = -1) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
|
|
|
if ($authLevel == -1) {
|
|
|
|
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "` WHERE `authLevel` > 0 ORDER BY `authLevel` DESC;";
|
|
|
|
} 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()) {
|
|
|
|
array_push($admins, $row);
|
|
|
|
}
|
|
|
|
return $admins;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Grant the Auth Level to the Player
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param Player $player
|
2014-01-31 13:40:07 +01:00
|
|
|
* @param int $authLevel
|
2013-11-09 17:24:03 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-06 17:48:40 +01:00
|
|
|
public function grantAuthLevel(Player &$player, $authLevel) {
|
2014-01-31 13:40:07 +01:00
|
|
|
if (!$player || !is_numeric($authLevel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$authLevel = (int)$authLevel;
|
2014-01-31 16:55:01 +01:00
|
|
|
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
|
2014-01-31 13:40:07 +01:00
|
|
|
return false;
|
2014-01-31 16:55:01 +01:00
|
|
|
}
|
2014-01-31 13:40:07 +01:00
|
|
|
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
|
2013-11-10 19:30:14 +01:00
|
|
|
`login`,
|
|
|
|
`authLevel`
|
|
|
|
) VALUES (
|
|
|
|
?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`authLevel` = VALUES(`authLevel`);";
|
|
|
|
$authStatement = $mysqli->prepare($authQuery);
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
2013-11-09 17:24:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
$authStatement->bind_param('si', $player->login, $authLevel);
|
2014-01-06 17:48:40 +01:00
|
|
|
$authStatement->execute();
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authStatement->error) {
|
2013-11-10 19:30:14 +01:00
|
|
|
trigger_error($authStatement->error);
|
|
|
|
$authStatement->close();
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 19:30:14 +01:00
|
|
|
$authStatement->close();
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2014-01-06 17:48:40 +01:00
|
|
|
$player->authLevel = $authLevel;
|
2014-02-27 13:16:34 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_AUTH_LEVEL_CHANGED, $player);
|
2014-01-31 13:40:07 +01:00
|
|
|
|
2014-01-06 17:48:40 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Send an Error Message to the Player
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-09 13:45:58 +01:00
|
|
|
* @param Player $player
|
2013-11-10 19:30:14 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
public function sendNotAllowed(Player $player) {
|
2014-01-31 16:55:01 +01:00
|
|
|
if (!$player) {
|
2014-01-31 13:40:07 +01:00
|
|
|
return false;
|
2014-01-31 16:55:01 +01:00
|
|
|
}
|
|
|
|
return $this->maniaControl->chat->sendError('You do not have the required Rights to perform this Action!', $player->login);
|
2013-11-13 01:43:12 +01:00
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 17:48:40 +01:00
|
|
|
* Check if the Player has enough Rights
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-09 13:45:58 +01:00
|
|
|
* @param Player $player
|
2014-01-31 13:40:07 +01:00
|
|
|
* @param int $neededAuthLevel
|
2013-11-10 19:30:14 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 19:30:14 +01:00
|
|
|
public static function checkRight(Player $player, $neededAuthLevel) {
|
|
|
|
return ($player->authLevel >= $neededAuthLevel);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-25 00:19:37 +01:00
|
|
|
|
2014-01-09 18:45:39 +01:00
|
|
|
/**
|
2014-01-09 22:13:25 +01:00
|
|
|
* Checks the permission by a right name
|
2014-01-09 18:45:39 +01:00
|
|
|
*
|
|
|
|
* @param Player $player
|
2014-01-31 13:40:07 +01:00
|
|
|
* @param $rightName
|
2014-01-09 18:45:39 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkPermission(Player $player, $rightName) {
|
|
|
|
$right = $this->maniaControl->settingManager->getSetting($this, $rightName);
|
|
|
|
return $this->checkRight($player, $right);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a Minimum Right Level needed for an action
|
|
|
|
*
|
|
|
|
* @param $rightName
|
|
|
|
* @param $authLevelNeeded
|
|
|
|
*/
|
|
|
|
public function definePermissionLevel($rightName, $authLevelNeeded) {
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, $rightName, $authLevelNeeded);
|
|
|
|
}
|
|
|
|
|
2013-11-25 00:19:37 +01:00
|
|
|
/**
|
|
|
|
* Get Name of the Authentication Level from Level Int
|
|
|
|
*
|
2014-04-27 16:22:12 +02:00
|
|
|
* @param mixed $authLevelInt
|
2013-11-25 00:19:37 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getAuthLevelName($authLevelInt) {
|
2014-04-29 20:54:50 +02:00
|
|
|
//FIXME somehow it fails here (look also AdminList.php)
|
2014-04-27 16:22:12 +02:00
|
|
|
if ($authLevelInt instanceof Player) {
|
|
|
|
$authLevelInt = $authLevelInt->authLevel;
|
|
|
|
} else {
|
|
|
|
$authLevelInt = (int) $authLevelInt;
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
|
2013-11-25 00:19:37 +01:00
|
|
|
return 'MasterAdmin';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
|
2013-11-25 00:19:37 +01:00
|
|
|
return 'SuperAdmin';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
|
2013-11-25 00:19:37 +01:00
|
|
|
return 'Admin';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
|
2013-12-31 17:17:11 +01:00
|
|
|
return 'Moderator';
|
2013-11-25 00:19:37 +01:00
|
|
|
}
|
|
|
|
return 'Player';
|
|
|
|
}
|
|
|
|
|
2013-12-31 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Get the Abbreviation of the Authentication Level from Level Int
|
|
|
|
*
|
2014-04-27 16:22:12 +02:00
|
|
|
* @param mixed $authLevelInt
|
2013-12-31 17:17:11 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getAuthLevelAbbreviation($authLevelInt) {
|
2014-04-27 16:22:12 +02:00
|
|
|
if ($authLevelInt instanceof Player) {
|
|
|
|
$authLevelInt = $authLevelInt->authLevel;
|
|
|
|
} else {
|
|
|
|
$authLevelInt = (int) $authLevelInt;
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
|
2013-12-31 17:17:11 +01:00
|
|
|
return 'MA';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
|
2013-12-31 17:17:11 +01:00
|
|
|
return 'SA';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
|
2013-12-31 17:17:11 +01:00
|
|
|
return 'AD';
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
|
2013-12-31 17:17:11 +01:00
|
|
|
return 'MOD';
|
|
|
|
}
|
2014-01-31 13:40:07 +01:00
|
|
|
return '';
|
2013-12-31 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 00:19:37 +01:00
|
|
|
/**
|
|
|
|
* Get Authentication Level Int from Level Name
|
|
|
|
*
|
2013-12-31 17:17:11 +01:00
|
|
|
* @param string $authLevelName
|
2013-11-25 00:19:37 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getAuthLevel($authLevelName) {
|
|
|
|
$authLevelName = strtolower($authLevelName);
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelName == 'MasterAdmin') {
|
2013-11-25 00:19:37 +01:00
|
|
|
return self::AUTH_LEVEL_MASTERADMIN;
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelName == 'SuperAdmin') {
|
2013-11-25 00:19:37 +01:00
|
|
|
return self::AUTH_LEVEL_SUPERADMIN;
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelName == 'Admin') {
|
2013-11-25 00:19:37 +01:00
|
|
|
return self::AUTH_LEVEL_ADMIN;
|
|
|
|
}
|
2014-01-09 22:13:25 +01:00
|
|
|
if ($authLevelName == 'Moderator') {
|
2013-12-31 17:17:11 +01:00
|
|
|
return self::AUTH_LEVEL_MODERATOR;
|
2013-11-25 00:19:37 +01:00
|
|
|
}
|
|
|
|
return self::AUTH_LEVEL_PLAYER;
|
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|