From 59054f32f69463197692a7a3fb4ef4d655e8c585 Mon Sep 17 00:00:00 2001 From: leogermani Date: Mon, 4 Nov 2019 15:38:04 -0300 Subject: [PATCH] add endpoint to get a collection caps/roles #274 --- .../class-tainacan-rest-roles-controller.php | 66 +++++++++++++++++++ tests/test-api-roles.php | 49 +++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/api/endpoints/class-tainacan-rest-roles-controller.php b/src/api/endpoints/class-tainacan-rest-roles-controller.php index 0716bb483..8110aca84 100644 --- a/src/api/endpoints/class-tainacan-rest-roles-controller.php +++ b/src/api/endpoints/class-tainacan-rest-roles-controller.php @@ -83,6 +83,15 @@ class REST_Roles_Controller extends REST_Controller { ), 'schema' => [$this, 'get_schema'] )); + register_rest_route( + $this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_collection_roles'), + 'permission_callback' => array($this, 'get_collection_roles_permissions_check'), + ) + )); } @@ -338,6 +347,63 @@ class REST_Roles_Controller extends REST_Controller { return current_user_can('tnc_rep_edit_users'); } + /** + * @param \WP_REST_Request $request + * + * @return bool|\WP_Error + */ + public function get_collection_roles_permissions_check( $request ) { + return current_user_can('tnc_rep_edit_users'); + } + + /** + * @param \WP_REST_Request $request + * + * @return \WP_Error|\WP_REST_Response + */ + public function get_collection_roles( $request ) { + + $collection_id = $request['collection_id']; + + $roles = \wp_roles()->roles; + + $caps = \tainacan_roles()->get_all_caps(); + $col_caps = []; + foreach ($caps as $cap => $c) { + if ( \strpos($cap, 'tnc_col_') === 0 || \strpos($cap, 'manage_tainacan_collection_') === 0 ) { + $col_caps[$cap] = $c; + } + } + + foreach ($col_caps as $cap => $c) { + $col_caps[$cap]['roles'] = []; + foreach ($roles as $slug => $role) { + + // capabilities we are looking for + $caps_aliases = [ + str_replace('%d', $collection_id, $cap), + str_replace('%d', 'all', $cap) + ]; + foreach ($caps_aliases as $alias) { + if ( array_key_exists($alias, $role['capabilities']) ) { + $col_caps[$cap]['roles'][$slug] = [ + 'slug' => $slug, + 'name' => translate_user_role($role['name']), + ]; + break; + } + + } // for each alias + + } // for each role + + } // for each cap + + return new \WP_REST_Response(['capabilities' => $col_caps], 200); + + } + + function get_schema() { $schema = [ diff --git a/tests/test-api-roles.php b/tests/test-api-roles.php index f46f18ebd..772eddd0d 100644 --- a/tests/test-api-roles.php +++ b/tests/test-api-roles.php @@ -14,7 +14,7 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { public static function setUpBeforeClass() { parent::setUpBeforeClass(); $role = get_role('administrator'); - $role->add_cap('edit_tainacan_users'); + $role->add_cap('tnc_rep_edit_users'); global $current_user; $current_user->get_role_caps(); } @@ -160,6 +160,53 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertTrue($role['capabilities']['upload_files']); } + public function test_get_collection_roles() { + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'teste', + 'status' => 'publish' + ), + true + ); + + $editor = get_role('editor'); + $contributor = get_role('contributor'); + $author = get_role('author'); + + $editor->add_cap('manage_tainacan_collection_' . $collection->get_id()); + + $author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_items' ); + $author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_metadata' ); + $author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_filters' ); + + $contributor->add_cap( 'tnc_col_all_edit_items' ); + $contributor->add_cap( 'tnc_col_all_edit_published_items' ); + + + $request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/roles'); + + $response = $this->server->dispatch($request); + //var_dump($create); + $this->assertEquals( 200, $response->get_status() ); + + $caps = $response->get_data()['capabilities']; + + $this->assertArrayHasKey('editor', $caps['manage_tainacan_collection_%d']['roles']); + + $this->assertArrayHasKey('author', $caps['tnc_col_%d_edit_items']['roles']); + $this->assertArrayHasKey('author', $caps['tnc_col_%d_edit_metadata']['roles']); + $this->assertArrayHasKey('author', $caps['tnc_col_%d_edit_filters']['roles']); + + $this->assertArrayHasKey('contributor', $caps['tnc_col_%d_edit_items']['roles']); + $this->assertArrayHasKey('contributor', $caps['tnc_col_%d_edit_published_items']['roles']); + + + + + } + } ?>