2026-01-11 18:13:18 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Nextcloud;
|
|
|
|
|
|
|
2026-01-18 21:51:57 +01:00
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
2026-01-18 14:35:41 +01:00
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
2026-01-19 15:31:34 +01:00
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2026-01-11 18:13:18 +01:00
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2026-01-18 14:35:41 +01:00
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-01-11 18:13:18 +01:00
|
|
|
|
|
|
|
|
|
|
class NextcloudService
|
|
|
|
|
|
{
|
2026-01-18 14:35:41 +01:00
|
|
|
|
protected PendingRequest $http;
|
2026-01-11 18:13:18 +01:00
|
|
|
|
|
2026-01-18 14:35:41 +01:00
|
|
|
|
public function __construct()
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->http = Http::withBasicAuth(
|
|
|
|
|
|
config('services.nextcloud.user'),
|
2026-01-11 18:13:18 +01:00
|
|
|
|
config('services.nextcloud.password')
|
2026-01-18 14:35:41 +01:00
|
|
|
|
)
|
|
|
|
|
|
->withHeaders([
|
|
|
|
|
|
'OCS-APIRequest' => 'true',
|
|
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
|
|
])
|
|
|
|
|
|
->baseUrl(config('services.nextcloud.url') . '/ocs/v1.php');
|
2026-01-11 18:13:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 14:35:41 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Désactive un utilisateur Nextcloud à partir de son email
|
2026-01-18 21:51:57 +01:00
|
|
|
|
* @throws ConnectionException
|
2026-01-18 14:35:41 +01:00
|
|
|
|
*/
|
|
|
|
|
|
public function disableUserByEmail(string $email): void
|
2026-01-11 18:13:18 +01:00
|
|
|
|
{
|
2026-01-18 14:35:41 +01:00
|
|
|
|
$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 l’email
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function findUserIdByEmail(string $email): ?string
|
|
|
|
|
|
{
|
2026-01-19 15:31:34 +01:00
|
|
|
|
return Cache::remember(
|
|
|
|
|
|
'nextcloud.user_id.' . md5($email),
|
|
|
|
|
|
now()->addDays(7),
|
|
|
|
|
|
function () use ($email) {
|
|
|
|
|
|
return $this->resolveUserIdByEmail($email);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-01-18 14:35:41 +01:00
|
|
|
|
|
2026-01-19 15:31:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws ConnectionException
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function resolveUserIdByEmail(string $email): ?string
|
|
|
|
|
|
{
|
|
|
|
|
|
$response = $this->http->get('/cloud/users');
|
2026-01-18 14:35:41 +01:00
|
|
|
|
|
|
|
|
|
|
$users = $response->json('ocs.data.users') ?? [];
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($users as $userId) {
|
|
|
|
|
|
$details = $this->http->get("/cloud/users/{$userId}");
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
$details->successful() &&
|
2026-01-19 15:31:34 +01:00
|
|
|
|
$details->json('ocs.data.email') === $email
|
2026-01-18 14:35:41 +01:00
|
|
|
|
) {
|
|
|
|
|
|
return $userId;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2026-01-11 18:13:18 +01:00
|
|
|
|
}
|
2026-01-19 15:31:34 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @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') ?? [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-11 18:13:18 +01:00
|
|
|
|
}
|