Now that #2857589: Remove the run-as option in drush commands has been fixed and we agree that the drush --user option should be used to run as a specific user, I would like to ask if it makes sense to run the commands as user 1 by default.

IMHO this would allow to import and export the content with less surprises on the user side (e.g. missing fields).

Thanks,
Antonio

Comments

ao2 created an issue. See original summary.

andypost’s picture

Assigned: Unassigned » berdir

Hm, probably it makes sense, the win is that no access restrictions will apply on export

@Berdir any thoughts?

kalpaitch’s picture

IMO absolutely. Yes. I have found that in order to implement #2698425: Do not re-import existing entities this is needed because in order to find out whether an entity exists we use need to look them up as user 1. If we look up as any other user with limited access and there are nodes that have access restrictions then they won't be found and we'll get a db error.

Next Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry '2c24a28a-fdb4-4c98-8118-7c845e6b0f3d' for key 'node_field__uuid__value': INSERT INTO
{node} (vid, type, uuid, langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1,
:db_insert_placeholder_2, :db_insert_placeholder_3); Array
(
    [:db_insert_placeholder_0] => 
    [:db_insert_placeholder_1] => page
    [:db_insert_placeholder_2] => 2c24a28a-fdb4-4c98-8118-7c845e6b0f3d
    [:db_insert_placeholder_3] => en
)
 in /vagrant/repos/subscriptions/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:770
moshe weitzman’s picture

I have been considering removing --user from drush since I thought this sort os masquerading is not needed anymore. Isn't there a lower level API that default content can use to bypass access control?

larowlan’s picture

Yeah, we can use the account switcher service in core.

mikran’s picture

Status: Active » Needs review
StatusFileSize
new2.35 KB

here is a patch that uses the account switcher service for the task

andypost’s picture

geerlingguy’s picture

Hmm... I tried using the patch in #6—applied it to default_content module successfully, did a drush cr, then ran:

drush dcer user 2 --folder=modules/custom/mysite_default_content/content/

But the exported user JSON still doesn't contain status, email, or roles.

geerlingguy’s picture

Status: Needs review » Needs work

Found that the changes in the patch only apply to the Drush < 9 versions, and I'm running Drush 9. Testing another version of the patch now which updates the Drush services and DefaultContentCommands.php as well.

geerlingguy’s picture

Assigned: berdir » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.49 KB

Attached patch also applies to Drush 9 commands. Seems to work well in my case—now I'm seeing roles, email, etc. (all the things available to user 1).

andypost’s picture

Btw \Drupal\default_content\Importer::importContent() already does switch to user 1

ao2’s picture

OK, so the feature itself is not controversial at all.

The question is: where should the the switch be performed? In Importer/Exporter or when the commands are called in Drupal\default_content\Commands.

And ideally the code should look similar for the Importer and the Exporter cases: same import mechanism and same names for variables.

I am not doing much Drupal these days so, even though I am the original reporter, I am afraid I could not work on these last details myself.

andypost’s picture

I think better to make it in exporter because it's hard to explain to module's users that they must user `--user` drush's argument (nobody read docs)
Also it will be minimize code base if someone will provide drupal console integration

moshe weitzman’s picture

My understanding is that Command would not consult a --user option because the module doesn't work without a non-priveleged user. It would simply switch to uid=1.

FWIW I dont especially care where the switch happens.

ao2’s picture

@andypost, just to clarify, when I was referring to the commands I was not suggesting that the user would have to pass `--user` to drush explicitly, as the very purpose of this issue is to avoid that.

I was referring to the current approach used in #10 to switch user in src/Commands/DefaultContentCommands.php opposed to the approach already used at import time, which is to switch the user in src/Importer.php.

I think that we'd better add the switch to src/Exporter.php.

@geerlingguy would you be up for it?

geerlingguy’s picture

Right now I don't have any cycles to devote to working on this patch, with it being end of year, holiday season and all that :(

amateescu’s picture

Title: Run drush commands as user 1 by default » Export content as user 1
Category: Plan » Bug report
StatusFileSize
new4.57 KB

Here's a patch that switches the user in Exporter rather than the drush commands themselves.

Status: Needs review » Needs work

The last submitted patch, 17: 2861591-17.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new5.33 KB
new784 bytes

Fixed the tests.

andypost’s picture

Status: Needs review » Needs work
+++ b/src/Exporter.php
@@ -127,6 +142,9 @@ class Exporter implements ExporterInterface {
   public function exportContentWithReferences($entity_type_id, $entity_id) {
+    $root_user = $this->entityTypeManager->getStorage('user')->load(1);
+    $this->accountSwitcher->switchTo($root_user);

not sure it is the best place, suppose better to add to \Drupal\default_content\Commands\DefaultContentCommands because drush 9 no longer have --user argument

This way we will keep compatibility with drush8 and will not affect all export methods

amateescu’s picture

Status: Needs work » Needs review

I think it's best to put this directly in the exporter class precisely because it's agnostic to the Drush version :)

larowlan’s picture

Status: Needs review » Needs work
+++ b/src/Exporter.php
@@ -127,6 +142,9 @@ class Exporter implements ExporterInterface {
+    $root_user = $this->entityTypeManager->getStorage('user')->load(1);
+    $this->accountSwitcher->switchTo($root_user);

we throw two execptions here, we should switch back before throwing for safety sake

In addition, I think we should check we're running in a cli context before we switch, in case someone has built UI for this - we don't want there to be an access bypass

other than that, looks good to me - thanks

amateescu’s picture

Title: Export content as user 1 » Export content as user 1 when running in a CLI context
Status: Needs work » Needs review
StatusFileSize
new6.9 KB
new3.79 KB

@larowlan, good points :)

we throw two execptions here, we should switch back before throwing for safety sake

Since the problematic part is the serialization process, not entity loading, I've moved the account switching after those exceptions are thrown. Also cleaned up the code a bit and added the exceptions to exportContent() as well.

In addition, I think we should check we're running in a cli context before we switch, in case someone has built UI for this - we don't want there to be an access bypass

Done :)

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

  • larowlan committed 73cf347 on 8.x-1.x authored by amateescu
    Issue #2861591 by amateescu, geerlingguy, mikran, andypost, ao2,...

Status: Fixed » Closed (fixed)

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