Problem/Motivation

Please refer the problem/motivation section of #2737719: EntityResource: Provide comprehensive test coverage: for every entity type, every format, every method

Proposed resolution

Write EntityResourceTestBase subclass for the BaseFieldOverride entity.
Add an access control handler to provide the same access control as FieldConfig entities. Unlike them base field overrides do not need to deal with locked field storage entities because field storage is provided by the base fields and they are always there and base field overrides should be deletable.

Remaining tasks

References

1. Follow-up of #2737719: EntityResource: Provide comprehensive test coverage: for every entity type, every format, every method
2. Subtask of #2824572: Write EntityResourceTestBase subclasses for every other entity type.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

naveenvalecha created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

shadcn’s picture

Version: 8.3.x-dev » 8.4.x-dev
Assigned: Unassigned » shadcn
Status: Active » Needs review
FileSize
10.69 KB

This patch also adds an access handler \Drupal\Core\Field\BaseFieldOverrideAccessControlHandler to \Drupal\Core\Field\Entity\BaseFieldOverride. Let's see what fails.

Status: Needs review » Needs work

The last submitted patch, 3: entityresource_provide-2843767-3.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
tstoeckler’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Novice
+++ b/core/lib/Drupal/Core/Field/BaseFieldOverrideAccessControlHandler.php
@@ -0,0 +1,22 @@
+  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
+    return AccessResult::allowedIfHasPermission($account, 'administer ' . $entity->getTargetEntityTypeId() . ' fields');
+  }

This is problematic because it doesn't call the parent implementation so it misses e.g. the

if ($operation == 'delete' && $entity->isNew()) {
      return AccessResult::forbidden()->addCacheableDependency($entity);
    }

part. I think it should do $parent_access->orIf(...) or something like that.

Wim Leers’s picture

First: thanks for your review — I'm very very very glad an Entity API maintainer is reviewing this :)


I was wondering what you meant by "parent". I'm assuming you're referring to the FieldConfig config entity type now. Because:

class BaseFieldOverride extends FieldConfigBase {

But … class FieldConfig extends FieldConfigBase implements FieldConfigInterface {.

IOW: BaseFieldOverride is not a child of FieldConfig, it's a sibling.

Doesn't that mean that we should reuse \Drupal\field\FieldConfigAccessControlHandler here?

tstoeckler’s picture

Oops, sorry for being so brief. No I actually meant parent as in

parent::checkAccess();

I.e. the default implementation in \Drupal\Core\Entity\EntityAccessControlHandler::checkAccess(). That for example includes a check that you are never allowed to delete an entity if it is new. That is something that we lose by overriding the method without calling the parent.

Wim Leers’s picture

Thanks for clarifying :)

But my question still stands though: BaseFieldOverride could reuse \Drupal\field\FieldConfigAccessControlHandler, and IMHO/AFAICT it should. What do you think?

tstoeckler’s picture

Very interesting idea. I checked and that's actually not possible, though. Because the associated field storage definitions of BaseFIeldOverride entities are BaseFieldDefinition objects and not FieldStorageConfig's, so they do not have isLocked().

Wim Leers’s picture

Ahhh, right :) Thanks for checking!

@arshadcn: So then please change the patch according to @tstoeckler's guidance in #8! :)

@tstoeckler++

shadcn’s picture

Status: Needs work » Needs review
FileSize
10.77 KB
781 bytes

@tstoeckler do you mean something like this?

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me! I guess in theory I could now complain about a missing kernel test for the added base field access control handler, but since it's literally two lines of code and the whole point of this is really extensive Rest test coverage, I hope we can sneak this by the committers without it. On the other hand @arshadcn if you would like to gain some extra credit ;-), feel free to write a kernel test for the new access handler.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: entityresource_provide-2843767-12.patch, failed testing.

himanshu-dixit’s picture

Status: Needs work » Reviewed & tested by the community

It looks like it was a random failure. Returning RTBC status

Wim Leers’s picture

#13: I'd strongly -1 a KernelTestBase test. We'd want a unit test for this.

And given that 99% of entity access control handlers don't have explicit test coverage, and the test coverage added here is pretty explicit, I think this is still a net step forward :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: entityresource_provide-2843767-12.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: entityresource_provide-2843767-12.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community

Without a doubt this is a Drupal CI infra fail:

