backdrop-contrib / backdrop-drush-extension

A set of commands and boot class for Drush and Backdrop CMS.
GNU General Public License v2.0
13 stars 18 forks source link

Support drush user-create (ucrt) #169

Closed jackaponte closed 5 years ago

jackaponte commented 5 years ago

The drush user-create aka drush ucrt command is not yet supported for Backdrop; filing this feature request as it would greatly benefit my coop in our development work!

PR by @jackaponte : https://github.com/backdrop-contrib/drush/pull/176

jackaponte commented 5 years ago

I'm going to try to work on this, though I've not worked on Drush before at all. Any hints are welcome, otherwise I'll be looking at what's been done to date and working from there.

serundeputy commented 5 years ago

Sounds good; thanks @jackaponte

Yeah; basically try adding it to $safe_commands in backdrop.drush.inc and if that does not work look at the drush/drush implementation and port it over to a backdrop drush command.

Let me know if you get stuck on anything!

thanks again!

jackaponte commented 5 years ago

Thanks for the support, @serundeputy; I'll let you know how it goes!

jackaponte commented 5 years ago

Adding user-create to $safe_commands inbackdrop.drush.inc` didn't work:

drush user-create testuser
Unable to load class Drush\User\User                                                                                                                                               [error]
Error: Call to a member function load_by_name() on boolean in drush_user_create_validate() (line 330 of /opt/drush/8/drush/drush/commands/user/user.drush.inc).
Drush command terminated abnormally due to an unrecoverable error.`

Next, figuring out what "look at the drush/drush implementation" means exactly, then doing it. Might need to do some learning about working with Drush commands more generally if I find myself too stuck!

serundeputy commented 5 years ago

Thanks for digging into this!

The command callback for user-create is drush_user_create (https://github.com/drush-ops/drush/blob/8.x/commands/user/user.drush.inc#L303).

Looks like the first thing that function does is instantiate an instance of the User class with drush_get_user_class().

I've not studied the class in question, but seems it did not even load the class. So we may need to roll our own drush_user_create that way we can use a Backdrop User class instead of the Drupal one.

so make a new file in backdrop/commands/user/ and name it backdrop_user_create.drush.inc you can start by copying one of the existing commands in /user/. the top function in those files is basically like a hook_menu but instead of a page callback you have a command callback. the command callback is where the processing of logic of doing the thing happens. The rest is just validation and helper functions.

If you need/want more clarification let me know!

jackaponte commented 5 years ago

Thanks again for the continued guidance here, @serundeputy!

I followed your advice, creating that new file as suggested, but got stuck fairly quickly at that drush_get_user_class() point. I don't know where to go from there to switch to using the Backdrop User class (this, right?) instead of the Drupal class (which I can't find docs on looking here).

I see that drush_user_get_class is already in use on line 283 of backdrop_core.drush.inc:

        if ($phase > DRUSH_BOOTSTRAP_BACKDROP_CONFIGURATION) {
          $status_table['install-profile'] = $boot_object->get_profile();
          if ($phase > DRUSH_BOOTSTRAP_BACKDROP_FULL) {
            $status_table['bootstrap'] = dt('Successful');
            if ($phase == DRUSH_BOOTSTRAP_BACKDROP_LOGIN) {
              $status_table['user'] = drush_user_get_class()->getCurrentUserAsSingle()->getUsername();
            }
          }
        }

So I assume it's working there and thereby working properly in some circumstances in Backdrop, just not how it's being used in the user-create command?

jackaponte commented 5 years ago

Forgot to mention, you can check out my work to date here on GitHub: https://github.com/jackaponte/drush/tree/ucrt-take-3

serundeputy commented 5 years ago

@jackaponte nice work!

Looks like we don't need that class as at the bootstrap level of this command we have access to the full backdrop api.

Give this a test when you get a chance: https://github.com/backdrop-contrib/drush/pull/176

jackaponte commented 5 years ago

Yay, thanks @serundeputy! I'm glad this helped get this work going.

Looks like we don't need that class as at the bootstrap level of this command we have access to the full backdrop api.

Ah, I see; that's good to know.

Give this a test when you get a chance: #176

I've tested it; new users are created properly! Though comparing results of the command in Backdrop and Drupal 7 shows there's still work to be done to get all the options working properly; I'll open a separate issue for that, though. :)

I think this is ready to be merged!

serundeputy commented 5 years ago

Thanks for testing @jackaponte !

I've added some output to be more similar to the drupal output:

─ $ ∴ lando.dev drush user-create gff016 --mail=gff016@gff.gov --password=gff

    Success: user gff016 created.

        User ID:    19
        User name:  gff016
        User mail:  gff016@gff.gov
        User roles: authenticated
        User status:    1

merging into 1.x-0.x will ship with 0.3.0.

jackaponte commented 5 years ago

Brilliant, thank you @serundeputy!