Every request, get the current logged in user (on the PC, no idea how they plan to implement this) and authenticate this user in the app (creating a new user if it doesn't exists yet).
Last active
December 29, 2015 23:21
-
-
Save wouterj/cd18ebe7a07ec49cea08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CurrentUserProvider implements UserProviderInterface | |
{ | |
// ... | |
public function loadByUsername($username) | |
{ | |
$user = $this->someBackend->findByUsername($username); | |
if (null === $user) { | |
$user = new User($username, null); | |
$this->someBackend->saveUser($user); | |
return $user; | |
} | |
return $user; | |
} | |
public function refreshUser(UserInterfacee $user) | |
{ | |
if (!$user instanceof CurrentUser) | |
throw new UnsupportedUserException; | |
return $this->someBackend->findByUsername($user->username()); | |
} | |
public function supports($class) | |
{ | |
return $class === CurrentUser::class; | |
} | |
} | |
class CurrentUserAuthentication implements SimplePreAuthenticatorInterface | |
{ | |
// get current logged in user and return the token | |
public function createToken(Request $request, $providerKey) | |
{ | |
return new PreAuthenticatedToken(get_user(), '', $providerKey); | |
} | |
// authenticate the previously created token | |
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) | |
{ | |
$username = $token->getUsername(); | |
if ('' === $username) { | |
return; | |
} | |
$user = $userProvider->loadByUsername($username); | |
return new PreAuthenticatedToken($user, null, $providerKey, $user->getRoles()); | |
} | |
public function supportsToken(TokenInterface $token, $providerKey) | |
{ | |
return $token instanceof PreAuthenticatedToken && $providerKey === $token->getProviderKey(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CurrentUserAuthenticator extends AbstractGuardAuthenticator | |
{ | |
// ... | |
public function getCredentials(Request $request) | |
{ | |
return ['username' => get_user()]; | |
} | |
public function getUser($credentials, UserProviderInterface $userProvider) | |
{ | |
$user = $this->someBackend->findByUsername($credentials['username']); | |
if (null === $user) { | |
$user = new User($credentials['username'], null); | |
$this->someBackend->saveUser($user); | |
return $user; | |
} | |
return $user; | |
} | |
public function checkCredentials($credentials, UserInterface $user) | |
{ | |
return true; | |
} | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) | |
{ | |
return null; | |
} | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) | |
{ | |
return new Response('Bummer! Authentication failed...', 403); | |
} | |
public function supportsRememberMe() | |
{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment