summaryrefslogtreecommitdiff
path: root/core/classes/authentication/Core
diff options
context:
space:
mode:
Diffstat (limited to 'core/classes/authentication/Core')
-rw-r--r--core/classes/authentication/Core/PasswordTrait.php10
-rw-r--r--core/classes/authentication/Core/UserManager.php33
2 files changed, 40 insertions, 3 deletions
diff --git a/core/classes/authentication/Core/PasswordTrait.php b/core/classes/authentication/Core/PasswordTrait.php
index 18d67b5..2d3cbd2 100644
--- a/core/classes/authentication/Core/PasswordTrait.php
+++ b/core/classes/authentication/Core/PasswordTrait.php
@@ -10,21 +10,27 @@
trait PasswordTrait
{
- private $cost = 10;
+
+ private $cost = 10; // Cost must be in the range of 4-31
+ // a cost value in the range of 8-11
+ // is a good balance between performance and security
+
public function cryptPassword(string $plainPassword): string
{
return password_hash($plainPassword, PASSWORD_BCRYPT, ['cost' => $this->cost]);
}
+
public function isPasswordValid(UserInterface $user, string $plainPassword): bool
{
return password_verify($plainPassword, $user->getPassword());
}
+
public function setCost(int $cost): void
{
- if ($cost < 4 || $cost > 12) {
+ if ($cost < 4 || $cost > 31) {
throw new \InvalidArgumentException('Cost must be in the range of 4-31.');
}
$this->cost = $cost;
diff --git a/core/classes/authentication/Core/UserManager.php b/core/classes/authentication/Core/UserManager.php
index 3b380b8..22a87f5 100644
--- a/core/classes/authentication/Core/UserManager.php
+++ b/core/classes/authentication/Core/UserManager.php
@@ -15,6 +15,7 @@ class UserManager implements UserManagerInterface
use PasswordTrait;
+
public function __construct()
{
if (session_status() === PHP_SESSION_NONE) {
@@ -22,6 +23,11 @@ class UserManager implements UserManagerInterface
}
}
+
+ /** getUserToken()
+ * == get user from session
+ *
+ */
public function getUserToken(): ?UserTokenInterface
{
$userToken = null;
@@ -32,15 +38,31 @@ class UserManager implements UserManagerInterface
return $userToken;
}
+
+ /** hasUserToken()
+ * == is user loged in
+ *
+ */
public function hasUserToken(): bool
{
$key = UserTokenInterface::DEFAULT_PREFIX_KEY;
return (array_key_exists($key, $_SESSION) && unserialize($_SESSION[$key]) !== false);
}
+
+ /** isGranted
+ *
+ * checks if user is granded some role(s)
+ * from the array of roles that are passed
+ *
+ * ex. $token->isGranted(['editor', 'designer']) ...
+ * returns true if the user is editor or designer (or both)
+ *
+ */
public function isGranted(array $roles): bool
{
- if (!is_null($userToken = $this->getUserToken())) {
+ // if (!is_null($userToken = $this->getUserToken())) {
+ if (is_null($userToken = $this->getUserToken())) {
return false;
}
@@ -51,6 +73,10 @@ class UserManager implements UserManagerInterface
return false;
}
+
+ /** createUserToken()
+ * == serializes user and stores it into session
+ */
public function createUserToken(UserInterface $user): UserTokenInterface
{
$userToken = new UserToken($user);
@@ -59,6 +85,11 @@ class UserManager implements UserManagerInterface
return $userToken;
}
+
+ /** logout
+ * == clear session
+ *
+ */
public function logout(): void
{
if ($this->hasUserToken()) {