Page MenuHomePhorge

D5337.1775384349.diff
No OneTemporary

Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None

D5337.1775384349.diff

diff --git a/src/app/Console/Commands/Tenant/CreateCommand.php b/src/app/Console/Commands/Tenant/CreateCommand.php
--- a/src/app/Console/Commands/Tenant/CreateCommand.php
+++ b/src/app/Console/Commands/Tenant/CreateCommand.php
@@ -168,7 +168,7 @@
$user = new User();
$user->email = $email;
$user->password = $this->option('password');
- $user->role = 'reseller';
+ $user->role = User::ROLE_RESELLER;
$user->tenant_id = $tenant->id;
if ($error = UsersController::validateEmail($email, $user)) {
diff --git a/src/app/Console/Commands/User/CreateCommand.php b/src/app/Console/Commands/User/CreateCommand.php
--- a/src/app/Console/Commands/User/CreateCommand.php
+++ b/src/app/Console/Commands/User/CreateCommand.php
@@ -63,6 +63,7 @@
} else {
[$local, $domainName] = explode('@', $email, 2);
+ $role = null;
$domain = $this->getDomain($domainName);
if (!$domain) {
diff --git a/src/app/Console/Commands/User/SetRoleCommand.php b/src/app/Console/Commands/User/SetRoleCommand.php
--- a/src/app/Console/Commands/User/SetRoleCommand.php
+++ b/src/app/Console/Commands/User/SetRoleCommand.php
@@ -3,6 +3,7 @@
namespace App\Console\Commands\User;
use App\Console\Command;
+use App\User;
class SetRoleCommand extends Command
{
@@ -28,16 +29,21 @@
public function handle()
{
$user = $this->getUser($this->argument('user'));
+ $role = $this->argument('role');
if (!$user) {
$this->error("User not found.");
return 1;
}
- $role = $this->argument('role');
+
if ($role === 'null') {
$this->info("Removing role.");
$role = null;
+ } elseif (!in_array($role, User::supportedRoles())) {
+ $this->error("Invalid role.");
+ return 1;
}
+
$user->role = $role;
$user->save();
}
diff --git a/src/app/Console/Commands/User/VerifyCommand.php b/src/app/Console/Commands/User/VerifyCommand.php
deleted file mode 100644
--- a/src/app/Console/Commands/User/VerifyCommand.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-
-namespace App\Console\Commands\User;
-
-use App\Console\Command;
-use App\Jobs\User\VerifyJob;
-
-class VerifyCommand extends Command
-{
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'user:verify {user}';
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Verify the state of a user account';
-
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $user = $this->getUser($this->argument('user'));
-
- if (!$user) {
- $this->error("User not found.");
- return 1;
- }
-
- VerifyJob::dispatchSync($user->id);
-
- // TODO: We should check the job result and print an error on failure
- }
-}
diff --git a/src/app/Http/Controllers/API/V4/Admin/StatsController.php b/src/app/Http/Controllers/API/V4/Admin/StatsController.php
--- a/src/app/Http/Controllers/API/V4/Admin/StatsController.php
+++ b/src/app/Http/Controllers/API/V4/Admin/StatsController.php
@@ -523,7 +523,7 @@
$user = $this->guard()->user();
// For resellers return their wallet currency
- if ($user->role == 'reseller') {
+ if ($user->role == User::ROLE_RESELLER) {
$currency = $user->wallet()->currency;
}
diff --git a/src/app/Http/Controllers/API/V4/Admin/WalletsController.php b/src/app/Http/Controllers/API/V4/Admin/WalletsController.php
--- a/src/app/Http/Controllers/API/V4/Admin/WalletsController.php
+++ b/src/app/Http/Controllers/API/V4/Admin/WalletsController.php
@@ -5,6 +5,7 @@
use App\Discount;
use App\Http\Controllers\API\V4\PaymentsController;
use App\Providers\PaymentProvider;
+use App\User;
use App\Wallet;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -86,7 +87,7 @@
$wallet->{$method}(abs($amount), $request->description);
- if ($user->role == 'reseller') {
+ if ($user->role == User::ROLE_RESELLER) {
if ($user->tenant && ($tenant_wallet = $user->tenant->wallet())) {
$desc = ($amount > 0 ? 'Awarded' : 'Penalized') . " user {$wallet->owner->email}";
$tenant_method = $amount > 0 ? 'debit' : 'credit';
diff --git a/src/app/Http/Controllers/API/V4/Reseller/UsersController.php b/src/app/Http/Controllers/API/V4/Reseller/UsersController.php
--- a/src/app/Http/Controllers/API/V4/Reseller/UsersController.php
+++ b/src/app/Http/Controllers/API/V4/Reseller/UsersController.php
@@ -90,11 +90,8 @@
->first();
if ($domain) {
- if (
- ($wallet = $domain->wallet())
- && ($owner = $wallet->owner()->withTrashed()->withSubjectTenantContext()->first())
- && empty($owner->role)
- ) {
+ $wallet = $domain->wallet();
+ if ($wallet && ($owner = $wallet->owner()->withTrashed()->withSubjectTenantContext()->first())) {
$result->push($owner);
}
}
diff --git a/src/app/Http/Controllers/API/V4/User/DelegationTrait.php b/src/app/Http/Controllers/API/V4/User/DelegationTrait.php
--- a/src/app/Http/Controllers/API/V4/User/DelegationTrait.php
+++ b/src/app/Http/Controllers/API/V4/User/DelegationTrait.php
@@ -57,7 +57,7 @@
{
$user = User::find($id);
- if (!$this->checkTenant($user) || $user->role) {
+ if (!$this->checkTenant($user) || $user->role == User::ROLE_SERVICE) {
return $this->errorResponse(404);
}
diff --git a/src/app/Http/Controllers/Controller.php b/src/app/Http/Controllers/Controller.php
--- a/src/app/Http/Controllers/Controller.php
+++ b/src/app/Http/Controllers/Controller.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\User;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
@@ -64,7 +65,7 @@
$user = $this->guard()->user();
- if ($user->role == 'admin') {
+ if ($user->role == User::ROLE_ADMIN) {
return true;
}
diff --git a/src/app/Jobs/User/CreateJob.php b/src/app/Jobs/User/CreateJob.php
--- a/src/app/Jobs/User/CreateJob.php
+++ b/src/app/Jobs/User/CreateJob.php
@@ -39,8 +39,7 @@
return;
}
- if ($user->role) {
- // Admins/resellers/service-accounts don't reside in LDAP (for now)
+ if ($user->role == User::ROLE_SERVICE) {
return;
}
@@ -61,6 +60,10 @@
return;
}
+ if (!$user->hasSku('mailbox')) {
+ return;
+ }
+
$withLdap = \config('app.with_ldap');
// see if the domain is ready
diff --git a/src/app/Jobs/User/Delegation/CreateJob.php b/src/app/Jobs/User/Delegation/CreateJob.php
--- a/src/app/Jobs/User/Delegation/CreateJob.php
+++ b/src/app/Jobs/User/Delegation/CreateJob.php
@@ -48,7 +48,7 @@
$user = $delegation->user;
$delegatee = $delegation->delegatee;
- if (!$user || !$delegatee || $user->role || $delegatee->role || $user->trashed() || $delegatee->trashed()) {
+ if (!$user || !$delegatee || $user->trashed() || $delegatee->trashed()) {
return;
}
diff --git a/src/app/Jobs/User/DeleteJob.php b/src/app/Jobs/User/DeleteJob.php
--- a/src/app/Jobs/User/DeleteJob.php
+++ b/src/app/Jobs/User/DeleteJob.php
@@ -23,8 +23,7 @@
return;
}
- if ($user->role) {
- // Admins/resellers don't reside in LDAP (for now)
+ if ($user->role == User::ROLE_SERVICE) {
return;
}
diff --git a/src/app/Jobs/User/ResyncJob.php b/src/app/Jobs/User/ResyncJob.php
--- a/src/app/Jobs/User/ResyncJob.php
+++ b/src/app/Jobs/User/ResyncJob.php
@@ -24,7 +24,7 @@
return;
}
- if ($user->role) {
+ if ($user->role == User::ROLE_SERVICE) {
// Admins/resellers don't reside in LDAP (for now)
return;
}
diff --git a/src/app/Jobs/User/UpdateJob.php b/src/app/Jobs/User/UpdateJob.php
--- a/src/app/Jobs/User/UpdateJob.php
+++ b/src/app/Jobs/User/UpdateJob.php
@@ -5,6 +5,7 @@
use App\Jobs\UserJob;
use App\Support\Facades\IMAP;
use App\Support\Facades\LDAP;
+use App\User;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
class UpdateJob extends UserJob implements ShouldBeUniqueUntilProcessing
@@ -28,7 +29,7 @@
return;
}
- if ($user->role) {
+ if ($user->role == User::ROLE_SERVICE) {
// Admins/resellers don't reside in LDAP (for now)
return;
}
diff --git a/src/app/Jobs/User/VerifyJob.php b/src/app/Jobs/User/VerifyJob.php
deleted file mode 100644
--- a/src/app/Jobs/User/VerifyJob.php
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-namespace App\Jobs\User;
-
-use App\Jobs\UserJob;
-use App\Support\Facades\IMAP;
-use App\User;
-
-class VerifyJob extends UserJob
-{
- /**
- * Execute the job.
- */
- public function handle()
- {
- $this->logJobStart($this->userEmail);
-
- $user = $this->getUser();
-
- if (!$user) {
- return;
- }
-
- if ($user->role) {
- // Admins/resellers don't reside in IMAP (for now)
- return;
- }
-
- // sanity checks
- if (!$user->hasSku('mailbox')) {
- $this->fail("User {$this->userId} has no mailbox SKU.");
- return;
- }
-
- // the user has a mailbox (or is marked as such)
- if ($user->isImapReady()) {
- return;
- }
-
- if (IMAP::verifyAccount($user->email)) {
- $user->status |= User::STATUS_IMAP_READY;
- $user->status |= User::STATUS_ACTIVE;
- $user->save();
- }
- }
-}
diff --git a/src/app/Providers/PaymentProvider.php b/src/app/Providers/PaymentProvider.php
--- a/src/app/Providers/PaymentProvider.php
+++ b/src/app/Providers/PaymentProvider.php
@@ -322,7 +322,7 @@
$url = Utils::serviceUrl('/wallet');
$domain = preg_replace('/:[0-9]+$/', '', request()->getHttpHost());
- if (str_starts_with($domain, 'reseller')) {
+ if (str_starts_with($domain, 'reseller.')) {
$url = preg_replace('|^(https?://)([^/]+)|', '\1' . $domain, $url);
}
diff --git a/src/app/Tenant.php b/src/app/Tenant.php
--- a/src/app/Tenant.php
+++ b/src/app/Tenant.php
@@ -85,7 +85,7 @@
*/
public function wallet(): ?Wallet
{
- $user = User::where('role', 'reseller')->where('tenant_id', $this->id)->first();
+ $user = User::where('role', User::ROLE_RESELLER)->where('tenant_id', $this->id)->first();
return $user ? $user->wallets->first() : null;
}
diff --git a/src/app/User.php b/src/app/User.php
--- a/src/app/User.php
+++ b/src/app/User.php
@@ -742,6 +742,20 @@
$this->attributes['role'] = $role;
}
+ /**
+ * Returns list of supported user roles
+ */
+ public static function supportedRoles(): array
+ {
+ $class = new \ReflectionClass(__CLASS__);
+
+ return array_values(array_filter(
+ $class->getConstants(),
+ fn ($key) => str_starts_with($key, 'ROLE_'),
+ \ARRAY_FILTER_USE_KEY
+ ));
+ }
+
/**
* Suspend all users/domains/groups in this account.
*/
diff --git a/src/tests/Feature/Console/User/SetRoleTest.php b/src/tests/Feature/Console/User/SetRoleTest.php
new file mode 100644
--- /dev/null
+++ b/src/tests/Feature/Console/User/SetRoleTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Tests\Feature\Console\User;
+
+use App\User;
+use Tests\TestCase;
+
+class SetRoleTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+ }
+
+ protected function tearDown(): void
+ {
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+
+ parent::tearDown();
+ }
+
+ /**
+ * Test command runs
+ */
+ public function testHandle(): void
+ {
+ $user = $this->getTestUser('wallets-controller@kolabnow.com');
+
+ $this->assertNull($user->role);
+
+ // Invalid user id
+ $code = \Artisan::call("user:set-role 123 admin");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame('User not found.', $output);
+
+ // Invalid role
+ $code = \Artisan::call("user:set-role {$user->id} 123");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame('Invalid role.', $output);
+
+ // Assign a role
+ $code = \Artisan::call("user:set-role {$user->id} " . User::ROLE_ADMIN);
+ $output = trim(\Artisan::output());
+ $this->assertSame(0, $code);
+ $this->assertSame('', $output);
+ $this->assertSame(User::ROLE_ADMIN, $user->fresh()->role);
+
+ // Remove a role
+ $code = \Artisan::call("user:set-role {$user->id} null");
+ $output = trim(\Artisan::output());
+ $this->assertSame(0, $code);
+ $this->assertSame('Removing role.', $output);
+ $this->assertNull($user->fresh()->role);
+ }
+}
diff --git a/src/tests/Feature/Console/User/VerifyTest.php b/src/tests/Feature/Console/User/VerifyTest.php
deleted file mode 100644
--- a/src/tests/Feature/Console/User/VerifyTest.php
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-
-namespace Tests\Feature\Console\User;
-
-use Illuminate\Support\Facades\Queue;
-use Tests\TestCase;
-
-class VerifyTest extends TestCase
-{
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->deleteTestUser('user@force-delete.com');
- }
-
- protected function tearDown(): void
- {
- $this->deleteTestUser('user@force-delete.com');
-
- parent::tearDown();
- }
-
- /**
- * Test the command
- */
- public function testHandle(): void
- {
- Queue::fake();
-
- // Non-existing user
- $code = \Artisan::call("user:verify unknown");
- $output = trim(\Artisan::output());
-
- $this->assertSame(1, $code);
- $this->assertSame("User not found.", $output);
-
- $user = $this->getTestUser('user@force-delete.com');
-
- // Test success
- $code = \Artisan::call("user:verify {$user->email}");
- $output = trim(\Artisan::output());
-
- $this->assertSame(0, $code);
- $this->assertSame("", $output);
-
- // TODO: Test the verification utility error conditions
- }
-}
diff --git a/src/tests/Feature/Jobs/User/CreateTest.php b/src/tests/Feature/Jobs/User/CreateTest.php
--- a/src/tests/Feature/Jobs/User/CreateTest.php
+++ b/src/tests/Feature/Jobs/User/CreateTest.php
@@ -4,6 +4,7 @@
use App\Domain;
use App\Jobs\User\CreateJob;
+use App\Sku;
use App\Support\Facades\DAV;
use App\Support\Facades\IMAP;
use App\Support\Facades\LDAP;
@@ -35,6 +36,7 @@
Queue::fake();
$user = $this->getTestUser('new-job-user@' . \config('app.domain'), ['status' => User::STATUS_NEW]);
+ $user->assignSku(Sku::withEnvTenantContext()->where('title', 'mailbox')->first());
$domain = Domain::where('namespace', \config('app.domain'))->first();
$domain->status |= Domain::STATUS_LDAP_READY;
$domain->save();
diff --git a/src/tests/Feature/Jobs/User/VerifyTest.php b/src/tests/Feature/Jobs/User/VerifyTest.php
deleted file mode 100644
--- a/src/tests/Feature/Jobs/User/VerifyTest.php
+++ /dev/null
@@ -1,72 +0,0 @@
-<?php
-
-namespace Tests\Feature\Jobs\User;
-
-use App\Jobs\User\VerifyJob;
-use App\Support\Facades\IMAP;
-use App\User;
-use Illuminate\Support\Facades\Queue;
-use Tests\TestCase;
-
-class VerifyTest extends TestCase
-{
- protected function setUp(): void
- {
- parent::setUp();
-
- $ned = $this->getTestUser('ned@kolab.org');
- $ned->status |= User::STATUS_IMAP_READY;
- $ned->save();
- }
-
- protected function tearDown(): void
- {
- $ned = $this->getTestUser('ned@kolab.org');
- $ned->status |= User::STATUS_IMAP_READY;
- $ned->save();
-
- parent::tearDown();
- }
-
- /**
- * Test job handle
- */
- public function testHandle(): void
- {
- Queue::fake();
-
- // Test non-existing user ID
- $job = (new VerifyJob(123))->withFakeQueueInteractions();
- $job->handle();
- $job->assertFailedWith("User 123 could not be found in the database.");
-
- // Test existing user (verification successful)
- $user = $this->getTestUser('ned@kolab.org');
-
- if ($user->isImapReady()) {
- $user->status ^= User::STATUS_IMAP_READY;
- $user->save();
- }
-
- $this->assertFalse($user->isImapReady());
-
- IMAP::shouldReceive('verifyAccount')->once()->with($user->email)->andReturn(true);
-
- $job = new VerifyJob($user->id);
- $job->handle();
-
- $user->refresh();
- $this->assertTrue($user->isImapReady());
-
- // Test existing user (verification not-successful)
- $user->status ^= User::STATUS_IMAP_READY;
- $user->save();
-
- IMAP::shouldReceive('verifyAccount')->once()->with($user->email)->andReturn(false);
-
- $job = new VerifyJob($user->id);
- $job->handle();
-
- $this->assertFalse($user->fresh()->isImapReady());
- }
-}

File Metadata

Mime Type
text/plain
Expires
Sun, Apr 5, 10:19 AM (12 h, 15 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18833173
Default Alt Text
D5337.1775384349.diff (17 KB)

Event Timeline