Skip to content

Commit 330940f

Browse files
authored
Merge pull request #6 from creativecommons/remove-user-list-api
add filter to remove user list endpoint on WP API rest
2 parents f9a07fd + 4f9cdd7 commit 330940f

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

inc/filters.php

+66-32
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,85 @@
11
<?php
22

33
/*
4-
Filter gravityforms countries list to show them with country code
4+
Filter gravityforms countries list to show them with country code
55
*/
6-
add_filter('gform_countries', function ($countries) {
7-
$new_countries = array();
6+
add_filter(
7+
'gform_countries',
8+
function ( $countries ) {
9+
$new_countries = array();
810

9-
foreach ($countries as $country) {
10-
$code = GF_Fields::get('address')->get_country_code($country);
11-
$new_countries[$code] = $country;
12-
}
11+
foreach ( $countries as $country ) {
12+
$code = GF_Fields::get( 'address' )->get_country_code( $country );
13+
$new_countries[ $code ] = $country;
14+
}
1315

14-
return $new_countries;
15-
});
16+
return $new_countries;
17+
}
18+
);
1619

17-
add_filter('body_class', function( $classes ){
18-
$classes[] = ( is_user_logged_in() ) ? 'logged-in' : 'not-logged-in';
19-
$classes[] = ( bp_commoners::current_user_is_accepted() ) ? 'accepted-member' : '';
20-
return $classes;
21-
});
20+
add_filter(
21+
'body_class',
22+
function( $classes ) {
23+
$classes[] = ( is_user_logged_in() ) ? 'logged-in' : 'not-logged-in';
24+
$classes[] = ( bp_commoners::current_user_is_accepted() ) ? 'accepted-member' : '';
25+
return $classes;
26+
}
27+
);
2228

2329
/*
24-
Disable admin bar except for administrators
30+
Disable admin bar except for administrators
2531
*/
2632

27-
add_action('after_setup_theme', 'remove_admin_bar');
33+
add_action( 'after_setup_theme', 'remove_admin_bar' );
2834

29-
function remove_admin_bar()
30-
{
31-
if (!current_user_can('administrator') && !is_admin()) {
32-
show_admin_bar(false);
33-
}
35+
function remove_admin_bar() {
36+
if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
37+
show_admin_bar( false );
38+
}
3439
}
3540
/**
3641
* We add member metadata when and administrator is added to the website
3742
* The administraror will ve an approved individual member but won't be listed in the members section because isn't a subscriber
3843
*/
3944
function add_admin_member_metadata( $user_id ) {
40-
if ( !current_user_can( 'edit_user', $user_id ) ) {
41-
return false;
42-
}
43-
if ( isset( $_POST['md_multiple_roles'] ) && ( in_array( 'administrator', $_POST['md_multiple_roles'] ) ) ) {
44-
ccgn_user_set_individual_applicant( $user_id );
45-
_ccgn_registration_user_set_stage( $user_id, 'accepted' );
46-
}
45+
if ( ! current_user_can( 'edit_user', $user_id ) ) {
46+
return false;
47+
}
48+
if ( isset( $_POST['md_multiple_roles'] ) && ( in_array( 'administrator', $_POST['md_multiple_roles'] ) ) ) {
49+
ccgn_user_set_individual_applicant( $user_id );
50+
_ccgn_registration_user_set_stage( $user_id, 'accepted' );
51+
}
52+
}
53+
// Hooks for user (Update/add)
54+
add_action( 'personal_options_update', 'add_admin_member_metadata' );
55+
add_action( 'edit_user_profile_update', 'add_admin_member_metadata' );
56+
add_action( 'user_register', 'add_admin_member_metadata' ); // When adding new users
57+
58+
/**
59+
* Wrap an existing default callback passed in parameter and create
60+
* a new permission callback introducing preliminary checks and
61+
* falling-back on the default callback in case of success.
62+
*/
63+
add_filter( 'rest_endpoints', 'api_users_endpoint_force_auth' );
64+
function permission_callback_hardener( $existing_callback ) {
65+
return function ( $request ) use ( $existing_callback ) {
66+
if ( ! current_user_can( 'list_users' ) ) {
67+
return new WP_Error(
68+
'rest_user_cannot_view',
69+
__( 'Sorry, you are not allowed to access users.' ),
70+
[ 'status' => rest_authorization_required_code() ]
71+
);
72+
}
73+
74+
return $existing_callback( $request );
75+
};
76+
}
77+
function api_users_endpoint_force_auth( $endpoints ) {
78+
$users_get_route = &$endpoints['/wp/v2/users'][0];
79+
$users_get_route['permission_callback'] = permission_callback_hardener( $users_get_route['permission_callback'] );
80+
81+
$user_get_route = &$endpoints['/wp/v2/users/(?P<id>[\d]+)'][0];
82+
$user_get_route['permission_callback'] = permission_callback_hardener( $user_get_route['permission_callback'] );
83+
84+
return $endpoints;
4785
}
48-
//Hooks for user (Update/add)
49-
add_action('personal_options_update', 'add_admin_member_metadata');
50-
add_action('edit_user_profile_update', 'add_admin_member_metadata');
51-
add_action('user_register', 'add_admin_member_metadata'); //When adding new users

0 commit comments

Comments
 (0)