Skip to content

Commit

Permalink
Merge pull request #79 from adamwathan/allow-overriding-user
Browse files Browse the repository at this point in the history
Allow overriding user by returning a user from the callback
  • Loading branch information
adamwathan committed Oct 26, 2015
2 parents fa30313 + ab5b1ac commit 2c74b7b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function __construct($auth, $users, $identities)
public function login($providerAlias, $userDetails, $callback = null)
{
$user = $this->getUser($providerAlias, $userDetails);
if ($callback) {
$callback($user, $userDetails);
}
$user = $this->runCallback($callback, $user, $userDetails);
$this->updateUser($user, $providerAlias, $userDetails);
$this->auth->login($user);
}
Expand All @@ -31,6 +29,13 @@ protected function getUser($providerAlias, $details)
return $this->users->create();
}

protected function runCallback($callback, $user, $userDetails)
{
$callback = $callback ?: function () {};
$callbackUser = $callback($user, $userDetails);
return $callbackUser ?: $user;
}

protected function updateUser($user, $providerAlias, $details)
{
$this->users->store($user);
Expand Down
77 changes: 71 additions & 6 deletions tests/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ public function test_login_creates_new_user_if_no_matching_user_exists()
public function test_login_uses_existing_user_if_matching_user_exists()
{
$providerAlias = 'provider';
$auth = M::spy();
$users = M::spy();
$identities = M::spy();

$userDetails = new SocialNormUser([]);
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();

$identities->shouldReceive('userExists')->andReturn(true);
$identities->shouldReceive('getByProvider')->andReturn(new OAuthIdentity);
$users->shouldReceive('findByIdentity')->andReturn($user);
$auth = M::spy();

$users = M::spy([
'findByIdentity' => $user
]);

$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);

$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails);
Expand All @@ -54,4 +59,64 @@ public function test_login_uses_existing_user_if_matching_user_exists()
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($user);
}

public function test_if_a_user_is_returned_from_the_callback_that_user_is_used()
{
$providerAlias = 'provider';

$userDetails = new SocialNormUser([]);
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();

$auth = M::spy();

$users = M::spy([
'findByIdentity' => $user
]);

$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);

$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails, function () use ($otherUser) {
return $otherUser;
});

$users->shouldNotHaveReceived('create');
$users->shouldHaveReceived('store')->with($otherUser);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($otherUser);
}

public function test_if_nothing_is_returned_from_the_callback_the_found_or_created_user_is_used()
{
$providerAlias = 'provider';

$userDetails = new SocialNormUser([]);
$foundUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();

$auth = M::spy();

$users = M::spy([
'findByIdentity' => $foundUser
]);

$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);

$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails, function () {
return;
});

$users->shouldNotHaveReceived('create');
$users->shouldHaveReceived('store')->with($foundUser);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($foundUser);
}
}

0 comments on commit 2c74b7b

Please sign in to comment.