Skip to content

Commit 7a71b7a

Browse files
Bohdan Korablovshiftedreality
authored andcommitted
MAGECLOUD-1020: Enterprise Cloud Edition Sets Up an Admin User with A Default Password (#41)
1 parent be0a569 commit 7a71b7a

File tree

17 files changed

+943
-270
lines changed

17 files changed

+943
-270
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"require-dev": {
2020
"phpunit/phpunit": "~6.2.0",
2121
"squizlabs/php_codesniffer": " ^3.0",
22-
"doctrine/instantiator": "1.0.5",
2322
"phpmd/phpmd": "@stable",
2423
"php-mock/php-mock-phpunit": "^2.0",
2524
"phpunit/php-code-coverage": "^5.2"

src/App/Container.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ public function __construct(string $root, array $config)
103103
->give(function () {
104104
return $this->makeWith(ProcessPool::class, [
105105
'processes' => [
106+
$this->make(DeployProcess\InstallUpdate\Install\EmailChecker::class),
106107
$this->make(DeployProcess\InstallUpdate\Install\Setup::class),
107108
$this->make(DeployProcess\InstallUpdate\Install\SecureAdmin::class),
108109
$this->make(DeployProcess\InstallUpdate\ConfigUpdate::class),
110+
$this->make(DeployProcess\InstallUpdate\Install\ResetPassword::class),
109111
],
110112
]);
111113
});
@@ -115,7 +117,9 @@ public function __construct(string $root, array $config)
115117
return $this->makeWith(ProcessPool::class, [
116118
'processes' => [
117119
$this->make(DeployProcess\InstallUpdate\ConfigUpdate::class),
120+
$this->make(DeployProcess\InstallUpdate\Update\SetAdminUrl::class),
118121
$this->make(DeployProcess\InstallUpdate\Update\Setup::class),
122+
$this->make(DeployProcess\InstallUpdate\Update\AdminCredentials::class),
119123
$this->make(DeployProcess\InstallUpdate\Update\ClearCache::class),
120124
],
121125
]);
@@ -128,10 +132,8 @@ public function __construct(string $root, array $config)
128132
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\DbConnection::class),
129133
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\Amqp::class),
130134
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\Redis::class),
131-
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\AdminCredentials::class),
132135
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\SearchEngine::class),
133136
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\Urls::class),
134-
$this->make(DeployProcess\InstallUpdate\ConfigUpdate\EnvConfiguration::class),
135137
],
136138
]);
137139
});

src/Config/Environment.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Environment
2727
const VAL_ENABLED = 'enabled';
2828
const VAL_DISABLED = 'disabled';
2929

30+
const DEFAULT_ADMIN_URL = 'admin';
31+
const DEFAULT_ADMIN_NAME = 'admin';
32+
const DEFAULT_ADMIN_FIRSTNAME = 'Admin';
33+
const DEFAULT_ADMIN_LASTNAME = 'Username';
34+
3035
/**
3136
* Let's keep variable names same for both phases.
3237
*/
@@ -346,47 +351,47 @@ public function getDbPassword(): string
346351
*/
347352
public function getAdminUsername(): string
348353
{
349-
return $this->getVariables()['ADMIN_USERNAME'] ?? 'admin';
354+
return $this->getVariables()['ADMIN_USERNAME'] ?? '';
350355
}
351356

352357
/**
353358
* @return string
354359
*/
355360
public function getAdminFirstname(): string
356361
{
357-
return $this->getVariables()['ADMIN_FIRSTNAME'] ?? 'John';
362+
return $this->getVariables()['ADMIN_FIRSTNAME'] ?? '';
358363
}
359364

360365
/**
361366
* @return string
362367
*/
363368
public function getAdminLastname(): string
364369
{
365-
return $this->getVariables()['ADMIN_LASTNAME'] ?? 'Doe';
370+
return $this->getVariables()['ADMIN_LASTNAME'] ?? '';
366371
}
367372

368373
/**
369374
* @return string
370375
*/
371376
public function getAdminEmail(): string
372377
{
373-
return $this->getVariables()['ADMIN_EMAIL'] ?? 'john@example.com';
378+
return $this->getVariables()['ADMIN_EMAIL'] ?? '';
374379
}
375380

376381
/**
377382
* @return string
378383
*/
379384
public function getAdminPassword(): string
380385
{
381-
return $this->getVariables()['ADMIN_PASSWORD'] ?? 'admin12';
386+
return $this->getVariables()['ADMIN_PASSWORD'] ?? '';
382387
}
383388

384389
/**
385390
* @return string
386391
*/
387392
public function getAdminUrl(): string
388393
{
389-
return $this->getVariables()['ADMIN_URL'] ?? 'admin';
394+
return $this->getVariables()['ADMIN_URL'] ?? '';
390395
}
391396

