php-censor/src/Security/Authentication/Service.php

107 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
2016-12-29 06:43:02 +01:00
namespace PHPCensor\Security\Authentication;
2018-03-04 09:56:02 +01:00
use PHPCensor\Config;
/**
* Authentication facade.
*
2016-12-29 06:43:02 +01:00
* @author Adirelle <adirelle@gmail.com>
*/
class Service
{
/**
* @var Service
*/
static private $instance;
2017-11-05 15:48:36 +01:00
/**
* Return the service singleton.
*
* @return Service
*/
public static function getInstance()
{
if (self::$instance === null) {
$config = Config::getInstance()->get(
'php-censor.security.auth_providers',
[
'internal' => [
'type' => 'internal'
]
]
);
$providers = [];
foreach ($config as $key => $providerConfig) {
$providers[$key] = self::buildProvider($key, $providerConfig);
}
self::$instance = new self($providers);
}
2016-12-29 06:43:02 +01:00
return self::$instance;
}
2017-11-05 15:48:36 +01:00
/**
* Create a provider from a given configuration.
*
* @param string $key
* @param string|array $config
2017-11-05 15:48:36 +01:00
*
* @return UserProviderInterface
*/
public static function buildProvider($key, $config)
{
2016-07-17 12:55:42 +02:00
$class = ucfirst($config['type']);
2016-12-29 06:43:02 +01:00
if (class_exists('\\PHPCensor\\Security\\Authentication\\UserProvider\\' . $class)) {
$class = '\\PHPCensor\\Security\\Authentication\\UserProvider\\' . $class;
}
return new $class($key, $config);
}
2017-11-05 15:48:36 +01:00
/**
* The table of providers.
*
* @var array
*/
private $providers;
2017-11-05 15:48:36 +01:00
/**
* Initialize the service.
*
* @param array $providers
*/
public function __construct(array $providers)
{
$this->providers = $providers;
}
2017-11-05 15:48:36 +01:00
/**
* Return all providers.
*
* @return UserProviderInterface[]
*/
public function getProviders()
{
return $this->providers;
}
2017-11-05 15:48:36 +01:00
/**
* Return the user providers that allows password authentication.
*
* @return LoginPasswordProviderInterface[]
*/
public function getLoginPasswordProviders()
{
$providers = [];
foreach ($this->providers as $key => $provider) {
if ($provider instanceof LoginPasswordProviderInterface) {
$providers[$key] = $provider;
}
}
return $providers;
}
}