diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-16 01:09:44 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-16 01:09:44 +0200 |
| commit | 1e438654005bd408447dc19c8a96099d228bb1aa (patch) | |
| tree | dc86181a0d3e130e9c18c911ca3cdac39cd6e4b6 /core/classes | |
| parent | 51b3554ff2988ac61ba3f604431e09941cd95545 (diff) | |
| download | classroom-1e438654005bd408447dc19c8a96099d228bb1aa.tar.gz classroom-1e438654005bd408447dc19c8a96099d228bb1aa.tar.bz2 classroom-1e438654005bd408447dc19c8a96099d228bb1aa.zip | |
working on user management
Diffstat (limited to 'core/classes')
| -rw-r--r-- | core/classes/authentication/Core/PasswordTrait.php | 10 | ||||
| -rw-r--r-- | core/classes/authentication/Core/UserManager.php | 33 | ||||
| -rw-r--r-- | core/classes/authentication/Token/UserToken.php | 12 | ||||
| -rw-r--r-- | core/classes/authentication/User.php | 12 |
4 files changed, 60 insertions, 7 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()) { diff --git a/core/classes/authentication/Token/UserToken.php b/core/classes/authentication/Token/UserToken.php index 6ce2eb6..520d4ad 100644 --- a/core/classes/authentication/Token/UserToken.php +++ b/core/classes/authentication/Token/UserToken.php @@ -15,16 +15,28 @@ class UserToken implements UserTokenInterface */ private $user; + public function __construct(UserInterface $user) { $this->user = $user; } + + /** getUser + * + */ public function getUser(): UserInterface { return $this->user; } + + /** serialize() + * + * serializes the user structure (with user's property values) + * ... then it will be saved into session[DEFAULT_PREFIX_KEY] + * + */ public function serialize(): string { return serialize($this); diff --git a/core/classes/authentication/User.php b/core/classes/authentication/User.php index 8f5aecb..4aef375 100644 --- a/core/classes/authentication/User.php +++ b/core/classes/authentication/User.php @@ -65,7 +65,8 @@ class User implements UserInterface * ------------------------------------------------------------------------- */ - /** setUserName + /** setUserName() + * * @param string $userName * @return User */ @@ -75,7 +76,8 @@ class User implements UserInterface return $this; } - /** setPassword + /** setPassword() + * * @param string $password * @return User */ @@ -85,7 +87,8 @@ class User implements UserInterface return $this; } - /** setRoles + /** setRoles() + * * @param array $roles * @return User */ @@ -95,7 +98,8 @@ class User implements UserInterface return $this; } - /** + /** setEnabled + * * @param bool $enabled * @return User */ |
