From ecec2d07d7ea6519b17989590b3c72a107d4b340 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 8 May 2019 08:25:11 +0000 Subject: [PATCH] Add the enabled flag to the api user and base user. Remove the enabled methods from site user since base class has them. Add checking if user account is enabled during authentication of api user. #194 --- catalyst/api-bundle/Entity/User.php | 23 ++++------- .../Security/APIKeyAuthenticator.php | 6 +++ catalyst/auth-bundle/Entity/User.php | 24 ++++++++++++ src/Entity/User.php | 38 ------------------- 4 files changed, 38 insertions(+), 53 deletions(-) diff --git a/catalyst/api-bundle/Entity/User.php b/catalyst/api-bundle/Entity/User.php index 712b5d5d..d47d8892 100644 --- a/catalyst/api-bundle/Entity/User.php +++ b/catalyst/api-bundle/Entity/User.php @@ -49,6 +49,12 @@ class User extends BaseUser implements UserInterface */ protected $roles; + // enabled + /** + * @ORM\Column(type="boolean") + */ + protected $enabled; + public function __construct() { // generate keys @@ -57,8 +63,9 @@ class User extends BaseUser implements UserInterface // set date created $this->date_create = new DateTime(); - + $this->roles = new ArrayCollection(); + $this->enabled = true; } public function getID() @@ -99,20 +106,6 @@ class User extends BaseUser implements UserInterface return $this->name; } - //public function getRoles() - //{ - // $str_roles = []; - // foreach ($this->roles as $role) - // $str_roles[] = $role->getID(); - - // return $str_roles; - //} - - //public function getRoleObjects() - //{ - // return $this->roles; - //} - public function getDateCreate() { return $this->date_create; diff --git a/catalyst/api-bundle/Security/APIKeyAuthenticator.php b/catalyst/api-bundle/Security/APIKeyAuthenticator.php index f40728df..27d1eaf1 100644 --- a/catalyst/api-bundle/Security/APIKeyAuthenticator.php +++ b/catalyst/api-bundle/Security/APIKeyAuthenticator.php @@ -131,6 +131,12 @@ class APIKeyAuthenticator implements SimplePreAuthenticatorInterface, Authentica // check if signature is valid $this->validateSignature($creds, $user->getSecretKey()); + // check if user is enabled + if (!$user->isEnabled()) + { + throw new CustomUserMessageAuthenticationException('User account is disabled'); + } + // $user = $user_provider->loadUserByUsername($username); return new PreAuthenticatedToken( diff --git a/catalyst/auth-bundle/Entity/User.php b/catalyst/auth-bundle/Entity/User.php index fad0699c..ad05fb16 100644 --- a/catalyst/auth-bundle/Entity/User.php +++ b/catalyst/auth-bundle/Entity/User.php @@ -5,10 +5,12 @@ namespace Catalyst\AuthBundle\Entity; abstract class User { protected $roles; + protected $enabled; public function __construct() { $this->roles = new ArrayCollection(); + $this->enabled = true; } public function getRoles() @@ -25,4 +27,26 @@ abstract class User return $this->roles; } + public function addRole(Role $role) + { + $this->roles->add($role); + return $this; + } + + public function clearRoles() + { + $this->roles->clear(); + return $this; + } + + public function setEnabled($enabled = true) + { + $this->enabled = $enabled; + return $this; + } + + public function isEnabled() + { + return $this->enabled; + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index bc106433..ccb4ddc8 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -153,33 +153,6 @@ class User extends BaseUser implements AdvancedUserInterface, Serializable return null; } - public function addRole(Role $role) - { - $this->roles->add($role); - return $this; - } - - public function clearRoles() - { - $this->roles->clear(); - return $this; - } - - //public function getRoles() - //{ - // // has to return set of strings because symfony is trying to move away from role objects - // $str_roles = []; - // foreach ($this->roles as $role) - // $str_roles[] = $role->getID(); - - // return $str_roles; - //} - - //public function getRoleObjects() - //{ - // return $this->roles; - //} - public function addHub(Hub $hub) { $this->hubs->add($hub); @@ -226,17 +199,6 @@ class User extends BaseUser implements AdvancedUserInterface, Serializable return true; } - public function setEnabled($enabled = true) - { - $this->enabled = $enabled; - return $this; - } - - public function isEnabled() - { - return $this->enabled; - } - public function serialize() { return serialize([