Skip to content

Commit

Permalink
extending user handling
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 3, 2014
1 parent 6ee3eca commit e116323
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 36 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ Orderby will also accept an array of multiple identifiers.
overhead in generating the total count so this should only be turned on when needed. This is
automatically turned on if the 'paged' filter is used.</td>
</tr>
<tr>
<td>who</td>
<td>string</td>
<td>Filters to users based on a subset of roles. Currently, only 'authors' is supported.</td>
</tr>
<tr>
<td>callback</td>
<td>string</td>
Expand Down
39 changes: 20 additions & 19 deletions api/v1/controllers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class UsersController {

private static $_model;

private static function get_list_users_cap() {
return apply_filters('thermal_list_users_cap', false);
}

public static function model() {
if ( !isset( self::$_model ) ) {
self::$_model = new UsersModel();
Expand All @@ -16,34 +20,26 @@ public static function model() {
}

public static function find( $app ) {
if ( !is_user_logged_in() ) {
$app->halt( '401', get_status_header_desc( '401' ) );
}

if ( !current_user_can( 'list_users' ) ) {
if ( ( $list_users_cap = self::get_list_users_cap() ) && !current_user_can( $list_users_cap ) ) {
$app->halt( '403', get_status_header_desc( '403' ) );
}

$found = 0;
$posts = array( );
$users = array( );
$request_args = $app->request()->get();

$args = self::convert_request( $request_args );

$model = self::model();

$users = $model->find( $args, $found );
array_walk( $posts, array( __CLASS__, 'format' ), 'read' );
array_walk( $users, array( __CLASS__, 'format' ), 'read' );

return empty( $request_args['no_found_rows'] ) ? compact( 'users', 'found' ) : compact( 'users' );
return $request_args['count_total'] ? compact( 'users', 'found' ) : compact( 'users' );
}

public static function findById( $app, $id ) {
if ( !is_user_logged_in() ) {
$app->halt( '401', get_status_header_desc( '401' ) );
}

if ( !current_user_can( 'list_users' ) && $id !== get_current_user_id() ) {
if ( ( $list_users_cap = self::get_list_users_cap() ) && !current_user_can( $list_users_cap ) && $id !== get_current_user_id() ) {
$app->halt( '403', get_status_header_desc( '403' ) );
}

Expand Down Expand Up @@ -97,8 +93,11 @@ public static function format( &$user, $state = 'read' ) {
'height' => 96,
)
),
'meta' => ( object ) array( )
) );
'meta' => ( object ) array(
'description' => get_user_meta($user->ID, 'description', true)
)
)
);
}

$user = apply_filters_ref_array( 'thermal_user_entity', array( ( object ) $data, &$user, $state ) );
Expand All @@ -117,7 +116,8 @@ public static function convert_request( $request_args ) {
'orderby' => array( ),
'order' => array( ),
'in' => array( __NAMESPACE__ . '\\toArray', __NAMESPACE__ . '\\applyInt' ),
'include_found' => array( __NAMESPACE__ . '\\toBool' )
'include_found' => array( __NAMESPACE__ . '\\toBool' ),
'who' => array( )
);

//strip any nonsafe args
Expand All @@ -140,10 +140,11 @@ public static function convert_request( $request_args ) {
$request_args['per_page'] = MAX_USERS_PER_PAGE;
}

if ( empty( $request_args['paged'] ) && empty( $request_args['include_found'] ) ) {
$request_args['count_total'] = false;
}
$request_args['count_total'] = ! ( empty( $request_args['paged'] ) && empty( $request_args['include_found'] ) );

if ( !empty( $request_args['who']) && !in_array( $request_args['who'], array( 'authors' ) ) ) {
unset( $request_args['who'] );
}
return $request_args;
}

Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: voceplatforms
Tags: thermal, JSON, API
Requires at least: 3.5
Tested up to: 3.5.1
Stable tag: 0.7.7
Stable tag: 0.8.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -29,6 +29,12 @@ Thermal is the WordPress plugin that gives you the power of WP_Query in a RESTfu
Yes. https://github.com/voceconnect/thermal-api

== Changelog ==
= 0.8.0 =
* Made users publicly accessible
* Added description to user's default meta
* Added filter 'thermal_list_users_cap' to allow required cap to be set for viewing user listing
* Fixed bug with how users were returned within index

= 0.7.7 =
* Added 'thermal_response' filter to allow modification of response object.

Expand Down
3 changes: 2 additions & 1 deletion tests/APITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function _getResponse( $envArgs ) {
}

public function setUp() {

parent::setUp();

\Slim\Slim::registerAutoloader();
}

