Compare commits

...

5 Commits

17 changed files with 631 additions and 10 deletions

View File

@@ -86,8 +86,9 @@ ISPCONFIG_TEST_SOAP_URI=
ISPCONFIG_TEST_USERNAME=
ISPCONFIG_TEST_PASSWORD=
#NEXTCLOUD_API_ID=
#NEXCLOUD_API_PWD=
NEXTCLOUD_URL=
NEXTCLOUD_USERNAME=
NEXTCLOUD_PASSWORD=
#SYMPA_API_ID=
#SYMPA_API_PWD=

View File

@@ -0,0 +1,172 @@
<?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;
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}';
public function __construct(
protected DolibarrService $dolibarr,
protected ISPConfigMailService $mailService,
protected NextcloudService $nextcloud
)
{
parent::__construct();
}
/**
* @throws ConnectionException
*/
public function handle(): int
{
$dryRun = $this->option('dry-run');
$emailFilter = $this->argument('email');
$this->info(
$dryRun
? 'DRY-RUN activé aucune modification ne sera effectuée'
: 'Mode réel activé'
);
if ($emailFilter) {
$this->warn("Mode utilisateur unique : {$emailFilter}");
}
$this->info('Récupération des adhérents Dolibarr');
$members = collect($this->dolibarr->getAllMembers());
$today = now()->startOfDay();
$expiredMembers = $members->filter(function ($member) use ($today) {
if (($member['statut'] ?? null) != 1) {
return false;
}
if (empty($member['datefin'])) {
return false;
}
return \Carbon\Carbon::parse($member['datefin'])->lt($today);
});
if ($emailFilter) {
$expiredMembers = $expiredMembers->filter(function ($member) use ($emailFilter) {
return $this->extractRetzienEmail($member['email'] ?? null) === $emailFilter;
});
if ($expiredMembers->isEmpty()) {
$this->warn("Aucun adhérent expiré trouvé pour {$emailFilter}");
return CommandAlias::SUCCESS;
}
}
$this->info("{$expiredMembers->count()} adhérent(s) expiré(s)");
foreach ($expiredMembers as $member) {
try {
$this->processMember($member, $dryRun);
} catch (\Throwable $e) {
Log::error('Erreur traitement adhérent', [
'member_id' => $member['id'],
'error' => $e->getMessage(),
]);
}
}
$this->info(
$dryRun
? 'DRY-RUN terminé aucune action effectuée'
: 'Traitement terminé'
);
return CommandAlias::SUCCESS;
}
/**
* @throws ConnectionException
* @throws \Exception
*/
protected function processMember(array $member, bool $dryRun): void
{
$email = $this->extractRetzienEmail($member['email'] ?? null);
$this->line("{$member['id']} - {$email}");
// Résiliation Dolibarr
if ($dryRun) {
$this->info("[DRY-RUN] Résiliation Dolibarr");
} else {
$this->dolibarr->updateMember($member['id'], [
'statut' => 0,
]);
}
// Désactivation mail
if ($email) {
$this->disableMailAccount($email, $dryRun);
}
// Désactivation Nextcloud
if ($email) {
if ($dryRun) {
$this->info("[DRY-RUN] Désactivation Nextcloud");
} else {
$this->nextcloud->disableUserByEmail($email);
}
}
}
/**
* @throws \Exception
*/
protected function disableMailAccount(string $email, bool $dryRun): void
{
$details = $this->mailService->getMailUserDetails($email);
if (!$details) {
$this->warn("Boîte mail inexistante : {$email}");
return;
}
if ($dryRun) {
$this->info("[DRY-RUN] Mail désactivé ({$email})");
return;
}
$result = $this->mailService->updateMailUser($email, [
'postfix' => 'n',
'disablesmtp' => 'y',
'disableimap' => 'y',
'disablepop3' => 'y',
]);
if (!$result) {
throw new \RuntimeException("Échec désactivation mail ISPConfig pour {$email}");
}
$this->info("Mail désactivé : {$email}");
}
protected function extractRetzienEmail(?string $emails): ?string
{
if (!$emails) {
return null;
}
return collect(explode(';', $emails))
->map(fn(string $email): string => trim($email))
->filter(fn(string $email): bool => str_contains($email, '@retzien.fr'))
->first();
}
}

View File

@@ -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;

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

@@ -2,9 +2,11 @@
namespace App\Filament\Resources\Members\Schemas;
use App\Enums\IspconfigType;
use App\Models\Member;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
@@ -12,6 +14,8 @@ use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Actions;
class MemberForm
@@ -91,7 +95,7 @@ class MemberForm
->label(Member::getAttributeLabel('country')),
])
->columns(2),
// Mail Retzien
Section::make('Messagerie ISPConfig Retzien')
->schema([
TextEntry::make('isp_mail_email')
@@ -126,6 +130,23 @@ class MemberForm
->visible(fn (?Member $record) =>
$record?->ispconfigMail() !== null
),
// Hébergement
Section::make('Hébergements Web')
->schema([
Placeholder::make('ispconfigs_web_display')
->label('')
->content(fn (?Member $record) => view('filament.components.ispconfig-web-list', [
'ispconfigs' => $record?->ispconfigs()
->where('type', IspconfigType::WEB)
->get() ?? collect()
]))
])
->visible(fn (?Member $record) =>
$record?->ispconfigs()->where('type', IspconfigType::WEB)->exists()
)
// Fin Hébergement
])
->columnSpan(3),
Grid::make(1)

View File

