Authentication/src/FuzeWorks/Authentication/AuthenticationPlugin.php

184 lines
6.0 KiB
PHP
Executable File

<?php
/**
* FuzeWorks Authentication Plugin.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013 - 2023 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (C) 2013 - 2023, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\Authentication;
use Application\Controller\AuthenticationController;
use FuzeWorks\Authentication\Model\Sessions;
use FuzeWorks\Authentication\Model\Users;
use FuzeWorks\Config;
use FuzeWorks\ConfigORM\ConfigORM;
use FuzeWorks\Database;
use FuzeWorks\DatabaseEngine\MongoEngine;
use FuzeWorks\DatabaseEngine\PDOEngine;
use FuzeWorks\Factory;
use FuzeWorks\iPluginHeader;
use FuzeWorks\Priority;
use FuzeWorks\Router;
class AuthenticationPlugin implements iPluginHeader
{
public Users $users;
public Sessions $sessions;
public ConfigORM $config;
protected string $authURL;
/**
* Starts the plugin and integrates it into the other systems of FuzeWorks.
*
* Particularly, AuthenticationPlugin first loads the MVC-components, and adds its internal directories to them.
* It then registers the /auth URL with Router and loads all the sub-components such as Users management, Sessions management and more.
*/
public function init()
{
// Load the basic requirements for every request
/** @var Config $config */
$config = Factory::getInstance('config');
$views = Factory::getInstance('views');
$controllers = Factory::getInstance('controllers');
$layout = Factory::getInstance("layouts");
// Determine path and add to components
$pluginPath = dirname(__DIR__, 3);
$config->addComponentPath($pluginPath, Priority::LOWEST);
$views->addComponentPath($pluginPath . DS . "view", Priority::LOWEST);
$controllers->addComponentPath($pluginPath . DS . "controller", Priority::LOWEST);
$layout->addComponentPath($pluginPath . DS . "layout", Priority::LOWEST);
// Open the configuration
$this->config = $config->getConfig('authentication');
// Settle auth URL
$webURL = $config->getConfig("web")->get("base_url");
$auth_selector = $this->config->get("auth_url");
$this->authURL = $webURL . "/" . $auth_selector;
// If the plugin is disabled, stop here
if ($this->config->get('auth_enabled') !== true)
return;
// Fetch Driver type and determine type
/** @var Database $databases */
$databases = Factory::getInstance("databases");
$databaseConnection = $this->config->get("database_group");
$engine = $databases->get($databaseConnection);
if ($engine instanceof PDOEngine)
$driver = Driver::PDO;
elseif ($engine instanceof MongoEngine)
$driver = Driver::MONGO;
else
$driver = Driver::UNKNOWN;
// Load submodules
$this->users = new Users($engine, $driver, $this);
$this->sessions = new Sessions($engine, $driver, $this->config, $this->users);
// Load dependencies which are needed upon every request with the plugin enabled
/** @var Router $router */
$router = Factory::getInstance('router');
// Register route and related views and controllers
$webString = $auth_selector . '(|\/(?P<viewMethod>.*?)(|\/(?P<viewParameters>.*?)))';
$router->addRoute($webString, ['viewType' => 'html', 'viewName' => 'authentication'], Priority::HIGH);
// And register the tracy bridge
if (class_exists('Tracy\Debugger', true))
new AuthenticationTracyBridge($this);
}
/**
* Method that returns on which URL authentication is set to listen to.
*
* By default, this is /auth , but this can be configured in the authentication configuration file.
*
* @return string
*/
public function getAuthenticationURL(): string
{
return $this->authURL;
}
/**
* Returns the authentication configuration file.
*
* @return ConfigORM
*/
public function getConfig(): ConfigORM
{
return $this->config;
}
/**
* @inheritDoc
*/
public function getName(): string
{
return 'auth';
}
/**
* @inheritDoc
*/
public function getClassesPrefix(): ?string
{
return null;
}
/**
* @inheritDoc
*/
public function getSourceDirectory(): ?string
{
return null;
}
/**
* @inheritDoc
*/
public function getPluginClass(): ?string
{
return null;
}
/**
* Returns this plugin in accordance with FuzeWorks\Plugins
*
* @return $this
*/
public function getPlugin(): AuthenticationPlugin
{
return $this;
}
}