various fixes in items api controller and permissions handling

This commit is contained in:
Leo Germani 2018-02-10 00:04:51 -02:00
parent d4eebaae64
commit 0d10196abc
5 changed files with 30 additions and 32 deletions

View File

@ -186,23 +186,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$this->item->set_collection($collection);
$field = get_post_meta($collection->get_id());
if(!empty($field)) {
foreach ($field as $key => $value){
$new_field = new Entities\Field();
try {
$set_ = 'set_' . $key;
$new_field->$set_( $value );
} catch (\Error $exception){
// Do nothing
}
}
}
return $new_field;
return $this->item;
}
/**
@ -223,7 +207,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
}
try {
$field = $this->prepare_item_for_database( [ $item, $collection_id ] );
$this->prepare_item_for_database( [ $item, $collection_id ] );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
@ -231,10 +215,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
if($this->item->validate()) {
$item = $this->items_repository->insert($this->item );
$item_metadata = new Entities\Item_Metadata_Entity($item, $field );
$field_added = $this->item_metadata->insert( $item_metadata );
return new WP_REST_Response($field_added->get_item()->__toArray(), 201 );
return new WP_REST_Response($this->item->__toArray(), 201 );
}
@ -255,7 +236,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$collection = $this->collections_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
return $collection->get_items_capabilities()->edit_posts;
return current_user_can($collection->get_items_capabilities()->edit_posts);
}
return false;

View File

@ -332,6 +332,10 @@ class Capabilities {
$role->add_cap($collection_items_caps->$cap);
}
}
// Refresh roles capabilities for current user to have instant effect
global $current_user;
$current_user->get_role_caps();
}
/**

View File

@ -39,7 +39,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods'
'description' => 'Agile methods',
'status' => 'publish'
),
true
);
@ -70,7 +71,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$first_item = $data[0];

View File

@ -69,8 +69,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
true
);
$field = $this->tainacan_field_factory->create_field('text', '', true);
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
@ -78,7 +76,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'description' => 'Descreve valor do campo data.',
'collection' => $collection,
'status' => 'publish',
'field_type' => $field->get_primitive_type(),
'field_type' => 'Tainacan\Field_Types\Text',
),
true
);
@ -142,8 +140,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
true
);
$field = $this->tainacan_field_factory->create_field('text', '', true);
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
@ -151,7 +147,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'description' => 'Descreve o dado do campo data.',
'collection' => $collection,
'status' => 'publish',
'field_type' => $field->get_primitive_type(),
'field_type' => 'Tainacan\Field_Types\Text',
),
true
);

View File

@ -49,7 +49,24 @@ class Permissions extends TAINACAN_UnitTestCase {
wp_set_current_user($new_contributor_user);
$this->assertTrue($collection->can_read());
$this->assertFalse($collection->can_publish());
$this->assertTrue(user_can($new_admin_user, $collection->get_items_capabilities()->edit_posts, $collection->get_id()), 'admin should be able to edit items in the collection');
$privateCollection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testePermsCC',
'description' => 'adasdasdsa',
'status' => 'private'
),
true
);
$this->assertTrue(user_can($new_admin_user, $collection->cap->read_post, $collection->get_id()), 'admin should be able read private collection');
// subsciber should not be able to
$this->assertFalse(user_can($new_user, $collection->cap->read_post, $collection->get_id()), 'subscriber should not be able read private collection');
}
}