Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/5.2' into 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 23, 2016
2 parents bf5303f + 16def48 commit e70ea00
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
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
6 changes: 4 additions & 2 deletions src/Illuminate/Http/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ public function hashName($path = null)
* Create a new file instance from a base instance.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
* @param bool $test
* @return static
*/
public static function createFromBase(SymfonyUploadedFile $file)
public static function createFromBase(SymfonyUploadedFile $file, $test = false)
{
return $file instanceof static ? $file : new static(
$file->getPathname(),
$file->getClientOriginalName(),
$file->getClientMimeType(),
$file->getClientSize(),
$file->getError()
$file->getError(),
$test
);
}
}
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

0 comments on commit e70ea00

Please sign in to comment.