Skip to content
Snippets Groups Projects
Select Git revision
  • d75cdfb94799269e97905e31183ed19963d1c9aa
  • master default
  • rocky-linux-9
  • development
  • v1.0.4
  • v1.0.3
  • v1.0.2
7 results

rest-web-service.php

Blame
  • rest-web-service.php 2.69 KiB
    <?php
    
    /**
     * REST Web Service using http://flightphp.com/
     * This Web Service should be called only by authorized applications external to
     * RAP, so this directory should be password protected.
     * 
     * Apache configuration:
     * <Location "/rap-ia2/ws">
     *     AuthType basic
     *     AuthName RAP
     *     AuthUserFile rap-service-passwd
     *     Require valid-user
     * </Location>
     */
    //
    $WS_PREFIX = '/ws';
    
    /**
     * Retrieve user information from login token.
     */
    Flight::route('GET ' . $WS_PREFIX . '/user-info', function() {
    
        global $dao;
    
        $token = Flight::request()->query['token'];
        $userData = $dao->findLoginToken($token);
    
        if (is_null($userData)) {
            http_response_code(404);
            die("Token not found");
        }
    
        $dao->deleteLoginToken($token);
    
        header('Content-Type: text/plain');
        echo $userData;
    });
    
    /**
     * Retrieve user information from user ID.
     */
    Flight::route('GET ' . $WS_PREFIX . '/user/@userId', function($userId) {
    
        global $dao;
    
        $user = $dao->findUserById($userId);
        if ($user !== null) {
            header('Content-Type: application/json');
            echo json_encode($user);
        } else {
            http_response_code(404);
            die("User not found");
        }
    });
    
    /**
     * Search users from search text (name, surname, email).
     */
    Flight::route('GET ' . $WS_PREFIX . '/user', function() {
    
        global $dao;
    
        $searchText = Flight::request()->query['search'];
        $users = $dao->searchUser($searchText);
        echo json_encode($users);
    });
    
    /**
     * Create new user from identity data. Return the new user encoded in JSON.
     */
    Flight::route('POST ' . $WS_PREFIX . '/user', function() {
    
        global $userHandler;
    
        $postData = Flight::request()->data;
    
        $user = new RAP\User();
    
        $identity = new RAP\Identity($postData['type']);
    
        $identity->email = $postData['email'];
        $identity->typedId = $postData['typedId'];
        if (isset($postData['name'])) {
            $identity->name = $postData['name'];
        }
        if (isset($postData['surname'])) {
            $identity->surname = $postData['surname'];
        }
        if (isset($postData['institution'])) {
            $identity->institution = $postData['institution'];
        }
        if (isset($postData['eppn'])) {
            $identity->eppn = $postData['eppn'];
        }
    
        $user->addIdentity($identity);
    
        $userHandler->saveUser($user);
    
        echo json_encode($user);
    });
    
    /**
     * Performs a join.
     */
    Flight::route('POST ' . $WS_PREFIX . '/join', function() {
    
        global $userHandler;
        $postData = Flight::request()->data;
    
        $userHandler->joinUsers($postData['user1'], $postData['user2']);
    
        // if the join has success, returns the remaining user id
        echo $postData['user1'];
    });
    
    Flight::route('GET ' . $WS_PREFIX . '/test', function() {
        
    });