1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-08-03 22:57:27 +02:00

Let parent class 'UserManager' manage database connection for 'Auth'

This commit is contained in:
Marco
2017-02-21 08:40:30 +01:00
parent 6a15679238
commit 9252bee030
2 changed files with 31 additions and 16 deletions

View File

@@ -33,8 +33,6 @@ final class Auth extends UserManager {
const THROTTLE_ACTION_CONSUME_TOKEN = 'confirm_email'; const THROTTLE_ACTION_CONSUME_TOKEN = 'confirm_email';
const HTTP_STATUS_CODE_TOO_MANY_REQUESTS = 429; const HTTP_STATUS_CODE_TOO_MANY_REQUESTS = 429;
/** @var PdoDatabase the database connection that will be used */
private $db;
/** @var boolean whether HTTPS (TLS/SSL) will be used (recommended) */ /** @var boolean whether HTTPS (TLS/SSL) will be used (recommended) */
private $useHttps; private $useHttps;
/** @var boolean whether cookies should be accessible via client-side scripts (*not* recommended) */ /** @var boolean whether cookies should be accessible via client-side scripts (*not* recommended) */
@@ -47,24 +45,13 @@ final class Auth extends UserManager {
private $throttlingTimeBucketSize; private $throttlingTimeBucketSize;
/** /**
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection that will be used * @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
* @param bool $useHttps whether HTTPS (TLS/SSL) will be used (recommended) * @param bool $useHttps whether HTTPS (TLS/SSL) will be used (recommended)
* @param bool $allowCookiesScriptAccess whether cookies should be accessible via client-side scripts (*not* recommended) * @param bool $allowCookiesScriptAccess whether cookies should be accessible via client-side scripts (*not* recommended)
* @param string $ipAddress the IP address that should be used instead of the default setting (if any), e.g. when behind a proxy * @param string $ipAddress the IP address that should be used instead of the default setting (if any), e.g. when behind a proxy
*/ */
public function __construct($databaseConnection, $useHttps = false, $allowCookiesScriptAccess = false, $ipAddress = null) { public function __construct($databaseConnection, $useHttps = false, $allowCookiesScriptAccess = false, $ipAddress = null) {
if ($databaseConnection instanceof PdoDatabase) { parent::__construct($databaseConnection);
$this->db = $databaseConnection;
}
elseif ($databaseConnection instanceof PdoDsn) {
$this->db = PdoDatabase::fromDsn($databaseConnection);
}
elseif ($databaseConnection instanceof \PDO) {
$this->db = PdoDatabase::fromPdo($databaseConnection, true);
}
else {
throw new \InvalidArgumentException('The database connection must be an instance of either `PdoDatabase`, `PdoDsn` or `PDO`');
}
$this->useHttps = $useHttps; $this->useHttps = $useHttps;
$this->allowCookiesScriptAccess = $allowCookiesScriptAccess; $this->allowCookiesScriptAccess = $allowCookiesScriptAccess;

View File

@@ -8,5 +8,33 @@
namespace Delight\Auth; namespace Delight\Auth;
use Delight\Db\PdoDatabase;
use Delight\Db\PdoDsn;
/** Abstract base class for components implementing user management */ /** Abstract base class for components implementing user management */
abstract class UserManager {} abstract class UserManager {
/** @var PdoDatabase the database connection to operate on */
protected $db;
/**
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
*/
protected function __construct($databaseConnection) {
if ($databaseConnection instanceof PdoDatabase) {
$this->db = $databaseConnection;
}
elseif ($databaseConnection instanceof PdoDsn) {
$this->db = PdoDatabase::fromDsn($databaseConnection);
}
elseif ($databaseConnection instanceof \PDO) {
$this->db = PdoDatabase::fromPdo($databaseConnection, true);
}
else {
$this->db = null;
throw new \InvalidArgumentException('The database connection must be an instance of either `PdoDatabase`, `PdoDsn` or `PDO`');
}
}
}