392397
/**

src/Process/Deploy/InstallUpdate/ConfigUpdate/AdminCredentials.php

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Process\Deploy\InstallUpdate\Install;
7+
8+
use Magento\MagentoCloud\Process\ProcessInterface;
9+
use Magento\MagentoCloud\Config\Environment;
10+
11+
/**
12+
* Validates presence of ADMIN_EMAIL environment variable.
13+
*
14+
* {@inheritdoc}
15+
*/
16+
class EmailChecker implements ProcessInterface
17+
{
18+
/**
19+
* @var Environment
20+
*/
21+
private $environment;
22+
23+
/**
24+
* @param Environment $environment
25+
*/
26+
public function __construct(Environment $environment)
27+
{
28+
$this->environment = $environment;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function execute()
35+
{
36+
if (!$this->environment->getAdminEmail()) {
37+
$message = 'ADMIN_EMAIL not set during install!'
38+
. ' We need this variable set to send the password reset email.'
39+
. ' Please set ADMIN_EMAIL and retry deploy.';
40+
41+
throw new \RuntimeException($message);
42+
}
43+
}
44+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Process\Deploy\InstallUpdate\Install;
7+
8+
use Magento\MagentoCloud\Config\Environment;
9+
use Magento\MagentoCloud\Process\ProcessInterface;
10+
use Psr\Log\LoggerInterface;
11+
use Magento\MagentoCloud\Util\UrlManager;
12+
use Magento\MagentoCloud\Filesystem\Driver\File;
13+
use Magento\MagentoCloud\Filesystem\DirectoryList;
14+
15+
/**
16+
* Sends email with link to reset password.
17+
*
18+
* {@inheritdoc}
19+
*/
20+
class ResetPassword implements ProcessInterface
21+
{
22+
/**
23+
* @var File
24+
*/
25+
private $file;
26+
27+
/**
28+
* @var DirectoryList
29+
*/
30+
private $directoryList;
31+
32+
/**
33+
* @var LoggerInterface
34+
*/
35+
private $logger;
36+
37+
/**
38+
* @var Environment
39+
*/
40+
private $environment;
41+
42+
/**
43+
* @var UrlManager
44+
*/
45+
private $urlManager;
46+
47+
/**
48+
* @param LoggerInterface $logger
49+
* @param Environment $environment
50+
* @param UrlManager $urlManager
51+
* @param File $file
52+
* @param DirectoryList $directoryList
53+
*/
54+
public function __construct(
55+
LoggerInterface $logger,
56+
Environment $environment,
57+
UrlManager $urlManager,
58+
File $file,
59+
DirectoryList $directoryList
60+
) {
61+
$this->logger = $logger;
62+
$this->environment = $environment;
63+
$this->urlManager = $urlManager;
64+
$this->file = $file;
65+
$this->directoryList = $directoryList;
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function execute()
72+
{
73+
if ($this->environment->getAdminPassword()) {
74+
return;
75+
}
76+
77+
$credentialsFile = $this->directoryList->getMagentoRoot() . '/var/credentials_email.txt';
78+
$urls = $this->urlManager->getUrls();
79+
$adminUrl = $urls['secure'][''] . ($this->environment->getAdminUrl() ?: Environment::DEFAULT_ADMIN_URL);
80+
$adminEmail = $this->environment->getAdminEmail();
81+
$adminUsername = $this->environment->getAdminUsername() ?: Environment::DEFAULT_ADMIN_NAME;
82+
83+
$emailContent = 'Welcome to Magento Commerce (Cloud)!' . PHP_EOL
84+
. 'To properly log into your provisioned Magento installation Admin panel, you need to change'
85+
. ' your Admin password. To update your password, click this link to access the Admin Panel:'
86+
. ' ' . $adminUrl . ' When the page opens, click the "Forgot your password" link. You should receive'
87+
. ' a password update email at ' . $adminEmail . ' Just in case, check your spam box if you don\'t see'
88+
. ' the email immediately.' . PHP_EOL
89+
. 'After the password is updated, you can login with the username ' . $adminUsername
90+
. ' and the new password.' . PHP_EOL
91+
. 'Need help? Please see'
92+
. ' http://devdocs.magento.com/guides/v2.2/cloud/onboarding/onboarding-tasks.html' . PHP_EOL
93+
. 'Thank you,' . PHP_EOL
94+
. 'Magento Commerce (Cloud)' . PHP_EOL;
95+
96+
$this->logger->info('Emailing admin URL to admin user ' . $adminUsername . ' at ' . $adminEmail);
97+
mail(
98+
$adminEmail,
99+
'Magento Commerce Cloud - Admin URL',
100+
$emailContent,
101+
'From: Magento Cloud <accounts@magento.cloud>'
102+
);
103+
$this->logger->info('Saving email with admin URL: ' . $credentialsFile);
104+
$this->file->filePutContents($credentialsFile, $emailContent);
105+
}
106+
}

0 commit comments

Comments
 (0)