Using Drush to manage users

When you need to add an account to Drupal, you will visit the People page and manually add a new user. Drush provides the complete user management for Drupal, from creation to role assignment, password recovery, and deletion. This workflow allows you to create users easily and provides them with a login without having to enter your Drupal site.

In this recipe, we will create a staff role with a staffmember user and log in as that user through Drush.

How to do it…

  1. Use the role-create command to create a new role labeled staff:
    $ drush role-create staff
    Created "staff"
    
  2. Use the role-lists command to verify that the role was created in Drupal:
    $ drush role-list
     ID             Role Label         
     anonymous      Anonymous user     
     authenticated  Authenticated user 
     administrator  Administrator      
     staff          Staff
    
  3. The user-create command will create our user:
    $ drush user-create staffmember
     User ID       :  2             
     User name     :  staffmember   
     User roles    :  authenticated 
     User status   :  1 
    
  4. In order to add the role, we need to use the user-add-role command:
    $ drush user-add-role staff staffmember
    Added role staff role to staffmember
    
  5. We will now log in as the staffmember user using the user-login command:
    $ drush user-login staffmember --uri=http://example.com
    http://example.com/user/reset/2/1452810532/Ia1nJvbr2UQ3Pi_QnmITlVgcCWzDtnKmHxf-I2eAqPE
    
  6. Provide the uri option to ensure that a correct URL points to a one time login link.
  7. Copy the link and paste it in your browser to log in as that user.
    How to do it…

How it works…

When you reset a password in Drupal, a special one-time login link is generated. The login link is based on a generated hash. The Drush command validates the given user, which exists in the Drupal site and then passes it to the user_pass_reset_url function from the User module.

The URL is made up of the user's ID, the timestamp when the link was generated, and a hash based on the user's last login time, link generation, and e-mail. When the link is loaded, this hash is rebuilt and verified. For example, if the user has logged in since the time it was generated, the link will become invalid.

When used on a machine that has a web browser installed, Drush will make an attempt to launch the link in a web browser for you. The browser option allows you to specify which browser should be launched. Additionally, you can use no-browser to prevent one from being launched.

There's more…

The command line offers the ability to simplify user management and user administration. Next, we will explore more on this topic in detail.

Advanced user-login use cases

The user-login command is a useful tool that allows some advanced use cases. For instance, you can append a path after the username and be launched to that path. You can pass a UID or e-mail instead of a username in order to log in as a user.

You can use the user-login to secure your admin user account. In Drupal, the user with the identifier of 1 is treated as the root, and can bypass all permissions. Many times, this is the default maintenance account used to work on the Drupal site. Instead of logging in manually, you can set the account to a very robust passphrase and use the user-login command when you need to access your site. With this, the only users who should be able to log in as the administrator account are those with access to run Drush commands on the website.

Using Drupal Console

Console also provided commands to interact with users. While they do not allow the creation of users or roles, they provide basic user management.

The user:login:url command will generate a one time login link for the specified user ID . This uses the same methods as the Drush command:

$ drupal user:login:url 2

The user:password:reset command allows you to reset a user's password to the new provided password. You can provide the user ID and new password as arguments, but if missing, the values will be prompted for interactively:

$ drupal user:password:reset 2 newpassword

The create:users command provides an interactive way to generate bulk users, which are useful to debug. However, it cannot make individual users with specific passwords like Drush.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset