361 lines
13 KiB
PHP
361 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ESI_CRVI_AGENDA\controllers;
|
|
|
|
use ESI_CRVI_AGENDA\helpers\Api_Helper;
|
|
use ESI_CRVI_AGENDA\controllers\CRVI_Local_Controller;
|
|
use ESI_CRVI_AGENDA\controllers\CRVI_Traducteur_Controller;
|
|
use ESI_CRVI_AGENDA\controllers\CRVI_Intervenant_Controller;
|
|
use ESI_CRVI_AGENDA\controllers\CRVI_Beneficiaire_Controller;
|
|
|
|
class CRVI_Entity_Controller {
|
|
|
|
public static function register_routes() {
|
|
// Endpoints pour la création de bénéficiaires
|
|
\register_rest_route('crvi/v1', '/agenda/beneficiaires', [
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [CRVI_Beneficiaire_Controller::class, 'create_item'],
|
|
'permission_callback' => [self::class, 'can_create_entity'],
|
|
],
|
|
]);
|
|
|
|
// Endpoints pour la création d'intervenants
|
|
\register_rest_route('crvi/v1', '/agenda/intervenants', [
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [CRVI_Intervenant_Controller::class, 'create_item'],
|
|
'permission_callback' => [self::class, 'can_create_entity'],
|
|
],
|
|
]);
|
|
|
|
// Endpoints pour la création de traducteurs
|
|
\register_rest_route('crvi/v1', '/agenda/traducteurs', [
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [CRVI_Traducteur_Controller::class, 'create_item'],
|
|
'permission_callback' => [self::class, 'can_create_entity'],
|
|
],
|
|
]);
|
|
|
|
// Endpoints pour la création de locaux
|
|
\register_rest_route('crvi/v1', '/agenda/locaux', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [CRVI_Local_Controller::class, 'get_items'],
|
|
'permission_callback' => '__return_true',
|
|
],
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [CRVI_Local_Controller::class, 'create'],
|
|
'permission_callback' => '__return_true', // Temporairement désactivé pour les tests
|
|
],
|
|
]);
|
|
|
|
// Route de test pour diagnostiquer l'authentification
|
|
\register_rest_route('crvi/v1', '/agenda/test-auth', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [self::class, 'test_auth'],
|
|
'permission_callback' => '__return_true',
|
|
],
|
|
]);
|
|
|
|
// Route pour obtenir un nonce d'authentification
|
|
\register_rest_route('crvi/v1', '/agenda/get-nonce', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [self::class, 'get_nonce'],
|
|
'permission_callback' => '__return_true',
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Vérifie si l'utilisateur peut créer des entités
|
|
* Seuls les administrateurs et opérateurs peuvent créer des entités
|
|
*/
|
|
public static function can_create_entity($request = null) {
|
|
// Log pour débogage
|
|
error_log('CRVI Auth - Checking permissions for user: ' . (is_user_logged_in() ? get_current_user_id() : 'not logged in'));
|
|
|
|
// Vérifier d'abord l'authentification par nonce
|
|
$nonce = $request->get_header('X-WP-Nonce');
|
|
if ($nonce && wp_verify_nonce($nonce, 'wp_rest')) {
|
|
error_log('CRVI Auth - Valid nonce provided');
|
|
// Nonce valide, vérifier les permissions
|
|
if (current_user_can('manage_options') || current_user_can('edit_posts')) {
|
|
error_log('CRVI Auth - User has required permissions');
|
|
return true;
|
|
} else {
|
|
error_log('CRVI Auth - User lacks required permissions');
|
|
return new \WP_Error('insufficient_permissions', 'Permissions insuffisantes. Seuls les administrateurs et opérateurs peuvent créer des entités.', ['status' => 403]);
|
|
}
|
|
}
|
|
|
|
// Fallback : vérifier l'authentification normale
|
|
if (!is_user_logged_in()) {
|
|
error_log('CRVI Auth - User not logged in');
|
|
return new \WP_Error('not_logged_in', 'Utilisateur non connecté. Utilisez un nonce valide ou connectez-vous.', ['status' => 401]);
|
|
}
|
|
|
|
$user = wp_get_current_user();
|
|
error_log('CRVI Auth - User roles: ' . implode(', ', $user->roles));
|
|
|
|
// Administrateurs : accès total
|
|
if (current_user_can('manage_options')) {
|
|
error_log('CRVI Auth - Administrator access granted');
|
|
return true;
|
|
}
|
|
|
|
// Opérateurs : peuvent créer des entités
|
|
if (current_user_can('edit_posts')) {
|
|
error_log('CRVI Auth - Operator access granted');
|
|
return true;
|
|
}
|
|
|
|
// Intervenants : lecture seule (pas de création d'entités)
|
|
if (current_user_can('read')) {
|
|
error_log('CRVI Auth - Intervenant access denied (read-only)');
|
|
return new \WP_Error('insufficient_permissions', 'Les intervenants ont un accès en lecture seule.', ['status' => 403]);
|
|
}
|
|
|
|
error_log('CRVI Auth - No valid permissions found');
|
|
return new \WP_Error('insufficient_permissions', 'Permissions insuffisantes.', ['status' => 403]);
|
|
}
|
|
|
|
/**
|
|
* Test d'authentification pour diagnostiquer les problèmes
|
|
*/
|
|
public static function test_auth($request) {
|
|
$auth_info = [
|
|
'is_logged_in' => is_user_logged_in(),
|
|
'current_user_id' => get_current_user_id(),
|
|
'user_roles' => [],
|
|
'permissions' => [
|
|
'manage_options' => current_user_can('manage_options'),
|
|
'edit_posts' => current_user_can('edit_posts'),
|
|
'read' => current_user_can('read'),
|
|
]
|
|
];
|
|
|
|
if (is_user_logged_in()) {
|
|
$user = wp_get_current_user();
|
|
$auth_info['user_roles'] = $user->roles;
|
|
$auth_info['user_login'] = $user->user_login;
|
|
}
|
|
|
|
return \ESI_CRVI_AGENDA\helpers\Api_Helper::json_success($auth_info);
|
|
}
|
|
|
|
/**
|
|
* Obtient un nonce pour l'authentification REST API
|
|
*/
|
|
public static function get_nonce($request) {
|
|
$nonce = wp_create_nonce('wp_rest');
|
|
return \ESI_CRVI_AGENDA\helpers\Api_Helper::json_success([
|
|
'nonce' => $nonce,
|
|
'instructions' => 'Utilisez ce nonce dans l\'en-tête X-WP-Nonce pour les requêtes POST'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Crée un nouveau bénéficiaire
|
|
*/
|
|
/* public static function create_beneficiaire($request) {
|
|
$data = $request->get_json_params();
|
|
|
|
// Validation des données
|
|
$validation = CRVI_Beneficiaire_Controller::validate_beneficiaire_data($data);
|
|
if (is_wp_error($validation)) {
|
|
return Api_Helper::json_error($validation->get_error_message(), 400);
|
|
}
|
|
|
|
// Création du post bénéficiaire
|
|
$post_data = [
|
|
'post_title' => $data['nom'] . ' ' . ($data['prenom'] ?? ''),
|
|
'post_type' => 'beneficiaire',
|
|
'post_status' => 'publish',
|
|
'meta_input' => [
|
|
'nom' => sanitize_text_field($data['nom']),
|
|
'prenom' => sanitize_text_field($data['prenom'] ?? ''),
|
|
'email' => sanitize_email($data['email'] ?? ''),
|
|
'telephone' => sanitize_text_field($data['telephone'] ?? ''),
|
|
]
|
|
];
|
|
|
|
$post_id = wp_insert_post($post_data);
|
|
|
|
if (is_wp_error($post_id)) {
|
|
return Api_Helper::json_error('Erreur lors de la création du bénéficiaire', 500);
|
|
}
|
|
|
|
return Api_Helper::json_success([
|
|
'id' => $post_id,
|
|
'nom' => $data['nom'],
|
|
'prenom' => $data['prenom'] ?? '',
|
|
'message' => 'Bénéficiaire créé avec succès'
|
|
]);
|
|
} */
|
|
|
|
/**
|
|
* Crée un nouvel intervenant
|
|
*/
|
|
/* public static function create_intervenant($request) {
|
|
$data = $request->get_json_params();
|
|
|
|
// Validation des données
|
|
$validation = self::validate_intervenant_data($data);
|
|
if (is_wp_error($validation)) {
|
|
return Api_Helper::json_error($validation->get_error_message(), 400);
|
|
}
|
|
|
|
// Création du post intervenant
|
|
$post_data = [
|
|
'post_title' => $data['nom'] . ' ' . ($data['prenom'] ?? ''),
|
|
'post_type' => 'intervenant',
|
|
'post_status' => 'publish',
|
|
'meta_input' => [
|
|
'nom' => sanitize_text_field($data['nom']),
|
|
'prenom' => sanitize_text_field($data['prenom'] ?? ''),
|
|
'email' => sanitize_email($data['email'] ?? ''),
|
|
'telephone' => sanitize_text_field($data['telephone'] ?? ''),
|
|
'specialite' => sanitize_text_field($data['specialite'] ?? ''),
|
|
]
|
|
];
|
|
|
|
$post_id = wp_insert_post($post_data);
|
|
|
|
if (is_wp_error($post_id)) {
|
|
return Api_Helper::json_error('Erreur lors de la création de l\'intervenant', 500);
|
|
}
|
|
|
|
return Api_Helper::json_success([
|
|
'id' => $post_id,
|
|
'nom' => $data['nom'],
|
|
'prenom' => $data['prenom'] ?? '',
|
|
'message' => 'Intervenant créé avec succès'
|
|
]);
|
|
} */
|
|
|
|
/**
|
|
* Crée un nouveau traducteur
|
|
*/
|
|
/* public static function create_traducteur($request) {
|
|
$data = $request->get_json_params();
|
|
|
|
// Validation des données
|
|
$validation = self::validate_traducteur_data($data);
|
|
if (is_wp_error($validation)) {
|
|
return Api_Helper::json_error($validation->get_error_message(), 400);
|
|
}
|
|
|
|
// Création du post traducteur
|
|
$post_data = [
|
|
'post_title' => $data['nom'] . ' ' . ($data['prenom'] ?? ''),
|
|
'post_type' => 'traducteur',
|
|
'post_status' => 'publish',
|
|
'meta_input' => [
|
|
'nom' => sanitize_text_field($data['nom']),
|
|
'prenom' => sanitize_text_field($data['prenom'] ?? ''),
|
|
'email' => sanitize_email($data['email'] ?? ''),
|
|
'telephone' => sanitize_text_field($data['telephone'] ?? ''),
|
|
'langues_parlees' => sanitize_text_field($data['langues_parlees'] ?? ''),
|
|
]
|
|
];
|
|
|
|
$post_id = wp_insert_post($post_data);
|
|
|
|
if (is_wp_error($post_id)) {
|
|
return Api_Helper::json_error('Erreur lors de la création du traducteur', 500);
|
|
}
|
|
|
|
return Api_Helper::json_success([
|
|
'id' => $post_id,
|
|
'nom' => $data['nom'],
|
|
'prenom' => $data['prenom'] ?? '',
|
|
'message' => 'Traducteur créé avec succès'
|
|
]);
|
|
} */
|
|
|
|
/**
|
|
* Crée un nouveau local
|
|
*/
|
|
/* public static function create_local($request) {
|
|
$data = $request->get_json_params();
|
|
|
|
// Debug: Log les données reçues
|
|
error_log('CRVI Local Create - Data received: ' . json_encode($data));
|
|
|
|
// Validation des données
|
|
$validation = CRVI_Local_Controller::validate_local_data($data);
|
|
if (is_wp_error($validation)) {
|
|
error_log('CRVI Local Create - Validation error: ' . $validation->get_error_message());
|
|
return Api_Helper::json_error($validation->get_error_message(), 400);
|
|
}
|
|
|
|
// Utiliser le modèle pour créer le local
|
|
$model = new \ESI_CRVI_AGENDA\models\CRVI_Local_Model();
|
|
$result = $model->create($data);
|
|
|
|
if (is_wp_error($result)) {
|
|
error_log('CRVI Local Create - Model error: ' . $result->get_error_message());
|
|
return Api_Helper::json_error($result->get_error_message(), $result->get_error_data()['status'] ?? 500);
|
|
}
|
|
|
|
error_log('CRVI Local Create - Success: ' . $result);
|
|
return Api_Helper::json_success([
|
|
'id' => $result,
|
|
'nom' => $data['nom'],
|
|
'message' => 'Local créé avec succès'
|
|
]);
|
|
} */
|
|
|
|
/**
|
|
* Validation des données de bénéficiaire
|
|
*/
|
|
/* private static function validate_beneficiaire_data($data) {
|
|
if (empty($data['nom'])) {
|
|
return new \WP_Error('missing_nom', 'Le nom est obligatoire');
|
|
}
|
|
|
|
if (!empty($data['email']) && !is_email($data['email'])) {
|
|
return new \WP_Error('invalid_email', 'L\'email n\'est pas valide');
|
|
}
|
|
|
|
return true;
|
|
} */
|
|
|
|
/**
|
|
* Validation des données d'intervenant
|
|
*/
|
|
/* private static function validate_intervenant_data($data) {
|
|
if (empty($data['nom'])) {
|
|
return new \WP_Error('missing_nom', 'Le nom est obligatoire');
|
|
}
|
|
|
|
if (!empty($data['email']) && !is_email($data['email'])) {
|
|
return new \WP_Error('invalid_email', 'L\'email n\'est pas valide');
|
|
}
|
|
|
|
return true;
|
|
} */
|
|
|
|
/**
|
|
* Validation des données de traducteur
|
|
*/
|
|
/* private static function validate_traducteur_data($data) {
|
|
if (empty($data['nom'])) {
|
|
return new \WP_Error('missing_nom', 'Le nom est obligatoire');
|
|
}
|
|
|
|
if (!empty($data['email']) && !is_email($data['email'])) {
|
|
return new \WP_Error('invalid_email', 'L\'email n\'est pas valide');
|
|
}
|
|
|
|
return true;
|
|
} */
|
|
|
|
}
|