Skip to content

Commit e6ff903

Browse files
fix: user getting logged out when doing backups or restores (#70)
1 parent ecc7a3d commit e6ff903

File tree

8 files changed

+393
-383
lines changed

8 files changed

+393
-383
lines changed

client/src/components/Backup.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export default {
2626
},
2727
},
2828
created() {
29-
console.log(this.chunkSize)
29+
// console.log(this.chunkSize)
30+
3031
window.backup = {
3132
chunkSize: this.chunkSize
3233
};

composer.lock

Lines changed: 363 additions & 356 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Backuper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Itiden\Backup;
66

7+
use Illuminate\Contracts\Auth\Authenticatable;
78
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Facades\Pipeline;
910
use Itiden\Backup\Contracts\Repositories\BackupRepository;
@@ -29,7 +30,7 @@ public function __construct(
2930
*
3031
* @throws Exceptions\BackupFailed
3132
*/
32-
public function backup(): BackupDto
33+
public function backup(?Authenticatable $user = null): BackupDto
3334
{
3435
$lock = $this->stateManager->getLock();
3536

@@ -61,8 +62,6 @@ public function backup(): BackupDto
6162

6263
$metadata = static::addMetaFromZipToBackupMeta($temp_zip_path, $backup);
6364

64-
$user = auth()->user();
65-
6665
if ($user) {
6766
$metadata->setCreatedBy($user);
6867
}

src/Http/Controllers/Api/RestoreController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
namespace Itiden\Backup\Http\Controllers\Api;
66

77
use Illuminate\Container\Attributes\Authenticated;
8-
use Illuminate\Contracts\Cache\Repository;
8+
use Illuminate\Contracts\Auth\Authenticatable;
99
use Illuminate\Http\JsonResponse;
10-
use Itiden\Backup\Exceptions\ActionAlreadyInProgress;
1110
use Itiden\Backup\Jobs\RestoreJob;
1211
use Itiden\Backup\StateManager;
13-
use Statamic\Contracts\Auth\User;
1412

1513
final readonly class RestoreController
1614
{
17-
public function __invoke(string $id, StateManager $stateManager, #[Authenticated] User $user): JsonResponse
18-
{
19-
$stateManager->dispatch(new RestoreJob($id, $user));
15+
public function __invoke(
16+
string $id,
17+
StateManager $stateManager,
18+
#[Authenticated] Authenticatable $user,
19+
): JsonResponse {
20+
$stateManager->dispatch(new RestoreJob(
21+
id: $id,
22+
user: $user,
23+
));
2024

2125
return response()->json(['message' => __('statamic-backup::backup.restore.started')]);
2226
}

src/Http/Controllers/Api/StoreBackupController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
namespace Itiden\Backup\Http\Controllers\Api;
66

77
use Illuminate\Container\Attributes\Authenticated;
8+
use Illuminate\Contracts\Auth\Authenticatable;
89
use Illuminate\Http\JsonResponse;
910
use Itiden\Backup\Jobs\BackupJob;
1011
use Itiden\Backup\StateManager;
11-
use Statamic\Contracts\Auth\User;
1212

1313
final readonly class StoreBackupController
1414
{
15-
public function __invoke(StateManager $stateManager, #[Authenticated] User $user): JsonResponse
15+
public function __invoke(StateManager $stateManager, #[Authenticated] Authenticatable $user): JsonResponse
1616
{
17-
$stateManager->dispatch(new BackupJob($user));
17+
$stateManager->dispatch(new BackupJob(user: $user));
1818

1919
return response()->json(['message' => __('statamic-backup::backup.backup_started')]);
2020
}

src/Jobs/BackupJob.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Itiden\Backup\Jobs;
66

7+
use Illuminate\Contracts\Auth\Authenticatable;
78
use Illuminate\Contracts\Cache\Repository;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Queue\Queueable;
1011
use Itiden\Backup\Backuper;
1112
use Itiden\Backup\StateManager;
12-
use Statamic\Contracts\Auth\User;
1313

1414
final class BackupJob implements ShouldQueue
1515
{
@@ -19,17 +19,15 @@ final class BackupJob implements ShouldQueue
1919
* Create a new job instance.
2020
*/
2121
public function __construct(
22-
private User $user,
22+
private Authenticatable $user,
2323
) {}
2424

2525
/**
2626
* Execute the job.
2727
*/
2828
public function handle(Backuper $backuper, Repository $cache): void
2929
{
30-
auth()->login($this->user); // ugly but it works;
31-
32-
$backuper->backup();
30+
$backuper->backup(user: $this->user);
3331

3432
$cache->forget(StateManager::JOB_QUEUED_KEY);
3533
}

src/Jobs/RestoreJob.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Itiden\Backup\Jobs;
66

7+
use Illuminate\Contracts\Auth\Authenticatable;
78
use Illuminate\Contracts\Cache\Repository;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Queue\Queueable;
1011
use Itiden\Backup\Restorer;
1112
use Itiden\Backup\StateManager;
12-
use Statamic\Contracts\Auth\User;
1313

1414
final class RestoreJob implements ShouldQueue
1515
{
@@ -20,17 +20,18 @@ final class RestoreJob implements ShouldQueue
2020
*/
2121
public function __construct(
2222
private string $id,
23-
private User $user,
23+
private Authenticatable $user,
2424
) {}
2525

2626
/**
2727
* Execute the job.
2828
*/
2929
public function handle(Restorer $backuper, Repository $cache): void
3030
{
31-
auth()->login($this->user); // ugly but it works;
32-
33-
$backuper->restoreFromId($this->id);
31+
$backuper->restoreFromId(
32+
id: $this->id,
33+
user: $this->user,
34+
);
3435

3536
$cache->forget(StateManager::JOB_QUEUED_KEY);
3637
}

src/Restorer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Itiden\Backup;
66

77
use Exception;
8+
use Illuminate\Contracts\Auth\Authenticatable;
89
use Illuminate\Support\Facades\Artisan;
910
use Illuminate\Support\Facades\File;
1011
use Illuminate\Support\Facades\Pipeline;
@@ -30,23 +31,23 @@ public function __construct(
3031
*
3132
* @throws Exception
3233
*/
33-
public function restoreFromId(string $id): void
34+
public function restoreFromId(string $id, ?Authenticatable $user = null): void
3435
{
3536
$backup = $this->repository->find($id);
3637

3738
if (!$backup) {
3839
throw new RuntimeException("Backup with id {$id} not found.");
3940
}
4041

41-
$this->restore($backup);
42+
$this->restore($backup, $user);
4243
}
4344

4445
/**
4546
* Restore to the given backup.
4647
*
4748
* @throws RestoreFailed
4849
*/
49-
public function restore(BackupDto $backup): void
50+
public function restore(BackupDto $backup, ?Authenticatable $user = null): void
5051
{
5152
$lock = $this->stateManager->getLock();
5253

@@ -70,7 +71,6 @@ public function restore(BackupDto $backup): void
7071

7172
event(new BackupRestored($backup));
7273

73-
$user = auth()->user();
7474
if ($user) {
7575
$backup->getMetadata()->addRestore($user);
7676
}

0 commit comments

Comments
 (0)