8aa29bea7f22275912085cb627e2e0d2995861d7
[yaffs-website] / vendor / drush / drush / src / Commands / core / LoginCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Drupal\user\Entity\User;
5 use Drush\Commands\DrushCommands;
6 use Drush\Drush;
7 use Drush\Exec\ExecTrait;
8 use Drush\SiteAlias\SiteAliasManagerAwareInterface;
9 use Drush\SiteAlias\SiteAliasManagerAwareTrait;
10
11 class LoginCommands extends DrushCommands implements SiteAliasManagerAwareInterface
12 {
13
14     use SiteAliasManagerAwareTrait;
15     use ExecTrait;
16
17     /**
18      * Display a one time login link for user ID 1, or another user.
19      *
20      * @command user:login
21      *
22      * @param string $path Optional path to redirect to after logging in.
23      * @option name A user name to log in as. If not provided, defaults to uid=1.
24      * @option browser Optional value denotes which browser to use (defaults to operating system default). Use --no-browser to suppress opening a browser.
25      * @option redirect-port A custom port for redirecting to (e.g., when running within a Vagrant environment)
26      * @bootstrap none
27      * @handle-remote-commands
28      * @aliases uli,user-login
29      * @usage drush user:login
30      *   Open default web browser and browse to homepage, logged in as uid=1.
31      * @usage drush user:login --name=ryan node/add/blog
32      *   Open default web browser (if configured or detected) for a one-time login link for username ryan that redirects to node/add/blog.
33      * @usage drush user:login --browser=firefox --mail=drush@example.org
34      *   Open firefox web browser, and login as the user with the e-mail address drush@example.org.
35      */
36     public function login($path = '', $options = ['name' => '1', 'browser' => true, 'redirect-port' => self::REQ])
37     {
38
39         // Redispatch if called against a remote-host so a browser is started on the
40         // the *local* machine.
41         $aliasRecord = $this->siteAliasManager()->getSelf();
42         if ($aliasRecord->isRemote()) {
43             $return = drush_invoke_process($aliasRecord, 'user-login', [$path], Drush::redispatchOptions(), ['integrate' => false]);
44             if ($return['error_status']) {
45                 throw new \Exception('Unable to execute user login.');
46             } else {
47                 $link = is_string($return['object']) ?: current($return['object']);
48             }
49         } else {
50             if (!Drush::bootstrapManager()->doBootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
51                 throw new \Exception(dt('Unable to bootstrap Drupal.'));
52             }
53
54             if ($options['name'] == 1) {
55                 $account = User::load(1);
56             } elseif (!$account = user_load_by_name($options['name'])) {
57                 throw new \Exception(dt('Unable to load user: !user', ['!user' => $options['name']]));
58             }
59             $link = user_pass_reset_url($account). '/login';
60             if ($path) {
61                 $link .= '?destination=' . $path;
62             }
63         }
64         $port = $options['redirect-port'];
65         $this->startBrowser($link, false, $port, $options['browser']);
66         // Use an array for backwards compat.
67         drush_backend_set_result([$link]);
68         return $link;
69     }
70 }