Advertisement
CaptainStarbuck

users_rest.php

Mar 9th, 2018
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.07 KB | None | 0 0
  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3.  
  4. # MantisBT is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # MantisBT is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. /**
  18.  * A webservice interface to Mantis Bug Tracker
  19.  *
  20.  * @package MantisBT
  21.  * @copyright Copyright MantisBT Team - mantisbt-dev@lists.sourceforge.net
  22.  * @link http://www.mantisbt.org
  23.  */
  24.  
  25. $g_app->group('/users', function() use ( $g_app ) {
  26.     $g_app->get( '/me', 'rest_user_get_me' );
  27.     $g_app->get( '', 'rest_users_get' );
  28.     $g_app->get( '/', 'rest_users_get' );
  29.  
  30.     $g_app->post( '/', 'rest_user_create' );
  31.     $g_app->post( '', 'rest_user_create' );
  32.  
  33.     $g_app->delete( '/{id}', 'rest_user_delete' );
  34.     $g_app->delete( '/{id}/', 'rest_user_delete' );
  35. });
  36.  
  37. /**
  38.  * A method that does the work to get information about current logged in user.
  39.  *
  40.  * @param \Slim\Http\Request $p_request   The request.
  41.  * @param \Slim\Http\Response $p_response The response.
  42.  * @param array $p_args Arguments
  43.  * @return \Slim\Http\Response The augmented response.
  44.  */
  45. function rest_user_get_me( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
  46.     $t_result = mci_user_get( auth_get_current_user_id() );
  47.     return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_result );
  48. }
  49.  
  50.  
  51. /**
  52.  * A method to get information about all users.
  53.  * Does not yet support arguments for sorting or filtering
  54.  *
  55.  * @param \Slim\Http\Request $p_request   The request.
  56.  * @param \Slim\Http\Response $p_response The response.
  57.  * @param array $p_args Arguments
  58.  * @return \Slim\Http\Response The augmented response.
  59.  */
  60. function rest_users_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
  61.     $t_query = 'SELECT id FROM {user}'; // WHERE ' . $t_where . ' ORDER BY ' . $c_sort . ' ' . $c_dir;
  62.     $t_result = db_query( $t_query ); //, $t_where_params, $p_per_page, $t_offset );
  63.  
  64.     $t_users = array();
  65.     while( $t_row = db_fetch_array( $t_result ) ) {
  66.         $t_user = mci_user_get( $t_row['id'] );
  67.     $t_users[] = $t_user;
  68.     }  
  69.    
  70.     return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_users );
  71. }
  72.  
  73. /**
  74.  * A method that creates a user.
  75.  *
  76.  * @param \Slim\Http\Request $p_request   The request.
  77.  * @param \Slim\Http\Response $p_response The response.
  78.  * @param array $p_args Arguments
  79.  * @return \Slim\Http\Response The augmented response.
  80.  */
  81. function rest_user_create( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
  82.     $t_payload = $p_request->getParsedBody();
  83.     if( $t_payload === null ) {
  84.         return $p_response->withStatus( HTTP_STATUS_BAD_REQUEST, "Unable to parse body, specify content type" );
  85.     }
  86.  
  87.     $t_data = array( 'payload' => $t_payload );
  88.     $t_command = new UserCreateCommand( $t_data );
  89.     $t_result = $t_command->execute();
  90.     $t_user_id = $t_result['id'];
  91.  
  92.     return $p_response->withStatus( HTTP_STATUS_CREATED, "User created with id $t_user_id" )->
  93.         withJson( array( 'user' => mci_user_get( $t_user_id ) ) );
  94. }
  95.  
  96. /**
  97.  * Delete an user given its id.
  98.  *
  99.  * @param \Slim\Http\Request $p_request   The request.
  100.  * @param \Slim\Http\Response $p_response The response.
  101.  * @param array $p_args Arguments
  102.  * @return \Slim\Http\Response The augmented response.
  103.  */
  104. function rest_user_delete( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
  105.     $t_user_id = $p_args['id'];
  106.  
  107.     $t_data = array(
  108.         'query' => array( 'id' => $t_user_id )
  109.     );
  110.  
  111.     $t_command = new UserDeleteCommand( $t_data );
  112.     $t_command->execute();
  113.  
  114.     return $p_response->withStatus( HTTP_STATUS_NO_CONTENT );
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement