diff --git a/readme.md b/readme.md index b425f9c..54d4432 100644 --- a/readme.md +++ b/readme.md @@ -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``` \ No newline at end of file +* ```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); +} +``` \ No newline at end of file diff --git a/src/Exception/RoleDoesNotExistException.php b/src/Exception/RoleDoesNotExistException.php new file mode 100644 index 0000000..1a20a44 --- /dev/null +++ b/src/Exception/RoleDoesNotExistException.php @@ -0,0 +1,13 @@ +user()->hasGotRole($roles)); + if (!auth()->user()->hasGotRole($roles)) { - throw UnauthorizedException::forRoles(); + throw new RoleDoesNotExistException(); } return $next($request); diff --git a/tests/RoleMiddlewareTest.php b/tests/RoleMiddlewareTest.php index 094b291..a3c69ec 100644 --- a/tests/RoleMiddlewareTest.php +++ b/tests/RoleMiddlewareTest.php @@ -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; @@ -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() { @@ -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(''); - }, $parameter)->status(); - } catch (UnauthorizedException $e) { - return $e->getStatusCode(); - } + return $middleware->handle(new Request(), function () { + return (new Response())->setContent(''); + }, $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(''); + }, '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' => 'tyest@gmail.com']); + Auth::login($user); + + $this->roleMiddleware->handle(new Request(), function () { + return (new Response())->setContent(''); + }, 'testRole')->status(); } }