00:07:02.781 Build was aborted
00:07:02.781 ERROR: Step ‘Archive the artifacts’ failed: no workspace for drupal_patches #10312
00:07:02.782 Checking console output
00:07:02.904 ERROR: Step ‘Publish JUnit test result report’ failed: no workspace for drupal_patches #10312
00:07:02.922 Finished: ABORTED
alexpott’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed 5dbe710 and pushed to 8.4.x. Thanks!

Given the new access control handler I think we need to discuss this before backporting to 8.3.x

diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
index d3ed8bd..1f4b03a 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
@@ -3,8 +3,6 @@
 namespace Drupal\Tests\rest\Functional\EntityResource\BaseFieldOverride;
 
 use Drupal\Core\Field\Entity\BaseFieldOverride;
-use Drupal\field\Entity\FieldConfig;
-use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\node\Entity\NodeType;
 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
 

Unused uses fixed on commit.

alexpott’s picture

Version: 8.3.x-dev » 8.4.x-dev
Status: Patch (to be ported) » Needs work

Discussed with @xjm. We agreed that there is an issue with issue scope here. We need a better issue title and issue summary because this is introducing non test changes and fixing missing things. Like the lack of a access control handler for basfieldoverride - is that a bug, security hole (I don;'t think so because I don't think that by enabling rest on basfieldoverride entities you can create them in HEAD) or a task because we adding missing stuff?

Wim Leers’s picture

Title: EntityResource: Provide comprehensive test coverage for BaseFieldOverride entity » [PP-1] EntityResource: Provide comprehensive test coverage for BaseFieldOverride entity + add missing access control handler
Status: Needs work » Postponed

a task because we adding missing stuff?

This.

It'a a task, because many (most) config entity types do not specify an access control handler, nor an admin_permission.

See #2870018-6: Should the 'access content' permission be used to control access to viewing configuration entities via REST for more detail.


This is blocked on #2870018: Should the 'access content' permission be used to control access to viewing configuration entities via REST reaching consensus.

Wim Leers’s picture

Title: [PP-1] EntityResource: Provide comprehensive test coverage for BaseFieldOverride entity + add missing access control handler » EntityResource: Provide comprehensive test coverage for BaseFieldOverride entity + add missing access control handler
Status: Postponed » Reviewed & tested by the community

Consensus was achieved! Quoting #2870018-35: Should the 'access content' permission be used to control access to viewing configuration entities via REST:

  1. config entities that do not yet have an access control handler nor an admin_permission (RdfMapping, Tour): add an admin_permission.
  2. update all access control handlers for entity types that currently grant access without requiring any permission (View, Menu, File, DateFormat): change them to require the entity type's admin_permission or the entity-type specific "view" permission (yet to be added), so for example: AccessResult::allowedIfHasPermissions($account, ['view menu', 'administer menu'], 'OR').

However, for this particular case (\Drupal\Core\Field\Entity\BaseFieldOverride), an admin_permission does not make sense, just like it does not make sense for \Drupal\field\Entity\FieldStorageConfig (a semantical sibling). So what we do instead, is duplicate the exact same approach as \Drupal\field\FieldStorageConfigAccessControlHandler:

return $access->orIf(AccessResult::allowedIfHasPermission($account, 'administer ' . $entity->getTargetEntityTypeId() . ' fields'));

This should be considered the equivalent of admin_permission for this config entity type. Therefore the latest patch already is actually implementing the consensus from #2870018: Should the 'access content' permission be used to control access to viewing configuration entities via REST.

alexpott’s picture

Category: Task » Bug report
Issue summary: View changes

Changing to a bug because this is a fixing an access control bug in base field overrides.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 2623bb6 to 8.4.x and 6695bf0 to 8.3.x. Thanks!

diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
index d3ed8bd..1f4b03a 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BaseFieldOverride/BaseFieldOverrideResourceTestBase.php
@@ -3,8 +3,6 @@
 namespace Drupal\Tests\rest\Functional\EntityResource\BaseFieldOverride;
 
 use Drupal\Core\Field\Entity\BaseFieldOverride;
-use Drupal\field\Entity\FieldConfig;
-use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\node\Entity\NodeType;
 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
 

Remove unused uses.

  • alexpott committed 6695bf0 on 8.3.x
    Issue #2843767 by arshadcn, Wim Leers, tstoeckler: EntityResource:...

  • alexpott committed b9c288b on 8.4.x
    Issue #2843767 by arshadcn, Wim Leers, tstoeckler: EntityResource:...
Wim Leers’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.