various fixes in items api controller and permissions handling
This commit is contained in:
parent
d4eebaae64
commit
0d10196abc
|
@ -186,23 +186,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
||||||
|
|
||||||
$this->item->set_collection($collection);
|
$this->item->set_collection($collection);
|
||||||
|
|
||||||
$field = get_post_meta($collection->get_id());
|
return $this->item;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,7 +207,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$field = $this->prepare_item_for_database( [ $item, $collection_id ] );
|
$this->prepare_item_for_database( [ $item, $collection_id ] );
|
||||||
} catch (\Error $exception){
|
} catch (\Error $exception){
|
||||||
return new WP_REST_Response($exception->getMessage(), 400);
|
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()) {
|
if($this->item->validate()) {
|
||||||
$item = $this->items_repository->insert($this->item );
|
$item = $this->items_repository->insert($this->item );
|
||||||
|
|
||||||
$item_metadata = new Entities\Item_Metadata_Entity($item, $field );
|
return new WP_REST_Response($this->item->__toArray(), 201 );
|
||||||
$field_added = $this->item_metadata->insert( $item_metadata );
|
|
||||||
|
|
||||||
return new WP_REST_Response($field_added->get_item()->__toArray(), 201 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -255,7 +236,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
||||||
$collection = $this->collections_repository->fetch($request['collection_id']);
|
$collection = $this->collections_repository->fetch($request['collection_id']);
|
||||||
|
|
||||||
if ($collection instanceof Entities\Collection) {
|
if ($collection instanceof Entities\Collection) {
|
||||||
return $collection->get_items_capabilities()->edit_posts;
|
return current_user_can($collection->get_items_capabilities()->edit_posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -332,6 +332,10 @@ class Capabilities {
|
||||||
$role->add_cap($collection_items_caps->$cap);
|
$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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -39,7 +39,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
'name' => 'Agile',
|
'name' => 'Agile',
|
||||||
'description' => 'Agile methods'
|
'description' => 'Agile methods',
|
||||||
|
'status' => 'publish'
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
@ -70,7 +71,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
$response = $this->server->dispatch($request);
|
$response = $this->server->dispatch($request);
|
||||||
|
|
||||||
$this->assertEquals(200, $response->get_status());
|
$this->assertEquals(200, $response->get_status());
|
||||||
|
|
||||||
$data = $response->get_data();
|
$data = $response->get_data();
|
||||||
|
|
||||||
$first_item = $data[0];
|
$first_item = $data[0];
|
||||||
|
|
|
@ -69,8 +69,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$field = $this->tainacan_field_factory->create_field('text', '', true);
|
|
||||||
|
|
||||||
$field = $this->tainacan_entity_factory->create_entity(
|
$field = $this->tainacan_entity_factory->create_entity(
|
||||||
'field',
|
'field',
|
||||||
array(
|
array(
|
||||||
|
@ -78,7 +76,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
'description' => 'Descreve valor do campo data.',
|
'description' => 'Descreve valor do campo data.',
|
||||||
'collection' => $collection,
|
'collection' => $collection,
|
||||||
'status' => 'publish',
|
'status' => 'publish',
|
||||||
'field_type' => $field->get_primitive_type(),
|
'field_type' => 'Tainacan\Field_Types\Text',
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
@ -142,8 +140,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$field = $this->tainacan_field_factory->create_field('text', '', true);
|
|
||||||
|
|
||||||
$field = $this->tainacan_entity_factory->create_entity(
|
$field = $this->tainacan_entity_factory->create_entity(
|
||||||
'field',
|
'field',
|
||||||
array(
|
array(
|
||||||
|
@ -151,7 +147,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
||||||
'description' => 'Descreve o dado do campo data.',
|
'description' => 'Descreve o dado do campo data.',
|
||||||
'collection' => $collection,
|
'collection' => $collection,
|
||||||
'status' => 'publish',
|
'status' => 'publish',
|
||||||
'field_type' => $field->get_primitive_type(),
|
'field_type' => 'Tainacan\Field_Types\Text',
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
|
@ -50,6 +50,23 @@ class Permissions extends TAINACAN_UnitTestCase {
|
||||||
$this->assertTrue($collection->can_read());
|
$this->assertTrue($collection->can_read());
|
||||||
$this->assertFalse($collection->can_publish());
|
$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');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue