From e588401cf0c12c73bcb22478e60eadbd96ad7e5b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 14 Jul 2018 09:58:06 +0200 Subject: [PATCH] Refactor a security code example to make it easier to understand --- security/custom_provider.rst | 37 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/security/custom_provider.rst b/security/custom_provider.rst index 491a93b0cb1..e7c3834b9e7 100644 --- a/security/custom_provider.rst +++ b/security/custom_provider.rst @@ -133,21 +133,7 @@ Here's an example of how this might look:: { public function loadUserByUsername($username) { - // make a call to your webservice here - $userData = ... - // pretend it returns an array on success, false if there is no user - - if ($userData) { - $password = '...'; - - // ... - - return new WebserviceUser($username, $password, $salt, $roles); - } - - throw new UsernameNotFoundException( - sprintf('Username "%s" does not exist.', $username) - ); + return $this->fetchUser($username); } public function refreshUser(UserInterface $user) @@ -158,13 +144,32 @@ Here's an example of how this might look:: ); } - return $this->loadUserByUsername($user->getUsername()); + return $this->fetchUser($username); } public function supportsClass($class) { return WebserviceUser::class === $class; } + + private function fetchUser($username) + { + // make a call to your webservice here + $userData = ... + // pretend it returns an array on success, false if there is no user + + if ($userData) { + $password = '...'; + + // ... + + return new WebserviceUser($username, $password, $salt, $roles); + } + + throw new UsernameNotFoundException( + sprintf('Username "%s" does not exist.', $username) + ); + } } Create a Service for the User Provider