Skip to content

Commit

Permalink
Two separate exception class will thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
bedus-creation committed Sep 1, 2020
1 parent 503baba commit 2d60c16
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 45 deletions.
32 changes: 31 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ and then following api are available to you.
This **sync** the roles, if a user has admin role and then you send only editor, it will remove admin role and then user will only have editor role. Send all roles to update the roles.
* ```public function getRoles(): array```
It returns roles in array.
* ```public function hasGotRole(array $roles): bool```
* ```public function hasGotRole(array $roles): bool```

## Exception
It throws following exception as below.
| Exception | Remarks |
| --- | --- |
| Aammui\RolePermission\Exception\UserNotLogin | User is not logged in yet. |
| Aammui\RolePermission\Exception\RoleDoesNotExist | A function or route is protected by a role, and logged in user doesn't have that role yet. |

#### UseCase: Exception uses for user redirection.
Suppose we want to redirect not logged in user to login page, which can be done using handling exception in ```app\Exceptions\Handler.php``` class. The purpose of this exception make available is to support full customization. For example you may want to redirect to login page for that user whom don't have right role, or you simply only want to show 403 page.
```php
// App\Exceptions\Handler.php;
use Aammui\RolePermission\Exception\UserNotLogin;
use Aammui\RolePermission\Exception\RoleDoesNotExist;

....

public function render($request, Throwable $exception)
{
if ($exception instanceof UserNotLogin) {
return redirect('/login');
}

if ($exception instanceof RoleDoesNotExist) {
session()->flash("User doesn't have right role and permission.");
return redirect()->back();
}
return parent::render($request, $exception);
}
```
13 changes: 13 additions & 0 deletions src/Exception/RoleDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aammui\RolePermission\Exception;

use Symfony\Component\HttpKernel\Exception\HttpException;

class RoleDoesNotExistException extends HttpException
{
public function __construct()
{
parent::__construct(403, 'User does not have the right roles.', null, []);
}
}
19 changes: 0 additions & 19 deletions src/Exception/UnauthorizedException.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exception/UserNotLoginException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aammui\RolePermission\Exception;

use Symfony\Component\HttpKernel\Exception\HttpException;

class UserNotLoginException extends HttpException
{
public function __construct()
{
parent::__construct(403, 'User is not logged in.', null, []);
}
}
10 changes: 5 additions & 5 deletions src/Middleware/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Aammui\RolePermission\Middleware;

use Aammui\RolePermission\Exception\RoleDoesNotExistException;
use Aammui\RolePermission\Exception\UnauthorizedException;
use Aammui\RolePermission\Exception\UserNotLoginException;
use Illuminate\Support\Facades\Auth;
use Closure;

Expand All @@ -18,17 +20,15 @@ class Role
*/
public function handle($request, Closure $next, $role = null)
{

if (Auth::guest()) {
throw UnauthorizedException::notLoggedIn();
throw new UserNotLoginException();
}

$role = $role ?? 'guest';
$roles = explode('|', $role);
// dd($roles);
// dd(auth()->user()->hasGotRole($roles));

if (!auth()->user()->hasGotRole($roles)) {
throw UnauthorizedException::forRoles();
throw new RoleDoesNotExistException();
}

return $next($request);
Expand Down
56 changes: 36 additions & 20 deletions tests/RoleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Aammui\RolePermission\Tests;

use Aammui\RolePermission\Exception\RoleDoesNotExistException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Aammui\RolePermission\Exception\UnauthorizedException;
use Aammui\RolePermission\Exception\UserNotLoginException;
use Aammui\RolePermission\Middleware\Role as RoleMiddleware;
use Illuminate\Support\Facades\Auth;

Expand All @@ -19,18 +20,6 @@ public function setUp(): void
$this->roleMiddleware = new RoleMiddleware();
}

/** @test */
public function a_guest_cannot_access_a_route_protected_by_rolemiddleware()
{
$this->assertEquals(
$this->runMiddleware(
$this->roleMiddleware,
'testRole'
),
403
);
}

/** @test */
public function a_user_can_access_a_route_protected_by_role_middleware_if_have_this_role()
{
Expand All @@ -51,12 +40,39 @@ public function a_user_can_access_a_route_protected_by_role_middleware_if_have_t

protected function runMiddleware($middleware, $parameter)
{
try {
return $middleware->handle(new Request(), function () {
return (new Response())->setContent('<html></html>');
}, $parameter)->status();
} catch (UnauthorizedException $e) {
return $e->getStatusCode();
}
return $middleware->handle(new Request(), function () {
return (new Response())->setContent('<html></html>');
}, $parameter)->status();
}

/**
* A guest cannot access a route protected by rolemiddleware
*
* @test
*/
public function UserNotLogin_exception_is_thrown_when_user_not_login()
{
$this->expectException(UserNotLoginException::class);

$this->roleMiddleware->handle(new Request(), function () {
return (new Response())->setContent('<html></html>');
}, 'testRole')->status();
}

/**
* A User cannot access a route protected by rolemiddleware
*
* @test
*/
public function RoleDoesNotExist_exception_is_thrown_when_user_not_login()
{
$this->expectException(RoleDoesNotExistException::class);

$user = User::create(['email' => '[email protected]']);
Auth::login($user);

$this->roleMiddleware->handle(new Request(), function () {
return (new Response())->setContent('<html></html>');
}, 'testRole')->status();
}
}

0 comments on commit 2d60c16

Please sign in to comment.