vendor/pierre-granger/apidae-membres/src/ApidaeMembres.php line 36

  1. <?php
  2. /**
  3.  * Documentation pour le fichier ApidaeMembres.php
  4.  * 
  5.  * 
  6.  */
  7. namespace PierreGranger;
  8. /**
  9.  * Documentation pour la classe ApidaeMembres
  10.  * 
  11.  * @author    Pierre Granger    <pierre@pierre-granger.fr>
  12.  * 
  13.  * 
  14.  */
  15. class ApidaeMembres extends ApidaeCore
  16. {
  17.     protected $projet_consultation_projetId null;
  18.     protected $projet_consultation_apiKey null;
  19.     private $servicesMU = [
  20.         'GET' => ['utilisateur/get-by-id''utilisateur/get-by-mail''utilisateur/get-by-membre''utilisateur/get-all-utilisateurs''membre/get-by-id'],
  21.         'POST' => ['membre/get-membres']
  22.     ];
  23.     const EXCEPTION_NOT_IN_MEMBRES 1;
  24.     const EXCEPTION_NOT_FILLEUL 2;
  25.     const EXCEPTION_NO_PERMISSION 3;
  26.     public function __construct(array $params null)
  27.     {
  28.         parent::__construct($params);
  29.         $this->timeout 30;
  30.         if (isset($params['projet_consultation_projetId']) && preg_match('#^[0-9]+$#'$params['projet_consultation_projetId']))
  31.             $this->projet_consultation_projetId $params['projet_consultation_projetId'];
  32.         else
  33.             throw new \Exception('missing projet_consultation_projetId');
  34.         if (isset($params['projet_consultation_apiKey']) && preg_match('#^[a-zA-Z0-9]{1,20}$#'$params['projet_consultation_apiKey']))
  35.             $this->projet_consultation_apiKey $params['projet_consultation_apiKey'];
  36.         else
  37.             throw new \Exception('missing projet_consultation_apiKey');
  38.     }
  39.     /**
  40.      * Récupère un Array des membres selon le $filter
  41.      * 
  42.      * @since    1.0
  43.      * 
  44.      * @return    array    Tableau associatif des membres
  45.      */
  46.     public function getMembres(array $filter$responseFields null)
  47.     {
  48.         $query = [
  49.             'projetId' => $this->projet_consultation_projetId,
  50.             'apiKey' => $this->projet_consultation_apiKey,
  51.             'filter' => $filter
  52.         ];
  53.         if (isset($responseFields) && $responseFields != null)
  54.             $query['responseFields'] = is_array($responseFields) ? json_encode($responseFields) : $responseFields;
  55.         return $this->apidaeCurlMU('membre/get-membres'$query);
  56.     }
  57.     /**
  58.      * Récupère la liste des filleuls selon l'idParrain
  59.      * 
  60.      * @param int $idParrain    int    Identifiant du membre parrain
  61.      * @return array Tableau associatif des membres filleuls
  62.      */
  63.     public function getFilleuls(int $idParrain, array $types null)
  64.     {
  65.         $filter = ['idParrain' => $idParrain];
  66.         if ($types == null || !is_array($types)) $types = ['Contributeur Généraliste'];
  67.         if (is_array($types) && sizeof($types) > 0$filter['types'] = $types;
  68.         $responseFields json_encode(["UTILISATEURS"]);
  69.         return $this->getMembres($filter$responseFields);
  70.     }
  71.     /**
  72.      * Récupération d'un utilisateur par son identifiant, via le service get-by-id de l'API Membres/utilisateurs d'Apidae
  73.      * @param int    $id_user
  74.      * @return array 
  75.      */
  76.     public function getUserById(int $id_user)
  77.     {
  78.         if (!preg_match('#^[0-9]+$#'$id_user)) throw new \Exception(__LINE__ " Invalid id_user for getUserById : " $id_user);
  79.         $query = [
  80.             'projetId' => $this->projet_consultation_projetId,
  81.             'apiKey' => $this->projet_consultation_apiKey
  82.         ];
  83.         return $this->apidaeCurlMU('utilisateur/get-by-id'$query$id_user);
  84.     }
  85.     public function getUtilisateur($var)
  86.     {
  87.         return $this->getUser($var);
  88.     }
  89.     public function getUser($var)
  90.     {
  91.         if (is_int($var)) return $this->getUserById($var);
  92.         elseif ((strpos($var'@')) !== false) return $this->getUserByMail($var);
  93.         return false;
  94.     }
  95.     /**
  96.      * Récupération d'un utilisateur par son adresse mail, via le service get-by-mail de l'API Membres/utilisateurs d'Apidae
  97.      * @param string    $mail_user
  98.      * @return array 
  99.      */
  100.     public function getUserByMail(string $mail_user)
  101.     {
  102.         if (false === filter_var($mail_userFILTER_VALIDATE_EMAIL)) throw new \Exception(__LINE__ " Invalid mail_user for getUserByMail : " $mail_user);
  103.         $params = [
  104.             'projetId' => $this->projet_consultation_projetId,
  105.             'apiKey' => $this->projet_consultation_apiKey
  106.         ];
  107.         return $this->apidaeCurlMU('utilisateur/get-by-mail'$params$mail_user);
  108.     }
  109.     /**
  110.      * Récupération de la liste des utilisateurs d'un membre par son identifiant, via le service get-by-membre de l'API Membres/utilisateurs d'Apidae
  111.      * @param int    $id_membre
  112.      * @return array 
  113.      */
  114.     public function getUsersByMember(int $id_membre)
  115.     {
  116.         if (!preg_match('#^[0-9]+$#'$id_membre)) throw new \Exception(__LINE__ ' Invalid id_membre for ' __FUNCTION__ ' : ' $id_membre);
  117.         $params = [
  118.             'projetId' => $this->projet_consultation_projetId,
  119.             'apiKey' => $this->projet_consultation_apiKey
  120.         ];
  121.         return $this->apidaeCurlMU('utilisateur/get-by-membre'$params$id_membre);
  122.     }
  123.     /**
  124.      *    Récupère un membre en fonction de son identifiant
  125.      *     @param int $id_membre
  126.      *     @return array
  127.      */
  128.     public function getMembreById(int $id_membre, array $responseFields null)
  129.     {
  130.         if (!preg_match('#^[0-9]+$#'$id_membre)) throw new \Exception(__LINE__ ' Invalid id_membre for ' __FUNCTION__ ' : ' $id_membre);
  131.         $query = [
  132.             'projetId' => $this->projet_consultation_projetId,
  133.             'apiKey' => $this->projet_consultation_apiKey
  134.         ];
  135.         if (isset($responseFields) && $responseFields != null && is_array($responseFields))
  136.             $query['responseFields'] = json_encode($responseFields);
  137.         return $this->apidaeCurlMU('membre/get-by-id'$query$id_membre);
  138.     }
  139.     public function getMembre(int $id_membre, array $responseFields null)
  140.     {
  141.         return $this->getMembreById($id_membre$responseFields);
  142.     }
  143.     /**
  144.      * Cet appel ne liste pas les utilisateurs enregistrés ou parrainés.
  145.      */
  146.     public function getAllUtilisateurs()
  147.     {
  148.         $this->timeout 600;
  149.         $query = [
  150.             'projetId' => $this->projet_consultation_projetId,
  151.             'apiKey' => $this->projet_consultation_apiKey
  152.         ];
  153.         $ret $this->apidaeCurlMU('utilisateur/get-all-utilisateurs'$query);
  154.         $this->timeout 15;
  155.         return $ret;
  156.     }
  157.     /**
  158.      * Gestion des appels cURL aux API membres et utilisateurs d'Apidae
  159.      * 
  160.      * @param    string    $service    Service (chemin relatif)
  161.      * @param    array    $params    Liste de paramètres qui seront envoyées en cURL : en POST elles seront converties via json_encode, en GET elles seront converties via http_build_query.
  162.      */
  163.     private function apidaeCurlMU(string $service, array $params nullstring $page null)
  164.     {
  165.         $debug $this->debug;
  166.         $method null;
  167.         if (in_array($service$this->servicesMU['GET'])) $method 'GET';
  168.         elseif (in_array($service$this->servicesMU['POST'])) $method 'POST';
  169.         else throw new \Exception(__LINE__ " Invalid function for apidaeCurl : " $service);
  170.         $url_base '/api/v002/' $service '/';
  171.         $url $url_base;
  172.         if ($page !== null && preg_match('#^[a-zA-Z0-9\@\.-]+$#'$page)) $url .= $page;
  173.         /**
  174.          * 19/02/2021 Fun fact syntaxe responseFields :
  175.          * Méthode en GET :
  176.          * get-membres/?query={"projetId":X,"apiKey":"Y","filter":{"idProjet":Z},"responseFields":["PROJETS"]}
  177.          * $params["responseFields"] doit être DEJA json_encode, sinon il faut l'encoder ici
  178.          * Méthode en POST
  179.          * get-by-id/1157?projetId=X&apiKey=Y&responseFields=["PROJETS"]
  180.          * $params["responseFields"] ne doit PAS être json_encode
  181.          * Sinon on se retrouve avec responseFields="[\"PROJETS\"]"
  182.          */
  183.         // On ne demande pas forcément du json, parce que la réponse peut être une 404 (donc format incorrect)
  184.         //$request_params = Array('format' => 'json') ;
  185.         $request_params = [];
  186.         if ($method == 'GET') {
  187.             if (isset($params['responseFields']) && is_array($params['responseFields']))
  188.                 $params['responseFields'] = json_encode($params['responseFields']);
  189.             $url .= '?' http_build_query($params);
  190.         }
  191.         if ($method == 'POST') {
  192.             if (isset($params['responseFields']) && !is_array($params['responseFields'])) {
  193.                 $test json_decode($params['responseFields'], true);
  194.                 if (json_last_error() == JSON_ERROR_NONE)
  195.                     $params['responseFields'] = $test;
  196.             }
  197.             // Force filter to {} instead of [] if empty
  198.             if (isset($params['filter']) && is_array($params['filter']))
  199.                 $params['filter'] = (object)$params['filter'];
  200.             $request_params['POST'] = 1;
  201.             $request_params['POSTFIELDS'] = 'query=' json_encode($params);
  202.         }
  203.         $result $this->request($url$request_params);
  204.         if ($result['code'] == 204) return false;
  205.         if ($result['code'] != 200) {
  206.             $details = [];
  207.             if ($this->debug) {
  208.                 $details['params'] = $params;
  209.                 $details['url'] = $url;
  210.                 $details['result'] = $result;
  211.             }
  212.             throw new ApidaeException(__CLASS__ ':' $service ' : incorrect http_code ' $result['code'], ApidaeException::INVALID_HTTPCODE$details);
  213.         }
  214.         return json_decode($result['body'], true);
  215.     }
  216.     /**
  217.      * L'utilisateur a-t-il les droits demandés ?
  218.      * @param    $utilisateurApidae    Array    Utilisateur Apidae défini par l'authentification SSO
  219.      * @param    $droits    Array    Droits qu'on va rechercher sur cet utilisateur
  220.      *                             $droits['membres']    Array    Le membre de l'utilisateur est-il dans la liste $droits['membres'] ? (liste d'identifiants numériques)
  221.      *                             $droits['filleuls']    Integer    Le membre de l'utilisateur est-il filleur du membre $droits['filleuls'] ?
  222.      *                             $droits['permissions']    Array    L'utilisateur a-t-il les permissions $droits['permissions'] ? (liste de string)
  223.      * 
  224.      */
  225.     public function droits($utilisateurApidae$droits)
  226.     {
  227.         $return true;
  228.         foreach ($droits as $droit => $valeurs) {
  229.             if ($droit == 'membres') {
  230.                 if (!in_array($utilisateurApidae['membre']['id'], $valeurs))
  231.                     return false;
  232.             }
  233.             if ($droit == 'filleuls') {
  234.                 $filleuls $this->getFilleuls($valeurs);
  235.                 if (!in_array($utilisateurApidae['membre']['id'], $filleuls))
  236.                     return false;
  237.             }
  238.             if ($droit == 'permissions') {
  239.                 $usr $this->getUserById($utilisateurApidae['id']);
  240.                 foreach ($valeurs as $p) {
  241.                     if (!in_array($p$usr['permissions']))
  242.                         return false;
  243.                 }
  244.             }
  245.         }
  246.         return $return;
  247.     }
  248. }