fix(NextCloud members and script - part 1)

This commit is contained in:
2026-01-19 15:31:34 +01:00
parent 08d1944491
commit b8de637b23
8 changed files with 287 additions and 29 deletions

View File

@@ -13,14 +13,15 @@ use Symfony\Component\Console\Command\Command as CommandAlias;
class HandleExpiredMembersDolibarr extends Command
{
protected $signature = 'members:cleanup-expired
{email? : Adresse email d\'un adhérent à traiter uniquement}
{--dry-run}';
{email? : Adresse email d\'un adhérent à traiter uniquement}
{--dry-run}';
public function __construct(
protected DolibarrService $dolibarr,
protected DolibarrService $dolibarr,
protected ISPConfigMailService $mailService,
protected NextcloudService $nextcloud
) {
protected NextcloudService $nextcloud
)
{
parent::__construct();
}
@@ -39,7 +40,7 @@ class HandleExpiredMembersDolibarr extends Command
);
if ($emailFilter) {
$this->info("Mode utilisateur unique : {$emailFilter}");
$this->warn("Mode utilisateur unique : {$emailFilter}");
}
$this->info('Récupération des adhérents Dolibarr');
@@ -61,8 +62,7 @@ class HandleExpiredMembersDolibarr extends Command
if ($emailFilter) {
$expiredMembers = $expiredMembers->filter(function ($member) use ($emailFilter) {
$email = $this->extractRetzienEmail($member['email'] ?? null);
return $email === $emailFilter;
return $this->extractRetzienEmail($member['email'] ?? null) === $emailFilter;
});
if ($expiredMembers->isEmpty()) {
@@ -71,7 +71,6 @@ class HandleExpiredMembersDolibarr extends Command
}
}
$this->info("{$expiredMembers->count()} adhérent(s) expiré(s)");
foreach ($expiredMembers as $member) {
@@ -90,11 +89,13 @@ class HandleExpiredMembersDolibarr extends Command
? 'DRY-RUN terminé aucune action effectuée'
: 'Traitement terminé'
);
return CommandAlias::SUCCESS;
}
/**
* @throws ConnectionException
* @throws \Exception
*/
protected function processMember(array $member, bool $dryRun): void
{
@@ -106,7 +107,9 @@ class HandleExpiredMembersDolibarr extends Command
if ($dryRun) {
$this->info("[DRY-RUN] Résiliation Dolibarr");
} else {
$this->dolibarr->setMemberStatus($member['id'], '0');
$this->dolibarr->updateMember($member['id'], [
'statut' => 0,
]);
}
// Désactivation mail
@@ -114,7 +117,7 @@ class HandleExpiredMembersDolibarr extends Command
$this->disableMailAccount($email, $dryRun);
}
// Désactivation Nextcloud
// Désactivation Nextcloud
if ($email) {
if ($dryRun) {
$this->info("[DRY-RUN] Désactivation Nextcloud");
@@ -124,6 +127,9 @@ class HandleExpiredMembersDolibarr extends Command
}
}
/**
* @throws \Exception
*/
protected function disableMailAccount(string $email, bool $dryRun): void
{
$details = $this->mailService->getMailUserDetails($email);
@@ -134,18 +140,22 @@ class HandleExpiredMembersDolibarr extends Command
}
if ($dryRun) {
$this->info("[DRY-RUN] Mail désactivé");
$this->info("[DRY-RUN] Mail désactivé ({$email})");
return;
}
$this->mailService->updateMailUser($email, [
$result = $this->mailService->updateMailUser($email, [
'postfix' => 'n',
'disablesmtp' => 'y',
'disableimap' => 'y',
'disablepop3' => 'y',
]);
$this->info("Mail désactivé");
if (!$result) {
throw new \RuntimeException("Échec désactivation mail ISPConfig pour {$email}");
}
$this->info("Mail désactivé : {$email}");
}
protected function extractRetzienEmail(?string $emails): ?string
@@ -155,8 +165,8 @@ class HandleExpiredMembersDolibarr extends Command
}
return collect(explode(';', $emails))
->map(fn (string $email): string => trim($email))
->filter(fn (string $email): bool => str_contains($email, '@retzien.fr'))
->map(fn(string $email): string => trim($email))
->filter(fn(string $email): bool => str_contains($email, '@retzien.fr'))
->first();
}
}

View File

@@ -0,0 +1,115 @@
<?php
namespace App\Console\Commands;
use App\Models\Member;
use App\Models\NextCloudMember;
use App\Services\Nextcloud\NextcloudService;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
class SyncNextcloudMembers extends Command
{
protected $signature = 'nextcloud:sync-members
{--dry-run : Ne pas écrire en base}
{--member= : Synchroniser un seul member_id}';
protected $description = 'Synchronise les comptes Nextcloud avec les adhérents';
public function __construct(
protected NextcloudService $nextcloud
) {
parent::__construct();
}
/**
* @throws ConnectionException
*/
public function handle(): int
{
$dryRun = $this->option('dry-run');
$memberFilter = $this->option('member');
$this->info(
$dryRun
? 'DRY-RUN activé'
: 'Synchronisation Nextcloud → Members'
);
/** index des membres par email */
$members = Member::query()
->where('email', 'like', '%@retzien.fr%')
->when($memberFilter, fn ($q) => $q->where('id', $memberFilter))
->get()
->filter(fn (Member $m) => !empty($m->retzien_email))
->keyBy(fn (Member $m) => strtolower($m->retzien_email));
if ($members->isEmpty()) {
$this->warn('Aucun membre à synchroniser');
return CommandAlias::SUCCESS;
}
$this->info("{$members->count()} membres candidats");
/**Récupération des users Nextcloud */
$userIds = $this->nextcloud->listUsers();
$this->info(count($userIds) . ' comptes Nextcloud trouvés');
$synced = 0;
foreach ($userIds as $userId) {
try {
$details = $this->nextcloud->getUserDetails($userId);
$email = strtolower($details['email'] ?? '');
if (!$email || !$members->has($email)) {
continue;
}
$member = $members[$email];
$payload = [
'member_id' => $member->id,
'nextcloud_user_id' => $userId,
'data' => [
'email' => $email,
'quota' => $details['quota'] ?? null,
'groups' => $details['groups'] ?? [],
'enabled' => !($details['enabled'] === false),
'last_login' => $details['lastLogin'] ?? null,
'raw' => $details, // utile pour debug
],
];
if ($dryRun) {
$this->line("[DRY-RUN] {$member->id}{$userId}");
} else {
NextCloudMember::query()->updateOrCreate(
[
'member_id' => $member->id,
'nextcloud_user_id' => $userId,
],
$payload
);
}
$synced++;
} catch (\Throwable $e) {
Log::error('Erreur sync Nextcloud', [
'user_id' => $userId,
'error' => $e->getMessage(),
]);
}
}
$this->info("Synchronisation terminée ({$synced} comptes liés)");
return CommandAlias::SUCCESS;
}
}