@@ -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);
@@ -136,10 +142,15 @@ class Member extends Model
->first();
}
public function ispconfigWeb(): ?IspconfigMember
public function ispconfigsWeb(): ?IspconfigMember
{
return $this->ispconfigs()
->where('type', IspconfigType::WEB)
->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

@@ -1,6 +1,6 @@
<?php
namespace App\Services;
namespace App\Services\Dolibarr;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
@@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Http;
class DolibarrService
{
protected string $baseUrl;
protected string $htaccessUrl;
protected string $username;
protected string $password;
protected string $apiKey;
@@ -16,6 +17,7 @@ class DolibarrService
public function __construct()
{
$this->baseUrl = config('services.dolibarr.base_url');
$this->htaccessUrl = config('services.dolibarr.htaccess_url');
$this->username = config('services.dolibarr.username');
$this->password = config('services.dolibarr.password');
$this->apiKey = config('services.dolibarr.api_key');
@@ -58,4 +60,23 @@ class DolibarrService
return $response->json();
}
/**
* 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,
$data
);
return $response->successful();
}
}

View File

@@ -87,4 +87,45 @@ class ISPConfigMailService extends ISPConfigService
'spam_filter' => $user['move_junk'] === 'y',
];
}
/**
* @throws \Exception
*/
public function updateMailUser(string $email, array $changes): bool
{
// On retrouve l'utilisateur
$user = collect($this->getAllMailUsers())
->firstWhere('email', $email);
if (!$user) {
return false;
}
$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 à lexemple ISPConfig
$result = $this->call('mail_user_update', [
0, // client_id (ADMIN)
$mailuserId, // primary_id
$mailUserRecord // FULL RECORD
]);
return (bool) $result;
}
}

View File

@@ -0,0 +1,106 @@
<?php
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;
class NextcloudService
{
protected PendingRequest $http;
public function __construct()
{
$this->http = Http::withBasicAuth(
config('services.nextcloud.user'),
config('services.nextcloud.password')
)
->withHeaders([
'OCS-APIRequest' => 'true',
'Accept' => 'application/json',
])
->baseUrl(config('services.nextcloud.url') . '/ocs/v1.php');
}
/**
* Désactive un utilisateur Nextcloud à partir de son email
* @throws ConnectionException
*/
public function disableUserByEmail(string $email): void
{
$userId = $this->findUserIdByEmail($email);
if (!$userId) {
Log::warning("Utilisateur Nextcloud introuvable", ['email' => $email]);
return;
}
$response = $this->http->put("/cloud/users/{$userId}/disable");
if (!$response->successful()) {
throw new \RuntimeException("Erreur désactivation Nextcloud {$userId}");
}
}
/**
* Trouve le userId Nextcloud à partir de lemail
*/
protected function findUserIdByEmail(string $email): ?string
{
return Cache::remember(
'nextcloud.user_id.' . md5($email),
now()->addDays(7),
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') ?? [];
foreach ($users as $userId) {
$details = $this->http->get("/cloud/users/{$userId}");
if (
$details->successful() &&
$details->json('ocs.data.email') === $email
) {
return $userId;
}
}
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

@@ -37,6 +37,7 @@ return [
'dolibarr' => [
'base_url' => env('DOLIBARR_URL'),
'htaccess_url' => env('DOLIBARR_URL_HTACCESS'),
'username' => env('DOLIBARR_USERNAME'),
'password' => env('DOLIBARR_PWD'),
'api_key' => env('DOLIBARR_APIKEY')
@@ -67,5 +68,9 @@ return [
],
'cache_ttl' => env('ISPCONFIG_CACHE_TTL', 300), // 5 minutes
],
'nextcloud' => [
'url' => env('NEXTCLOUD_URL'),
'user' => env('NEXTCLOUD_USERNAME'),
'password' => env('NEXTCLOUD_PASSWORD')
],
];

View File

@@ -0,0 +1,27 @@
<?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('webdomains_members', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('webdomains_members');
}
};

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');
}
};

2
package-lock.json generated
View File

@@ -1,5 +1,5 @@
{
"name": "app",
"name": "roxane",
"lockfileVersion": 3,
"requires": true,
"packages": {

View File

@@ -0,0 +1,36 @@
<div class="space-y-4">
@foreach($ispconfigs as $ispconfig)
<div class="border rounded-lg p-4 bg-white dark:bg-gray-800">
<div class="flex items-start justify-between mb-3">
<h4 class="text-lg font-semibold text-gray-900 dark:text-white">
{{ $ispconfig->data['domain'] ?? 'Domaine non défini' }}
</h4>
<div>
<a
href="#"
target="_blank"
class="inline-flex items-center gap-1 text-sm text-primary-600 hover:text-primary-700 dark:text-primary-400 font-medium"
>
Gérer dans ISPConfig
</a>
</div>
</div>
<div class="grid grid-cols-2 gap-3 text-sm">
@foreach($ispconfig->data as $key => $value)
@if(!is_array($value) && !is_null($value))
<div class="flex flex-col">
<span class="text-gray-500 dark:text-gray-400 text-xs">
{{ ucfirst(str_replace('_', ' ', $key)) }}
</span>
<span class="font-medium text-gray-900 dark:text-white">
{{ $value }}
</span>
</div>
@endif
@endforeach
</div>
</div>
@endforeach
</div>

View File

@@ -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

View File

@@ -19,7 +19,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
// TEST
Route::get('/test/sync-ispconfig', function () {
/*Route::get('/test/sync-ispconfig', function () {
if (!app()->isLocal()) {
abort(403);
@@ -37,6 +37,14 @@ Route::get('/test/isp-mails', function() {
$ispService = new \App\Services\ISPConfig\ISPConfigMailService;
return $ispService->getAllMailDomains();
});*/
Route::get('/test-dolibarr', function () {
$dolibarrService = new \App\Services\Dolibarr\DolibarrService();
$members = $dolibarrService->getAllMembers();
dd($members);
});
require __DIR__.'/settings.php';