Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117955891
D3974.1775495559.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D3974.1775495559.diff
View Options
diff --git a/src/app/Console/Commands/User/ResyncCommand.php b/src/app/Console/Commands/User/ResyncCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/User/ResyncCommand.php
@@ -0,0 +1,82 @@
+<?php
+
+namespace App\Console\Commands\User;
+
+use App\Console\Command;
+use App\User;
+
+class ResyncCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'user:resync {user?} {--deleted-only} {--dry-run}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = "Re-Synchronize users with the imap/ldap backend(s)";
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = $this->argument('user');
+ $deleted_only = $this->option('deleted-only');
+ $dry_run = $this->option('dry-run');
+
+ if (!empty($user)) {
+ if ($user = $this->getUser($user, true)) {
+ $users = [$user];
+ } else {
+ $this->error("User not found.");
+ return 1;
+ }
+ } else {
+ $users = User::withTrashed();
+
+ if ($deleted_only) {
+ $users->whereNotNull('deleted_at')
+ ->where(function($query) {
+ $query->where('status', '&', User::STATUS_IMAP_READY)->orWhere('status', '&', User::STATUS_LDAP_READY);
+ });
+ }
+
+ $users = $users->orderBy('id')->cursor();
+ }
+
+ foreach ($users as $user) {
+ if ($user->trashed()) {
+ if ($user->isLdapReady() || $user->isImapReady()) {
+ if ($dry_run) {
+ $this->info("{$user->email}: will be pushed");
+ continue;
+ }
+
+ if ($user->isDeleted()) {
+ // Remove the DELETED flag so the DeleteJob can do the work
+ $user->timestamps = false;
+ $user->update(['status' => $user->status ^ User::STATUS_DELETED]);
+ }
+
+ // TODO: Maybe add an option to do this not asyncronously?
+ \App\Jobs\User\DeleteJob::dispatch($user->id);
+
+ $this->info("{$user->email}: pushed");
+ }
+ } else {
+ // TODO: resync the non-deleted users too.
+ // So whenever we suspect a user to be not in sync we just call
+ // user:resync instead of job:usercreate/job:userupdate commands
+ // Maybe we should also have domain:resync, resource:resync and so on.
+ }
+ }
+ }
+}
diff --git a/src/tests/Feature/Console/User/ResyncTest.php b/src/tests/Feature/Console/User/ResyncTest.php
new file mode 100644
--- /dev/null
+++ b/src/tests/Feature/Console/User/ResyncTest.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Tests\Feature\Console\User;
+
+use App\User;
+use Illuminate\Support\Facades\Queue;
+use Tests\TestCase;
+
+class ResyncTest extends TestCase
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+
+ $this->deleteTestUser('user@force-delete.com');
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function tearDown(): void
+ {
+ $this->deleteTestUser('user@force-delete.com');
+
+ parent::tearDown();
+ }
+
+ /**
+ * Test the command
+ */
+ public function testHandle(): void
+ {
+ // Non-existing user
+ $code = \Artisan::call("user:resync unknown");
+ $output = trim(\Artisan::output());
+
+ $this->assertSame(1, $code);
+ $this->assertSame("User not found.", $output);
+
+ $user = $this->getTestUser('user@force-delete.com');
+ User::where('id', $user->id)->update([
+ 'deleted_at' => now(),
+ 'status' => User::STATUS_DELETED | User::STATUS_IMAP_READY,
+ ]);
+
+ Queue::fake();
+
+ // Test success (--dry-run)
+ $code = \Artisan::call("user:resync {$user->email} --dry-run");
+ $output = trim(\Artisan::output());
+
+ $this->assertSame(0, $code);
+ $this->assertSame("{$user->email}: will be pushed", $output);
+ $this->assertTrue($user->fresh()->isDeleted());
+
+ Queue::assertNothingPushed();
+
+ // Test success
+ $code = \Artisan::call("user:resync {$user->email}");
+ $output = trim(\Artisan::output());
+
+ $this->assertSame(0, $code);
+ $this->assertSame("{$user->email}: pushed", $output);
+ $user->refresh();
+ $this->assertFalse($user->isDeleted());
+ $this->assertTrue($user->isImapReady());
+
+ Queue::assertPushed(\App\Jobs\User\DeleteJob::class, 1);
+ Queue::assertPushed(\App\Jobs\User\DeleteJob::class, function ($job) use ($user) {
+ $job_user_id = TestCase::getObjectProperty($job, 'userId');
+ return $job_user_id === $user->id;
+ });
+
+ Queue::fake();
+ User::withTrashed()->where('id', $user->id)->update(['status' => User::STATUS_DELETED]);
+
+ // Test nothing to be done (deleted user)
+ $code = \Artisan::call("user:resync {$user->email}");
+ $output = trim(\Artisan::output());
+
+ $this->assertSame(0, $code);
+ $this->assertSame("", $output);
+ Queue::assertNothingPushed();
+
+ Queue::fake();
+ User::withTrashed()->where('id', $user->id)->update([
+ 'status' => User::STATUS_DELETED | User::STATUS_IMAP_READY
+ ]);
+
+ // Remove all deleted users except one, to not interfere
+ User::withTrashed()->whereNotIn('id', [$user->id])->forceDelete();
+
+ // Test run for all deleted users
+ $code = \Artisan::call("user:resync --deleted-only");
+ $output = trim(\Artisan::output());
+
+ $this->assertSame(0, $code);
+ $this->assertSame("{$user->email}: pushed", $output);
+
+ // TODO: Test other cases (non-deleted users)
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Apr 6, 5:12 PM (17 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18838479
Default Alt Text
D3974.1775495559.diff (6 KB)
Attached To
Mode
D3974: User:resync command
Attached
Detach File
Event Timeline