wip(Resiliation script)
This commit is contained in:
91
app/Console/Commands/HandleExpiredMembersDolibarr.php
Normal file
91
app/Console/Commands/HandleExpiredMembersDolibarr.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\Dolibarr\DolibarrService;
|
||||
use App\Services\ISPConfig\ISPConfigMailService;
|
||||
use App\Services\Nextcloud\NextcloudService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class HandleExpiredMembersDolibarr extends Command
|
||||
{
|
||||
protected $signature = 'members:cleanup-expired';
|
||||
protected $description = 'Résilie les adhérents expirés et désactive leurs services';
|
||||
|
||||
public function __construct(
|
||||
protected DolibarrService $dolibarr,
|
||||
protected ISPConfigMailService $mailService,
|
||||
protected NextcloudService $nextcloud
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$this->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é");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\Dolibarr;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
@@ -58,4 +58,15 @@ class DolibarrService
|
||||
|
||||
return $response->json();
|
||||
}
|
||||
|
||||
public function setMemberStatus(int|string $id, string $status): bool
|
||||
{
|
||||
$response = $this->client()->put(
|
||||
$this->baseUrl . '/members/' . $id,
|
||||
['status' => $status]
|
||||
);
|
||||
|
||||
return $response->successful();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
26
app/Services/Nextcloud/NextcloudService.php
Normal file
26
app/Services/Nextcloud/NextcloudService.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Nextcloud;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class NextcloudService
|
||||
{
|
||||
public function disableUserByEmail(string $email): bool
|
||||
{
|
||||
$username = $this->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);
|
||||
}
|
||||
}
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "app",
|
||||
"name": "roxane",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user