Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add authenticate method to the guards #13651

Merged
merged 4 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Illuminate/Auth/AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Auth;

use Exception;

class AuthenticationException extends Exception
{
/**
* The guard instance.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;

/**
* Create a new authentication exception.
*
* @param \Illuminate\Contracts\Auth\Guard|null $guard
*/
public function __construct($guard = null)
{
$this->guard = $guard;

parent::__construct('Unauthenticated.');
}

/**
* Get the guard instance.
*
* @return \Illuminate\Contracts\Auth\Guard|null
*/
public function guard()
{
return $this->guard;
}
}
20 changes: 19 additions & 1 deletion src/Illuminate/Auth/GuardHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ trait GuardHelpers
*/
protected $provider;

/**
* Determine if the current user is authenticated.
*
* @return \Illuminate\Contracts\Auth\Authenticatable
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function authenticate()
{
if (! is_null($user = $this->user())) {
return $user;
}

throw new AuthenticationException($this);
}

/**
* Determine if the current user is authenticated.
*
Expand Down Expand Up @@ -59,10 +75,12 @@ public function id()
* Set the current user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
* @return $this
*/
public function setUser(AuthenticatableContract $user)
{
$this->user = $user;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,13 +691,15 @@ public function getUser()
* Set the current user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
* @return $this
*/
public function setUser(AuthenticatableContract $user)
{
$this->user = $user;

$this->loggedOut = false;

return $this;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use Illuminate\Http\Response;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Exception\HttpResponseException;
Expand Down Expand Up @@ -100,6 +101,8 @@ public function render($request, Exception $e)
return $e->getResponse();
} elseif ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof AuthorizationException) {
$e = new HttpException(403, $e->getMessage());
} elseif ($e instanceof ValidationException && $e->getResponse()) {
Expand Down Expand Up @@ -175,6 +178,22 @@ protected function convertExceptionToResponse(Exception $e)
return SymfonyResponse::create($decorated, $e->getStatusCode(), $e->getHeaders());
}

/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function unauthenticated($request, AuthenticationException $e)
{
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}

/**
* Get the html response content.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ public function testLoginFiresLoginEvent()
$mock->login($user);
}

public function testAuthenticateReturnsUserWhenUserIsNotNull()
{
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$guard = $this->getGuard()->setUser($user);

$this->assertEquals($user, $guard->authenticate());
}

/**
* @expectedException \Illuminate\Auth\AuthenticationException
*/
public function testAuthenticateThrowsWhenUserIsNull()
{
$guard = $this->getGuard();
$guard->getSession()->shouldReceive('get')->once()->andReturn(null);

$guard->authenticate();
}

public function testIsAuthedReturnsTrueWhenUserIsNotNull()
{
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
Expand Down