Skip to content

Commit e704f34

Browse files
Updated Dotenv::getSettings to prepend & append start/end chars to each pattern. Updated Dotenv::getSettings to construct trusted_host_patterns based on DOMAINS if
`TRUSTED_HOST_PATTERNS` environment variable is not set. Updated `SHIELD` variable to serve as the default username/password for the prompt if filled.
1 parent 7d271ee commit e704f34

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
44

55
## [Unreleased][unreleased]
66

7+
## [0.2.0] - 2022-03-14
8+
9+
### Changed
10+
11+
- *Updated `Dotenv::getSettings` to prepend & append start/end chars to each pattern.*
12+
- Updated `Dotenv::getSettings` to construct `trusted_host_patterns` based on `DOMAINS` if
13+
`TRUSTED_HOST_PATTERNS` environment variable is not set.
14+
- Updated `SHIELD` variable to serve as the default username/password for the prompt if filled.
15+
716
## [0.1.17] - 2022-01-27
817

918
### Fixed
@@ -126,7 +135,8 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
126135

127136
**Initial release!**
128137

129-
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.17...main
138+
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.0...main
139+
[0.2.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.17...0.2.0
130140
[0.1.17]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.16...0.1.17
131141
[0.1.16]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.15...0.1.16
132142
[0.1.15]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.14...0.1.15

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,24 @@ MAILGUN_URL=https://[email protected]
195195
```
196196

197197
#### SHIELD
198-
A boolean allowing the enabling/disabling of Shield module auth functionality.
198+
A string allowing the enabling/disabling of Shield module auth functionality.
199+
200+
If empty, shield will not be enabled.
201+
202+
If filled, the string will be used as username & password by default.
199203

200204
Note: _Make sure the "Enable Shield" checkbox is checked in Drupal & that config is committed._
201205

202206
##### SHIELD_USERNAME
203207
The username for Shield to require, if enabled.
204208

209+
This package will use the value of `SHIELD` as username, by default.
210+
205211
##### SHIELD_PASSWORD
206212
The password for Shield to require, if enabled.
207213

214+
This package will use the value of `SHIELD` as password, by default.
215+
208216
##### SHIELD_MESSAGE
209217
The _public_ message Shield should show in the auth prompt if enabled.
210218

@@ -236,7 +244,11 @@ so the shorter DSN can be used.
236244
Streamlined environment-dependent configuration of _one_ Solr core is supported at this time.
237245

238246
#### TRUSTED_HOST_PATTERNS
239-
A CSV list of patterns specifying [trusted hosts](https://www.drupal.org/docs/installing-drupal/trusted-host-settings#s-protecting-in-drupal-8).
247+
Optional. A CSV list of patterns specifying [trusted hosts](https://www.drupal.org/docs/installing-drupal/trusted-host-settings#s-protecting-in-drupal-8). If this
248+
variable is not set, this package will populate Drupal's `trusted_host_patterns` array
249+
based on the value of [DOMAINS](#domains).
250+
251+
Start (`^`) & end (`$`) characters are added to every pattern, by default.
240252

241253
#### Configuration Conclusion
242254
With these few Environment Variables, you will be able to configure Drupal in a streamlined

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"psr-4": { "UnleashedTech\\Drupal\\Dotenv\\": "src/" }
1414
},
1515
"extra": {
16-
"branch-alias": { "dev-main": "1.0-dev" }
16+
"branch-alias": { "dev-main": "2.0-dev" }
1717
}
1818
}

src/Dotenv.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function getConfig(): array
115115

116116
// Default to having shield enabled.
117117
if (isset($_SERVER['SHIELD'])) {
118-
$config['shield.settings']['shield_enable'] = (bool)$_SERVER['SHIELD'];
118+
$config['shield.settings']['shield_enable'] = (bool) $_SERVER['SHIELD'];
119119
} else {
120120
$config['shield.settings']['shield_enable'] = TRUE;
121121
}
@@ -174,9 +174,15 @@ public function getConfig(): array
174174
if (isset($_SERVER['SHIELD_USERNAME'])) {
175175
$config['shield.settings']['credentials']['shield']['user'] = $_SERVER['SHIELD_USERNAME'];
176176
}
177+
else {
178+
$config['shield.settings']['credentials']['shield']['user'] = $_SERVER['SHIELD'];
179+
}
177180
if (isset($_SERVER['SHIELD_PASSWORD'])) {
178181
$config['shield.settings']['credentials']['shield']['pass'] = $_SERVER['SHIELD_PASSWORD'];
179182
}
183+
else {
184+
$config['shield.settings']['credentials']['shield']['pass'] = $_SERVER['SHIELD'];
185+
}
180186
if (isset($_SERVER['SHIELD_MESSAGE'])) {
181187
$config['shield.settings']['print'] = $_SERVER['SHIELD_MESSAGE'];
182188
}
@@ -282,7 +288,14 @@ public function getSettings(): array
282288
$settings['hash_salt'] = $_SERVER['HASH_SALT'];
283289
}
284290
if (isset($_SERVER['TRUSTED_HOST_PATTERNS'])) {
285-
$settings['trusted_host_patterns'] = explode(',', $_SERVER['TRUSTED_HOST_PATTERNS']);
291+
foreach (explode(',', $_SERVER['TRUSTED_HOST_PATTERNS']) as $pattern) {
292+
$settings['trusted_host_patterns'][] = '^' . $pattern . '$';
293+
}
294+
}
295+
else {
296+
foreach ($this->getDomains() as $domain) {
297+
$settings['trusted_host_patterns'][] = '^' . str_replace('.', '\.', $domain) . '$';
298+
}
286299
}
287300

288301
switch ($envName) {
@@ -313,6 +326,16 @@ public function getSettings(): array
313326
return $settings;
314327
}
315328

329+
/**
330+
* Gets the domains for this environment.
331+
*
332+
* @return array
333+
* The domains for this environment.
334+
*/
335+
public function getDomains(): array {
336+
return \explode(',', $_SERVER['DOMAINS'] ?? 'default.example');
337+
}
338+
316339
/**
317340
* Gets the Drupal-multi-site $sites array, based on environment variables.
318341
*
@@ -321,7 +344,7 @@ public function getSettings(): array
321344
*/
322345
public function getSites(): array
323346
{
324-
$domains = \explode(',', $_SERVER['DOMAINS'] ?? 'default.example');
347+
$domains = $this->getDomains();
325348
$siteNames = \explode(',', $_SERVER['SITES'] ?? 'default');
326349
$sites = [];
327350
foreach ($siteNames as $siteName) {

0 commit comments

Comments
 (0)