diff --git a/app/Console/Commands/HandleExpiredMembersDolibarr.php b/app/Console/Commands/HandleExpiredMembersDolibarr.php new file mode 100644 index 0000000..b4c4bbc --- /dev/null +++ b/app/Console/Commands/HandleExpiredMembersDolibarr.php @@ -0,0 +1,91 @@ +info('Récupération des adhérents Dolibarr'); + + $members = collect($this->dolibarr->getAllMembers()); + + $expiredMembers = $members->filter(fn ($m) => $m['status'] === 'expired'); + + $this->info("{$expiredMembers->count()} adhérent(s) expiré(s)"); + + foreach ($expiredMembers as $member) { + try { + $this->processMember($member); + } catch (\Throwable $e) { + Log::error('Erreur traitement adhérent', [ + 'member_id' => $member['id'], + 'error' => $e->getMessage(), + ]); + } + } + + $this->info('Traitement terminé'); + return Command::SUCCESS; + } + + protected function processMember(array $member): void + { + $email = $member['email'] ?? null; + + $this->info("👤 {$member['id']} - {$email}"); + + // 1. Résiliation Dolibarr + $this->dolibarr->setMemberStatus($member['id'], 'resilie'); + + // 2. Désactivation mail ISPConfig + if ($email) { + $this->disableMailAccount($email); + } + + // 3. Désactivation Nextcloud + if ($email) { + $this->nextcloud->disableUserByEmail($email); + } + } + + protected function disableMailAccount(string $email): void + { + $details = $this->mailService->getMailUserDetails($email); + + if (!$details) { + $this->warn("Boîte mail inexistante : {$email}"); + return; + } + + $this->mailService->updateMailUser($email, [ + 'postfix' => 'n', + 'disablesmtp' => 'y', + 'disableimap' => 'y', + 'disablepop3' => 'y', + ]); + + $this->info("📧 Mail désactivé"); + } +} diff --git a/app/Console/Commands/SyncDolibarrMembers.php b/app/Console/Commands/SyncDolibarrMembers.php index 751828f..4d3b514 100644 --- a/app/Console/Commands/SyncDolibarrMembers.php +++ b/app/Console/Commands/SyncDolibarrMembers.php @@ -4,7 +4,7 @@ namespace App\Console\Commands; use App\Models\Member; use App\Models\Membership; -use App\Services\DolibarrService; +use App\Services\Dolibarr\DolibarrService; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Http\Client\ConnectionException; diff --git a/app/Services/DolibarrService.php b/app/Services/Dolibarr/DolibarrService.php similarity index 84% rename from app/Services/DolibarrService.php rename to app/Services/Dolibarr/DolibarrService.php index 0ebd257..e617d54 100644 --- a/app/Services/DolibarrService.php +++ b/app/Services/Dolibarr/DolibarrService.php @@ -1,6 +1,6 @@ json(); } + + public function setMemberStatus(int|string $id, string $status): bool + { + $response = $this->client()->put( + $this->baseUrl . '/members/' . $id, + ['status' => $status] + ); + + return $response->successful(); + } + } diff --git a/app/Services/ISPConfig/ISPConfigMailService.php b/app/Services/ISPConfig/ISPConfigMailService.php index 16694c1..df38aef 100644 --- a/app/Services/ISPConfig/ISPConfigMailService.php +++ b/app/Services/ISPConfig/ISPConfigMailService.php @@ -87,4 +87,20 @@ class ISPConfigMailService extends ISPConfigService 'spam_filter' => $user['move_junk'] === 'y', ]; } + + public function updateMailUser(string $email, array $changes): bool + { + $allUsers = $this->getAllMailUsers(); + $user = collect($allUsers)->firstWhere('email', $email); + + if (!$user) { + return false; + } + + return $this->call('mail_user_update', [ + 'primary_id' => $user['mailuser_id'], + 'params' => $changes, + ]); + } + } diff --git a/app/Services/Nextcloud/NextcloudService.php b/app/Services/Nextcloud/NextcloudService.php new file mode 100644 index 0000000..722a880 --- /dev/null +++ b/app/Services/Nextcloud/NextcloudService.php @@ -0,0 +1,26 @@ +getUsernameFromEmail($email); + + return Http::withBasicAuth( + config('services.nextcloud.username'), + config('services.nextcloud.password') + )->put( + config('services.nextcloud.base_url') . "/ocs/v1.php/cloud/users/{$username}/disable", + [] + )->successful(); + } + + protected function getUsernameFromEmail(string $email): string + { + return strstr($email, '@', true); + } +} diff --git a/package-lock.json b/package-lock.json index 577899f..0ac9071 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "app", + "name": "roxane", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/routes/dev-routes.php b/routes/dev-routes.php index 25f5e00..4dcc0ad 100644 --- a/routes/dev-routes.php +++ b/routes/dev-routes.php @@ -3,7 +3,7 @@ use Illuminate\Support\Facades\Route; Route::get('/call-dolibarr', function () { - $call = new App\Services\DolibarrService; + $call = new \App\Services\Dolibarr\DolibarrService; $members = $call->getAllMembers(); // find specific $userData = collect($members)->firstWhere('id', 124); // Isabelle AK