fix(NextCloud members and script - part 1)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
115
app/Console/Commands/SyncNextcloudMembers.php
Normal file
115
app/Console/Commands/SyncNextcloudMembers.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,12 @@ class Member extends Model
|
||||
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
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
@@ -142,4 +148,9 @@ class Member extends Model
|
||||
->where('type', IspconfigType::WEB)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function nextcloudAccounts(): HasMany
|
||||
{
|
||||
return $this->hasMany(NextCloudMember::class, 'member_id');
|
||||
}
|
||||
}
|
||||
|
||||
27
app/Models/NextCloudMember.php
Normal file
27
app/Models/NextCloudMember.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -61,14 +61,22 @@ class DolibarrService
|
||||
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(
|
||||
$this->baseUrl . '/members/' . $id,
|
||||
['status' => $status]
|
||||
$data
|
||||
);
|
||||
|
||||
return $response->successful();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -88,19 +88,44 @@ class ISPConfigMailService extends ISPConfigService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function updateMailUser(string $email, array $changes): bool
|
||||
{
|
||||
$allUsers = $this->getAllMailUsers();
|
||||
$user = collect($allUsers)->firstWhere('email', $email);
|
||||
// On retrouve l'utilisateur
|
||||
$user = collect($this->getAllMailUsers())
|
||||
->firstWhere('email', $email);
|
||||
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->call('mail_user_update', [
|
||||
'primary_id' => $user['mailuser_id'],
|
||||
'params' => $changes,
|
||||
$mailuserId = (int) $user['mailuser_id'];
|
||||
|
||||
// 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 à l’exemple ISPConfig
|
||||
$result = $this->call('mail_user_update', [
|
||||
0, // client_id (ADMIN)
|
||||
$mailuserId, // primary_id
|
||||
$mailUserRecord // FULL RECORD
|
||||
]);
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services\Nextcloud;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -46,15 +47,24 @@ class NextcloudService
|
||||
|
||||
/**
|
||||
* Trouve le userId Nextcloud à partir de l’email
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
protected function findUserIdByEmail(string $email): ?string
|
||||
{
|
||||
$response = $this->http->get('/cloud/users');
|
||||
return Cache::remember(
|
||||
'nextcloud.user_id.' . md5($email),
|
||||
now()->addDays(7),
|
||||
function () use ($email) {
|
||||
return $this->resolveUserIdByEmail($email);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (!$response->successful()) {
|
||||
throw new \RuntimeException('Erreur récupération utilisateurs Nextcloud');
|
||||
}
|
||||
/**
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
protected function resolveUserIdByEmail(string $email): ?string
|
||||
{
|
||||
$response = $this->http->get('/cloud/users');
|
||||
|
||||
$users = $response->json('ocs.data.users') ?? [];
|
||||
|
||||
@@ -63,7 +73,7 @@ class NextcloudService
|
||||
|
||||
if (
|
||||
$details->successful() &&
|
||||
($details->json('ocs.data.email') === $email)
|
||||
$details->json('ocs.data.email') === $email
|
||||
) {
|
||||
return $userId;
|
||||
}
|
||||
@@ -71,4 +81,26 @@ class NextcloudService
|
||||
|
||||
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') ?? [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user