TrackManiaControl/application/core/Admin/AuthenticationManager.php

295 lines
7.2 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl\Admin;
2013-11-09 17:24:03 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
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
*
* @author steeffeen & kremsy
2013-11-09 17:24:03 +01:00
*/
2014-01-09 22:13:25 +01:00
class AuthenticationManager implements CallbackListener {
2013-11-09 17:24:03 +01:00
/**
* Constants
*/
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';
2013-11-09 17:24:03 +01:00
/**
2014-01-06 17:48:40 +01:00
* Public Properties
*/
public $authCommands = null;
2014-01-06 17:48:40 +01:00
/**
* Private Properties
2013-11-09 17:24:03 +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
*
* @param \ManiaControl\ManiaControl $maniaControl
2013-11-09 17:24:03 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->authCommands = new AuthCommands($maniaControl);
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
}
/**
* 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() {
$mysqli = $this->maniaControl->database->mysqli;
// Remove all MasterAdmins
$adminQuery = "UPDATE `" . PlayerManager::TABLE_PLAYERS . "`
SET `authLevel` = ?
WHERE `authLevel` = ?;";
$adminStatement = $mysqli->prepare($adminQuery);
2014-01-09 22:13:25 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$adminLevel = self::AUTH_LEVEL_SUPERADMIN;
2014-01-06 14:22:48 +01:00
$masterAdminLevel = self::AUTH_LEVEL_MASTERADMIN;
$adminStatement->bind_param('ii', $adminLevel, $masterAdminLevel);
$adminStatement->execute();
2014-01-09 22:13:25 +01:00
if ($adminStatement->error) {
trigger_error($adminStatement->error);
}
$adminStatement->close();
// Set MasterAdmins
$masterAdmins = $this->maniaControl->config->masteradmins->xpath('login');
$adminQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`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) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$success = true;
foreach($masterAdmins as $masterAdmin) {
$login = (string)$masterAdmin;
2014-02-13 00:46:41 +01:00
$adminStatement->bind_param('si', $login, $masterAdminLevel);
$adminStatement->execute();
2014-01-09 22:13:25 +01:00
if ($adminStatement->error) {
trigger_error($adminStatement->error);
$success = false;
}
}
$adminStatement->close();
return $success;
2013-11-09 17:24:03 +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
*
* @param Player $player
* @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) {
if (!$player || !is_numeric($authLevel)) {
return false;
}
$authLevel = (int)$authLevel;
2014-01-31 16:55:01 +01:00
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
return false;
2014-01-31 16:55:01 +01:00
}
$mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`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) {
trigger_error($mysqli->error, E_USER_ERROR);
2013-11-09 17:24:03 +01:00
return false;
}
$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) {
trigger_error($authStatement->error);
$authStatement->close();
return false;
2013-11-09 17:24:03 +01:00
}
$authStatement->close();
2014-01-06 17:48:40 +01:00
$player->authLevel = $authLevel;
$this->maniaControl->callbackManager->triggerCallback($player);
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
*
* @param Player $player
* @return bool
2013-11-09 17:24:03 +01:00
*/
public function sendNotAllowed(Player $player) {
2014-01-31 16:55:01 +01:00
if (!$player) {
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-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
*
* @param Player $player
* @param int $neededAuthLevel
* @return bool
2013-11-09 17:24:03 +01:00
*/
public static function checkRight(Player $player, $neededAuthLevel) {
return ($player->authLevel >= $neededAuthLevel);
2013-11-09 17:24:03 +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
* @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);
}
/**
* Get Name of the Authentication Level from Level Int
*
* @param int $authLevelInt
* @return string
*/
public static function getAuthLevelName($authLevelInt) {
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MasterAdmin';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SuperAdmin';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'Admin';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'Moderator';
}
return 'Player';
}
/**
* Get the Abbreviation of the Authentication Level from Level Int
*
* @param int $authLevelInt
* @return string
*/
public static function getAuthLevelAbbreviation($authLevelInt) {
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_MASTERADMIN) {
return 'MA';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_SUPERADMIN) {
return 'SA';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_ADMIN) {
return 'AD';
}
2014-01-09 22:13:25 +01:00
if ($authLevelInt == self::AUTH_LEVEL_MODERATOR) {
return 'MOD';
}
return '';
}
/**
* Get Authentication Level Int from Level Name
*
* @param string $authLevelName
* @return int
*/
public static function getAuthLevel($authLevelName) {
$authLevelName = strtolower($authLevelName);
2014-01-09 22:13:25 +01:00
if ($authLevelName == 'MasterAdmin') {
return self::AUTH_LEVEL_MASTERADMIN;
}
2014-01-09 22:13:25 +01:00
if ($authLevelName == 'SuperAdmin') {
return self::AUTH_LEVEL_SUPERADMIN;
}
2014-01-09 22:13:25 +01:00
if ($authLevelName == 'Admin') {
return self::AUTH_LEVEL_ADMIN;
}
2014-01-09 22:13:25 +01:00
if ($authLevelName == 'Moderator') {
return self::AUTH_LEVEL_MODERATOR;
}
return self::AUTH_LEVEL_PLAYER;
}
2013-11-09 17:24:03 +01:00
}