add capabilities param to POST and PATCH roles #274
This commit is contained in:
parent
e9cbaae462
commit
5ce4707230
|
@ -44,6 +44,10 @@ class REST_Roles_Controller extends REST_Controller {
|
|||
'type' => 'string',
|
||||
'required' => true
|
||||
),
|
||||
'capabilities' => array(
|
||||
'description' => __('Array of capabilities, where the keys are capability slugs and values are booleans', 'tainacan'),
|
||||
'required' => false
|
||||
),
|
||||
)
|
||||
),
|
||||
'schema' => [$this, 'get_schema']
|
||||
|
@ -58,6 +62,17 @@ class REST_Roles_Controller extends REST_Controller {
|
|||
'methods' => \WP_REST_Server::EDITABLE,
|
||||
'callback' => array($this, 'update_item'),
|
||||
'permission_callback' => array($this, 'update_item_permissions_check'),
|
||||
'args' => array(
|
||||
'name' => array(
|
||||
'description' => __('New role name', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'required' => false
|
||||
),
|
||||
'capabilities' => array(
|
||||
'description' => __('Array of capabilities, where the keys are capability slugs and values are booleans', 'tainacan'),
|
||||
'required' => false
|
||||
),
|
||||
)
|
||||
),
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
|
@ -124,6 +139,10 @@ class REST_Roles_Controller extends REST_Controller {
|
|||
|
||||
$new_role = add_role($role_slug, $name);
|
||||
|
||||
if ( isset($request['capabilities']) && is_array($request['capabilities']) ) {
|
||||
$this->handle_capabilities_for_role($role_slug, $request['capabilities']);
|
||||
}
|
||||
|
||||
if ($new_role instanceof \WP_Role) {
|
||||
return new \WP_REST_Response($this->_prepare_item_for_response($role_slug, $name, $new_role->capabilities, $request), 201);
|
||||
}
|
||||
|
@ -218,7 +237,11 @@ class REST_Roles_Controller extends REST_Controller {
|
|||
|
||||
}
|
||||
|
||||
if ( isset($request['add_cap']) ) {
|
||||
if ( is_array($request['capabilities']) ) {
|
||||
|
||||
$this->handle_capabilities_for_role($role_slug, $request['capabilities']);
|
||||
|
||||
} elseif ( isset($request['add_cap']) ) {
|
||||
// validate that we only deal with tainacan capabilities
|
||||
if ( ! in_array( \tainacan_roles()->get_cap_generic_name($request['add_cap']) , \tainacan_roles()->get_all_caps_slugs() ) ) {
|
||||
return new \WP_REST_Response([
|
||||
|
@ -242,6 +265,34 @@ class REST_Roles_Controller extends REST_Controller {
|
|||
|
||||
return new \WP_REST_Response($this->_prepare_item_for_response($role_slug, \wp_roles()->roles[$role_slug]['name'], \wp_roles()->roles[$role_slug]['capabilities'], $request), 200);
|
||||
|
||||
}
|
||||
|
||||
private function handle_capabilities_for_role($role_slug, $newcaps) {
|
||||
|
||||
if ( !isset( \wp_roles()->roles[$role_slug] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$role = \wp_roles()->roles[$role_slug];
|
||||
|
||||
foreach ($role['capabilities'] as $cap => $val) {
|
||||
var_dump($cap);
|
||||
if ( ! in_array( \tainacan_roles()->get_cap_generic_name($cap) , \tainacan_roles()->get_all_caps_slugs() ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( !array_key_exists($cap, $newcaps) ) {
|
||||
\wp_roles()->remove_cap($role_slug, $cap);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach ( $newcaps as $cap => $val ) {
|
||||
\wp_roles()->add_cap($role_slug, $cap, $val);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -308,6 +308,114 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
|
|||
$this->assertEquals( 200, $response->get_status(), 'should be permitted');
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @group xis
|
||||
*/
|
||||
public function test_create_get_roles_with_caps() {
|
||||
|
||||
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
||||
|
||||
$request->set_query_params([
|
||||
'name' => 'New role',
|
||||
'capabilities' => [
|
||||
'tnc_rep_edit_collections' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$create = $this->server->dispatch($request);
|
||||
//var_dump($create);
|
||||
$this->assertEquals( 201, $create->get_status() );
|
||||
|
||||
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
|
||||
|
||||
//$request->set_query_params($name_query);
|
||||
|
||||
$name_response = $this->server->dispatch($request);
|
||||
$data = $name_response->get_data();
|
||||
$this->assertArrayHasKey('tainacan-new-role', $data);
|
||||
$this->assertEquals('New role', $data['tainacan-new-role']['name']);
|
||||
|
||||
$role = $data['tainacan-new-role'];
|
||||
|
||||
$this->assertArrayHasKey('tnc_rep_edit_collections', $role['capabilities']);
|
||||
$this->assertTrue($role['capabilities']['tnc_rep_edit_collections']);
|
||||
|
||||
}
|
||||
|
||||
public function test_edit_role_with_caps() {
|
||||
|
||||
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
||||
|
||||
$request->set_query_params([
|
||||
'name' => 'New role',
|
||||
'capabilities' => [
|
||||
'tnc_rep_edit_collections' => true,
|
||||
'tnc_rep_delete_collections' => true,
|
||||
'tnc_rep_edit_taxonomies' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$create = $this->server->dispatch($request);
|
||||
$this->assertEquals( 201, $create->get_status() );
|
||||
|
||||
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role');
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( translate_user_role('New role'), $data['name'] );
|
||||
$this->assertArrayHasKey('tnc_rep_edit_collections', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_edit_collections']);
|
||||
$this->assertArrayHasKey('tnc_rep_delete_collections', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_delete_collections']);
|
||||
$this->assertArrayHasKey('tnc_rep_edit_taxonomies', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_edit_taxonomies']);
|
||||
|
||||
|
||||
|
||||
// EDIT
|
||||
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
|
||||
|
||||
$request->set_query_params(
|
||||
[
|
||||
'name' => 'Changed name',
|
||||
'capabilities' => [
|
||||
'tnc_rep_edit_collections' => true,
|
||||
'tnc_rep_delete_collections' => true,
|
||||
'tnc_rep_edit_metadata' => true // replaced tnc_rep_edit_taxonomies by tnc_rep_edit_metadata
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role');
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( translate_user_role('Changed name'), $data['name'] );
|
||||
$this->assertArrayHasKey('tnc_rep_edit_collections', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_edit_collections']);
|
||||
$this->assertArrayHasKey('tnc_rep_delete_collections', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_delete_collections']);
|
||||
$this->assertArrayHasKey('tnc_rep_edit_metadata', $data['capabilities']);
|
||||
$this->assertTrue($data['capabilities']['tnc_rep_edit_metadata']);
|
||||
|
||||
$this->assertArrayNotHasKey('tnc_rep_edit_taxonomies', $data['capabilities']);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue