Skip to content

Commit 5cb68fc

Browse files
committed
minor #10066 Refactor a security code example to make it easier to understand (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Refactor a security code example to make it easier to understand This replaces #10064 and extracts the common code into a new method. Commits ------- e588401 Refactor a security code example to make it easier to understand
2 parents 2d1cecb + e588401 commit 5cb68fc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

security/custom_provider.rst

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,7 @@ Here's an example of how this might look::
133133
{
134134
public function loadUserByUsername($username)
135135
{
136-
// make a call to your webservice here
137-
$userData = ...
138-
// pretend it returns an array on success, false if there is no user
139-
140-
if ($userData) {
141-
$password = '...';
142-
143-
// ...
144-
145-
return new WebserviceUser($username, $password, $salt, $roles);
146-
}
147-
148-
throw new UsernameNotFoundException(
149-
sprintf('Username "%s" does not exist.', $username)
150-
);
136+
return $this->fetchUser($username);
151137
}
152138

153139
public function refreshUser(UserInterface $user)
@@ -158,13 +144,32 @@ Here's an example of how this might look::
158144
);
159145
}
160146

161-
return $this->loadUserByUsername($user->getUsername());
147+
return $this->fetchUser($username);
162148
}
163149

164150
public function supportsClass($class)
165151
{
166152
return WebserviceUser::class === $class;
167153
}
154+
155+
private function fetchUser($username)
156+
{
157+
// make a call to your webservice here
158+
$userData = ...
159+
// pretend it returns an array on success, false if there is no user
160+
161+
if ($userData) {
162+
$password = '...';
163+
164+
// ...
165+
166+
return new WebserviceUser($username, $password, $salt, $roles);
167+
}
168+
169+
throw new UsernameNotFoundException(
170+
sprintf('Username "%s" does not exist.', $username)
171+
);
172+
}
168173
}
169174

170175
Create a Service for the User Provider

0 commit comments

Comments
 (0)