Skip to content

Commit 57eac53

Browse files
committed
Add basic auth and settings
1 parent e4b04b8 commit 57eac53

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

src/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
// Restrict access to the front-end to the following IP addresses (logged in admins always have access).
6161
//'restrictFrontEndIpAddresses' => = [],
6262

63+
// Whether to use basic authentication when accessing the site.
64+
//'basicAuthEnabled' => false,
65+
6366
// A username to use for basic authentication when accessing the site.
6467
//'basicAuthUsername' => '',
6568

src/models/SettingsModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class SettingsModel extends Model
7070
*/
7171
public array|string $restrictFrontEndIpAddresses = [];
7272

73+
/**
74+
* @var bool
75+
*/
76+
public bool $basicAuthEnabled = false;
77+
7378
/**
7479
* @var string
7580
*/

src/services/TestsService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function beforeRunTests(): void
141141
}
142142

143143
$config = ['timeout' => 10];
144-
if (!Sherlock::$plugin->getIsLite() && Sherlock::$plugin->settings->basicAuthUsername) {
144+
if (!Sherlock::$plugin->getIsLite() && Sherlock::$plugin->settings->basicAuthEnabled) {
145145
$config['auth'] = [
146146
Sherlock::$plugin->settings->basicAuthUsername,
147147
Sherlock::$plugin->settings->basicAuthPassword,

src/templates/_settings.twig

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,30 +284,42 @@
284284
{{ not isLite ? '<hr>' }}
285285

286286
{% set fields %}
287-
<h1>{{ 'Basic Auth'|t('sherlock') }}</h1>
288-
{{ forms.autosuggestField({
289-
label: 'Username'|t('sherlock'),
290-
name: (isLite ? '' : 'basicAuthUsername'),
291-
instructions: 'A username to use for basic authentication when accessing the site.'|t('sherlock'),
292-
suggestEnvVars: true,
293-
suggestions: craft.cp.getEnvSuggestions(),
294-
warning: (config.basicAuthUsername is defined ? configWarning('basicAuthUsername')),
295-
value: (isLite ? '' : settings.basicAuthUsername),
296-
errors: settings.getErrors('basicAuthUsername'),
297-
disabled: readOnly,
298-
}) }}
299-
{{ forms.autosuggestField({
300-
type: 'password',
301-
label: 'Password'|t('sherlock'),
302-
name: (isLite ? '' : 'basicAuthPassword'),
303-
instructions: 'A password to use for basic authentication when accessing the site.'|t('sherlock'),
287+
{{ forms.lightswitchField({
288+
label: 'Basic Auth'|t('sherlock'),
289+
name: (isLite ? '' : 'basicAuthEnabled'),
290+
instructions: 'Whether to enable basic authentication when accessing the site.'|t('sherlock'),
304291
suggestEnvVars: true,
305292
suggestions: craft.cp.getEnvSuggestions(),
306-
warning: (config.basicAuthPassword is defined ? configWarning('basicAuthPassword')),
307-
value: (isLite ? '' : settings.basicAuthPassword),
308-
errors: settings.getErrors('basicAuthPassword'),
293+
warning: (config.basicAuthEnabled is defined ? configWarning('basicAuthEnabled')),
294+
value: (isLite ? '' : settings.basicAuthEnabled),
295+
errors: settings.getErrors('basicAuthEnabled'),
309296
disabled: readOnly,
297+
toggle: 'basicAuth',
310298
}) }}
299+
<div id="basicAuth" class="hidden">
300+
{{ forms.autosuggestField({
301+
label: 'Username'|t('sherlock'),
302+
name: (isLite ? '' : 'basicAuthUsername'),
303+
instructions: 'A username to use for basic authentication when accessing the site.'|t('sherlock'),
304+
suggestEnvVars: true,
305+
suggestions: craft.cp.getEnvSuggestions(),
306+
warning: (config.basicAuthUsername is defined ? configWarning('basicAuthUsername')),
307+
value: (isLite ? '' : settings.basicAuthUsername),
308+
errors: settings.getErrors('basicAuthUsername'),
309+
disabled: readOnly,
310+
}) }}
311+
{{ forms.autosuggestField({
312+
label: 'Password'|t('sherlock'),
313+
name: (isLite ? '' : 'basicAuthPassword'),
314+
instructions: 'A password to use for basic authentication when accessing the site.'|t('sherlock'),
315+
suggestEnvVars: true,
316+
suggestions: craft.cp.getEnvSuggestions(),
317+
warning: (config.basicAuthPassword is defined ? configWarning('basicAuthPassword')),
318+
value: (isLite ? '' : settings.basicAuthPassword),
319+
errors: settings.getErrors('basicAuthPassword'),
320+
disabled: readOnly,
321+
}) }}
322+
</div>
311323
{% endset %}
312324

313325
{% include 'sherlock/_includes/paidFields' %}

0 commit comments

Comments
 (0)