Files
roxane/app/Services/Nextcloud/NextcloudService.php
Nebulae b8de637b23
All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 1m9s
fix(NextCloud members and script - part 1)
2026-01-19 15:31:34 +01:00

107 lines
2.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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') ?? [];
}
}