File tree Expand file tree Collapse file tree 6 files changed +22
-23
lines changed Expand file tree Collapse file tree 6 files changed +22
-23
lines changed Original file line number Diff line number Diff line change @@ -40,9 +40,9 @@ A Laravel package that captures outgoing mail and stores it for in-app viewing.
4040The published ` config/inbox.php ` file exposes several options:
4141
4242- ` INBOX_ENABLED ` &mdash ; enable the inbox even in production (defaults to non-production only).
43- - ` INBOX_PUBLIC ` &mdash ; bypass authorization and allow public access.
4443- ` INBOX_GATE ` &mdash ; ability checked by the ` mailbox.authorize ` middleware (defaults to ` viewMailbox ` ).
4544- ` INBOX_DASHBOARD_ROUTE ` &mdash ; URI where the dashboard is mounted (` /mailbox ` by default).
45+ - ` INBOX_REDIRECT ` &mdash ; URI where the user is redirected when they are unauthorized (defaults to Laravel's Forbidden Page).
4646- ` INBOX_STORE_DRIVER ` & ` INBOX_FILE_PATH ` &mdash ; storage driver and path for captured messages.
4747- ` INBOX_RETENTION ` &mdash ; number of seconds before stored messages are purged.
4848
Original file line number Diff line number Diff line change 22
33return [
44 'enabled ' => env ('INBOX_ENABLED ' , env ('APP_ENV ' ) !== 'production ' ),
5- 'public ' => env ('INBOX_PUBLIC ' , false ),
65 'store ' => [
76 'driver ' => env ('INBOX_STORE_DRIVER ' , 'file ' ),
87 'resolvers ' => [
1716 'seconds ' => (int ) env ('INBOX_RETENTION ' , 60 * 60 * 24 ),
1817 ],
1918 'gate ' => env ('INBOX_GATE ' , 'viewMailbox ' ),
19+ 'unauthorized_redirect ' => env ('INBOX_REDIRECT ' , null ),
2020 'route ' => env ('INBOX_DASHBOARD_ROUTE ' , 'mailbox ' ),
2121 'middleware ' => ['web ' ],
2222];
Original file line number Diff line number Diff line change @@ -9,16 +9,17 @@ class AuthorizeInboxMiddleware
99{
1010 public function handle ($ request , Closure $ next )
1111 {
12- if (config ('inbox.public ' , false )) {
13- return $ next ($ request );
14- }
15-
1612 $ ability = config ('inbox.gate ' , 'viewMailbox ' );
17- // Let Gate decide (works with or without authenticated user; $user can be null)
18- if (Gate::allows ($ ability )) {
19- return $ next ($ request );
13+
14+ if (! Gate::allows ($ ability )) {
15+ $ redirect = config ('inbox.unauthorized_redirect ' );
16+
17+ if ($ redirect ) {
18+ return redirect ($ redirect );
19+ }
20+ abort (403 );
2021 }
2122
22- abort ( 403 );
23+ return $ next ( $ request );
2324 }
2425}
Original file line number Diff line number Diff line change 11<?php
22
3- use Illuminate \Support \Facades \Gate ;
43use Illuminate \Support \Facades \Route ;
54use Redberry \MailboxForLaravel \CaptureService ;
65use Redberry \MailboxForLaravel \Http \Controllers \AssetController ;
@@ -50,13 +49,6 @@ function storeMessage(): array
5049 $ this ->get ("/mailbox/messages/ {$ key }/attachments/missing.txt " )->assertNotFound ();
5150 });
5251
53- it ('rejects unauthorized access when middleware denies ' , function () {
54- Gate::shouldReceive ('allows ' )->with ('viewMailbox ' )->andReturn (false );
55- config ()->set ('inbox.public ' , false );
56-
57- $ this ->get ('/mailbox/messages/abc/attachments/file.txt ' )->assertForbidden ();
58- });
59-
6052 it ('streams large assets without loading entire file into memory ' , function () {
6153 $ file = tempnam (sys_get_temp_dir (), 'inbox- ' );
6254 file_put_contents ($ file , str_repeat ('A ' , 1024 * 1024 ));
Original file line number Diff line number Diff line change 2121 $ this ->get ('/mailbox-test ' )->assertForbidden ();
2222 });
2323
24- it ('allows access when config( inbox.public)=true ' , function () {
25- config ()->set ('inbox.public ' , true );
26- Gate::shouldReceive ('allows ' )->never ( );
24+ it ('redirects to 403 page when no inbox.unauthorized_redirect config is set ' , function () {
25+ config ()->set ('inbox.unauthorized_redirect ' , null );
26+ Gate::shouldReceive ('allows ' )->with ( ' viewMailbox ' )-> andReturn ( false );
2727
28- $ this ->get ('/mailbox-test ' )->assertOk ();
28+ $ this ->get ('/mailbox-test ' )->assertForbidden ();
29+ });
30+
31+ it ('redirects to inbox.unauthorized_redirect page when set in config ' , function () {
32+ config ()->set ('inbox.unauthorized_redirect ' , '/custom-unauthorized ' );
33+ Gate::shouldReceive ('allows ' )->with ('viewMailbox ' )->andReturn (false );
34+
35+ $ this ->get ('/mailbox-test ' )->assertRedirect ('/custom-unauthorized ' );
2936 });
3037
3138 it ('denies access in production when config forbids public access ' , function () {
Original file line number Diff line number Diff line change 5353 it ('merges default config values correctly ' , function () {
5454 expect (config ('inbox.store.driver ' ))->toBe ('file ' );
5555 expect (config ('inbox.middleware ' ))->toBe (['web ' ]);
56- expect (config ('inbox.public ' ))->toBeFalse ();
5756 });
5857});
You can’t perform that action at this time.
0 commit comments