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

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

View File

@@ -109,6 +109,12 @@ class Member extends Model
return "{$this->firstname} {$this->lastname}"; return "{$this->firstname} {$this->lastname}";
} }
public function getRetzienEmailAttribute(): string
{
$emails = explode(';', $this->email);
return collect($emails)->filter(fn($email) => str_contains($email, '@retzien.fr'))->first();
}
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
@@ -142,4 +148,9 @@ class Member extends Model
->where('type', IspconfigType::WEB) ->where('type', IspconfigType::WEB)
->first(); ->first();
} }
public function nextcloudAccounts(): HasMany
{
return $this->hasMany(NextCloudMember::class, 'member_id');
}
} }

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Models;
use App\Enums\IspconfigType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class NextCloudMember extends Model
{
protected $table = 'nextclouds_members';
protected $fillable = [
'member_id',
'nextcloud_user_id',
'data',
];
protected $casts = [
'data' => 'array',
];
public function member(): BelongsTo
{
return $this->belongsTo(Member::class);
}
}

View File

@@ -61,14 +61,22 @@ class DolibarrService
return $response->json(); return $response->json();
} }
public function setMemberStatus(int|string $id, int|string $status): bool /**
* Update a member with custom data
*
* @param int|string $id The Dolibarr member ID (rowid)
* @param array $data Array of attributes to update (e.g. ['email' => 'new@email.com', 'array_options' => ['options_custom' => 'val']])
* @throws ConnectionException
*/
public function updateMember(int|string $id, array $data): bool
{ {
$response = $this->client()->put( $response = $this->client()->put(
$this->baseUrl . '/members/' . $id, $this->baseUrl . '/members/' . $id,
['status' => $status] $data
); );
return $response->successful(); return $response->successful();
} }
} }

View File

@@ -88,19 +88,44 @@ class ISPConfigMailService extends ISPConfigService
]; ];
} }
/**
* @throws \Exception
*/
public function updateMailUser(string $email, array $changes): bool public function updateMailUser(string $email, array $changes): bool
{ {
$allUsers = $this->getAllMailUsers(); // On retrouve l'utilisateur
$user = collect($allUsers)->firstWhere('email', $email); $user = collect($this->getAllMailUsers())
->firstWhere('email', $email);
if (!$user) { if (!$user) {
return false; return false;
} }
return $this->call('mail_user_update', [ $mailuserId = (int) $user['mailuser_id'];
'primary_id' => $user['mailuser_id'],
'params' => $changes, // On récupère l'enregistrement COMPLET (OBLIGATOIRE)
$mailUserRecord = $this->call('mail_user_get', [
$mailuserId
]); ]);
if (!is_array($mailUserRecord)) {
throw new \RuntimeException('mail_user_get did not return array');
} }
// On applique les changements
foreach ($changes as $key => $value) {
$mailUserRecord[$key] = $value;
}
// appel conforme EXACT à lexemple ISPConfig
$result = $this->call('mail_user_update', [
0, // client_id (ADMIN)
$mailuserId, // primary_id
$mailUserRecord // FULL RECORD
]);
return (bool) $result;
}
} }

View File

@@ -4,6 +4,7 @@ namespace App\Services\Nextcloud;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -46,15 +47,24 @@ class NextcloudService
/** /**
* Trouve le userId Nextcloud à partir de lemail * Trouve le userId Nextcloud à partir de lemail
* @throws ConnectionException
*/ */
protected function findUserIdByEmail(string $email): ?string protected function findUserIdByEmail(string $email): ?string
{ {
$response = $this->http->get('/cloud/users'); return Cache::remember(
'nextcloud.user_id.' . md5($email),
if (!$response->successful()) { now()->addDays(7),
throw new \RuntimeException('Erreur récupération utilisateurs Nextcloud'); function () use ($email) {
return $this->resolveUserIdByEmail($email);
} }
);
}
/**
* @throws ConnectionException
*/
protected function resolveUserIdByEmail(string $email): ?string
{
$response = $this->http->get('/cloud/users');
$users = $response->json('ocs.data.users') ?? []; $users = $response->json('ocs.data.users') ?? [];
@@ -63,7 +73,7 @@ class NextcloudService
if ( if (
$details->successful() && $details->successful() &&
($details->json('ocs.data.email') === $email) $details->json('ocs.data.email') === $email
) { ) {
return $userId; return $userId;
} }
@@ -71,4 +81,26 @@ class NextcloudService
return null; return null;
} }
/**
* @throws ConnectionException
*/
public function listUsers(): array
{
return $this->http
->get('/cloud/users')
->json('ocs.data.users') ?? [];
}
/**
* @throws ConnectionException
*/
public function getUserDetails(string $userId): array
{
return $this->http
->get("/cloud/users/{$userId}")
->json('ocs.data') ?? [];
}
} }

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('nextclouds_members', function (Blueprint $table) {
$table->id();
$table->foreignId('member_id')->constrained('members')->onDelete('NO ACTION');
$table->string('nextcloud_user_id')->nullable();
$table->json('data')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('nextclouds_members');
}
};