Skip to content

Commit 57b93b6

Browse files
authored
Merge pull request #21 from RedberryProducts/feat/redirect-url
Feat/redirect url
2 parents a773760 + 304aff3 commit 57b93b6

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ A Laravel package that captures outgoing mail and stores it for in-app viewing.
4040
The published `config/inbox.php` file exposes several options:
4141

4242
- `INBOX_ENABLED` — enable the inbox even in production (defaults to non-production only).
43-
- `INBOX_PUBLIC` — bypass authorization and allow public access.
4443
- `INBOX_GATE` — ability checked by the `mailbox.authorize` middleware (defaults to `viewMailbox`).
4544
- `INBOX_DASHBOARD_ROUTE` — URI where the dashboard is mounted (`/mailbox` by default).
45+
- `INBOX_REDIRECT` — URI where the user is redirected when they are unauthorized (defaults to Laravel's Forbidden Page).
4646
- `INBOX_STORE_DRIVER` & `INBOX_FILE_PATH` — storage driver and path for captured messages.
4747
- `INBOX_RETENTION` — number of seconds before stored messages are purged.
4848

config/inbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
return [
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' => [
@@ -17,6 +16,7 @@
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
];

src/Http/Middleware/AuthorizeInboxMiddleware.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff 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
}

tests/Feature/AssetControllerTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Gate;
43
use Illuminate\Support\Facades\Route;
54
use Redberry\MailboxForLaravel\CaptureService;
65
use 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));

tests/Feature/AuthorizeInboxMiddlewareTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
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 () {

tests/Unit/InboxServiceProviderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,5 @@
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
});

0 commit comments

Comments
 (0)