We need user permission control on promotion in our company's system. @skyred and @bojanz has reached an agreement to add author system on promotion several days ago.

Proposed resolution:

  1. Add 'uid' base field on promotion entity type
  2. Make promotion implement EntityOwnerInterface to realize user permission control on it.

Comments

caseylau created an issue. See original summary.

lawxen’s picture

Status: Active » Needs review
StatusFileSize
new4.81 KB
lawxen’s picture

StatusFileSize
new6.1 KB
new1.19 KB

Add hook_update for uid field

murz’s picture

Confirm this suggestion, very needed! Patch seems broken with current version of Commerce, I'll try to update it for current version. Or maybe @caseylau - you already have updated version of patch?

murz’s picture

As workaround without patching module core, I solve this problem via adding uid field using hook:

use Drupal\Core\Field\BaseFieldDefinition;

function mymodule_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  $fields = array();

  if ($entity_type->id() === 'commerce_promotion') {
    $fields['owner'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel('Owner')
      ->setSettings(array(
        'target_type' => 'user',
        'default_value' => 0,
      ))
      ->setDisplayOptions('form', [
        'type' => 'dynamic_entity_reference_default',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('form', TRUE);
  }

  return $fields;
}
jsacksick’s picture

Status: Needs review » Needs work

This needs tests and we should now be using the EntiyOwnerTrait introduced by #3073942: Commerce entities implementing EntityOwnerInterface should always return a UserInterface object.

jsacksick’s picture

Status: Needs work » Needs review
StatusFileSize
new6.68 KB

This should do the trick.

jsacksick’s picture

StatusFileSize
new6.93 KB

We were missing the following:

  $entity_type = $definition_update_manager->getEntityType('commerce_promotion');
  $keys = $entity_type->getKeys();
  $keys['owner'] = 'uid';
  $entity_type->set('entity_keys', $keys);
  $definition_update_manager->updateEntityType($entity_type);
jsacksick’s picture

The initial default value for the field is set to the first admin user.

Status: Needs review » Needs work

The last submitted patch, 8: 2965729-8.patch, failed testing. View results

amateescu’s picture

+++ b/modules/promotion/commerce_promotion.install
@@ -216,3 +217,47 @@ function commerce_promotion_update_8208() {
+  $admin_roles = \Drupal::entityTypeManager()
...
+    $query = \Drupal::entityTypeManager()

These queries need an accessCheck(FALSE).

jsacksick’s picture

Status: Needs work » Needs review
StatusFileSize
new8.92 KB

Not sure what to do about the existing "update commerce_promotion" which no longer exists... Should we grant the "update any commerce_promotion" to roles having the "update commerce_promotion" permission?

jsacksick’s picture

StatusFileSize
new9.24 KB
new864 bytes

Added code to grant the "update any commerce_promotion" permission to all roles having the "update commerce_promotion" permission.

jsacksick’s picture

Status: Needs review » Fixed

  • jsacksick committed d733619 on 8.x-2.x
    Issue #2965729 by jsacksick, lawxen, amateescu: Add author on promotion...

Status: Fixed » Closed (fixed)

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