Expand Down
2 changes: 0 additions & 2 deletions tests/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/


require_once 'PHPUnit/Autoload.php';

$config_file_path = dirname( __FILE__ ) . '/../wp-tests-config.php';

/*
Expand Down
117 changes: 105 additions & 12 deletions tests/v1/controllers/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,125 @@

class UsersControllerTest extends APITestCase {

private function getTestUserData() {
return array(
array(
'user_login' => 'test1_login',
'user_pass' => wp_generate_password(),
'user_email' => '[email protected]',
'user_nicename' => 'test1',
'user_url' => 'http://example.org',
'display_name' => 'Test User1',
'description' => 'Test Description',
'role' => 'administrator'
),
array(
'user_login' => 'test2_login',
'user_pass' => wp_generate_password(),
'user_email' => '[email protected]',
'user_nicename' => 'test2',
'user_url' => 'http://example.org',
'display_name' => 'Test User2',
'description' => 'Test Description',
'role' => 'editor'
),
array(
'user_login' => 'test3_login',
'user_pass' => wp_generate_password(),
'user_email' => '[email protected]',
'user_nicename' => 'test3',
'user_url' => 'http://example.org',
'display_name' => 'Test User3',
'description' => 'Test Description',
'role' => 'author'
),
array(
'user_login' => 'test4_login',
'user_pass' => wp_generate_password(),
'user_email' => '[email protected]',
'user_nicename' => 'test4',
'user_url' => 'http://example.org',
'display_name' => 'Test User4',
'description' => 'Test Description',
'role' => 'subscriber'
),
);
}

public function testGetUsers() {

$user = array_shift($this->getTestUserData());
$user['role'] = 'subscriber';
$user['id'] = wp_insert_user($user);

list($status, $headers, $body) = $this->_getResponse( array(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/users/',
'QUERY_STRING' => '',
'QUERY_STRING' => 'who=authors',
) );

$data = json_decode( $body );
$this->assertEquals('401', $status);
$this->assertEquals( '200', $status );
$this->assertInternalType( 'object', $data );
$this->assertObjectHasAttribute( 'users', $data );
$this->assertInternalType( 'array', $data->users );
$this->assertGreaterThanOrEqual( 1, count( $data->users ) );
$this->assertObjectNotHasAttribute( 'found', $data );

//clean up
wp_delete_user($user['id']);
}

public function testGetUser() {
$user_id = wp_insert_user(array(
'user_login' => 'test_get_user',
));
if(is_wp_error($user_id)) {
$user_id = get_user_by('login', 'test_get_user')->ID;
}

$user = array_shift($this->getTestUserData());
$user['role'] = 'editor';
$user['id'] = wp_insert_user($user);

list($status, $headers, $body) = $this->_getResponse( array(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/users/' . $user_id,
'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/users/' . $user['id'],
'QUERY_STRING' => '',
) );

$data = json_decode( $body );
$this->assertEquals('401', $status);

$this->assertEquals( '200', $status );
$this->assertInternalType( 'object', $data );

$this->assertObjectHasAttribute('id', $data );
$this->assertInternalType( 'int', $data->id );
$this->assertEquals( $user['id'], $data->id );

$this->assertObjectHasAttribute('id_str', $data );
$this->assertInternalType( 'string', $data->id_str );
$this->assertEquals( (string) $user['id'], $data->id_str );

$this->assertObjectHasAttribute('nicename', $data );
$this->assertInternalType( 'string', $data->nicename);
$this->assertEquals( $user['user_nicename'], $data->nicename );

$this->assertObjectHasAttribute('display_name', $data );
$this->assertInternalType( 'string', $data->display_name );
$this->assertEquals( $user['display_name'], $data->display_name );

$this->assertObjectHasAttribute('user_url', $data );
$this->assertInternalType( 'string', $data->user_url );
$this->assertEquals( $user['user_url'], $data->user_url );

$this->assertObjectHasAttribute('posts_url', $data );
$this->assertInternalType( 'string', $data->posts_url );

$this->assertObjectHasAttribute('avatar', $data );
$this->assertInternalType( 'array', $data->avatar );

$this->assertObjectHasAttribute('meta', $data );
$this->assertInternalType( 'object', $data->meta );

$data = json_decode( $body );

//clean up
wp_delete_user($user['id']);
}
}


2 changes: 1 addition & 1 deletion thermal-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Plugin Name: Thermal API
Version: 0.7.7
Version: 0.8.0
Plugin URI: http://thermal-api.com/
Description: The power of WP_Query in a RESTful API.
Author: Voce Platforms
Expand Down

0 comments on commit e116323

Please sign in to comment.