Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ public interface IConfigurationService
/// </summary>
void SetButtonSoundEnabled(bool enabled);

/// <summary>
/// When true, Administration and Settings ask for the tablet's screen
/// code before they open; see <see cref="OperatorLock"/>. Defaults to
/// true. The tablet's own switch, not Freedom's.
/// </summary>
bool IsScreenCodeRequired { get; }

/// <summary>
/// Persists the screen-code switch. Takes effect on the next
/// navigation to an operator's page.
/// </summary>
void SetScreenCodeRequired(bool required);

/// <summary>
/// The compliance contact, from Freedom (<c>compliance.email</c>) —
/// used as the Reply-To on acceptance confirmations. Empty when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace TheBleedingDeacons.Intergroup.Register.Services;
/// Away for less than <see cref="DefaultGrace"/> keeps the window; any
/// longer and the code is asked for again.</para>
///
/// <para><b>It can be switched off</b>, per tablet, from Settings: a tablet
/// that never leaves the operator need not ask. Off, nothing is guarded, so
/// nothing is asked and nothing is closed. Switching it off is itself behind
/// the code, since Settings is.</para>
///
/// <para>This decides only when to ask. Asking is the platform's, in the app,
/// because the screen-lock prompt is Android's own.</para>
/// </summary>
Expand All @@ -36,6 +41,7 @@ public sealed class OperatorLock
public static readonly TimeSpan DefaultGrace = TimeSpan.FromMinutes(2);

private readonly HashSet<string> _guarded;
private readonly Func<bool> _required;
private readonly TimeProvider _time;
private readonly TimeSpan _window;
private readonly TimeSpan _grace;
Expand All @@ -47,17 +53,23 @@ public sealed class OperatorLock
/// <param name="time">The clock; the system's unless a test supplies one.</param>
/// <param name="window">How long a correct code lasts; <see cref="DefaultWindow"/> unless given.</param>
/// <param name="grace">How long the app may be away; <see cref="DefaultGrace"/> unless given.</param>
/// <param name="required">
/// Whether the code is asked for at all, read at each navigation so the
/// switch takes effect at once. Always, unless given.
/// </param>
public OperatorLock(
IEnumerable<string> guardedRoutes,
TimeProvider? time = null,
TimeSpan? window = null,
TimeSpan? grace = null)
TimeSpan? grace = null,
Func<bool>? required = null)
{
ArgumentNullException.ThrowIfNull(guardedRoutes);

_window = Positive(window ?? DefaultWindow, nameof(window));
_grace = Positive(grace ?? DefaultGrace, nameof(grace));
_guarded = new HashSet<string>(guardedRoutes, StringComparer.Ordinal);
_required = required ?? (static () => true);
_time = time ?? TimeProvider.System;
}

Expand All @@ -77,11 +89,12 @@ public bool IsOpen
/// Whether a Shell location reaches an operator's page. Any segment
/// counts, so <c>//SettingsPage/MailSettingsPage</c> and a bare
/// <c>MailSettingsPage</c> pushed from Settings are both guarded; a
/// query string is not part of the route.
/// query string is not part of the route. Nothing is guarded while the
/// code is switched off.
/// </summary>
public bool Guards(string? location)
{
if (string.IsNullOrWhiteSpace(location))
if (string.IsNullOrWhiteSpace(location) || !_required())
return false;

var query = location.IndexOf('?', StringComparison.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public sealed class FakeConfigurationService : IConfigurationService
public bool IsComplianceAcceptanceEmailEnabled { get; set; } = true;

public bool IsButtonSoundEnabled { get; set; } = true;
public bool IsScreenCodeRequired { get; set; } = true;

public string ComplianceEmail { get; set; } = "privacy@aa-bristol.org";

Expand Down Expand Up @@ -171,6 +172,7 @@ public Task SaveActiveIntergroupMeetingAsync(int? meetingId)
public void SetComplianceAcceptanceEmailEnabled(bool enabled) => IsComplianceAcceptanceEmailEnabled = enabled;

public void SetButtonSoundEnabled(bool enabled) => IsButtonSoundEnabled = enabled;
public void SetScreenCodeRequired(bool required) => IsScreenCodeRequired = required;

public void InvalidateCache()
{
Expand Down
29 changes: 29 additions & 0 deletions TheBleedingDeacons.Intergroup.Register.Tests/OperatorLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,33 @@ public void Coming_back_without_having_left_changes_nothing()

Assert.True(gate.IsOpen);
}

[Theory]
[InlineData("//AdminPage")]
[InlineData("//SettingsPage/MailSettingsPage")]
public void Switched_off_nothing_is_guarded(string location)
{
var gate = new OperatorLock(Guarded, _time, Window, required: () => false);

Assert.False(gate.Guards(location));
}

[Fact]
public void The_switch_is_read_at_each_navigation()
{
var required = true;
var gate = new OperatorLock(Guarded, _time, Window, required: () => required);

Assert.True(gate.Guards("//SettingsPage"));

required = false;
Assert.False(gate.Guards("//SettingsPage"));

required = true;
Assert.True(gate.Guards("//SettingsPage"));
}

[Fact]
public void Without_a_switch_the_code_is_always_asked_for() =>
Assert.True(new OperatorLock(Guarded, _time).Guards("//AdminPage"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public sealed class FakeConfigurationService : IConfigurationService
public bool IsComplianceAcceptanceEmailEnabled { get; set; } = true;

public bool IsButtonSoundEnabled { get; set; } = true;
public bool IsScreenCodeRequired { get; set; } = true;

public string ComplianceEmail { get; set; } = string.Empty;

Expand Down Expand Up @@ -229,6 +230,7 @@ public Task SaveActiveIntergroupMeetingAsync(int? meetingId)
public void SetComplianceAcceptanceEmailEnabled(bool enabled) => IsComplianceAcceptanceEmailEnabled = enabled;

public void SetButtonSoundEnabled(bool enabled) => IsButtonSoundEnabled = enabled;
public void SetScreenCodeRequired(bool required) => IsScreenCodeRequired = required;

public void InvalidateCache()
{
Expand Down
9 changes: 7 additions & 2 deletions TheBleedingDeacons.Intergroup.Register/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,13 @@ public static MauiApp CreateMauiApp()

builder.Services.AddScoped<IPopupNotification, PopupNotificationService>();

// Administration and Settings ask for the tablet's screen code.
builder.Services.AddSingleton(_ => new OperatorLock(AppShell.OperatorRoutes));
// Administration and Settings ask for the tablet's screen code, unless
// Settings has it switched off for this tablet.
builder.Services.AddSingleton(sp =>
{
var config = sp.GetRequiredService<IConfigurationService>();
return new OperatorLock(AppShell.OperatorRoutes, required: () => config.IsScreenCodeRequired);
});
#if ANDROID
builder.Services.AddSingleton<IScreenLockPrompt, AndroidScreenLockPrompt>();
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ConfigurationService : IConfigurationService
private const string WELCOME_EMAIL_ENABLED_KEY = "welcome_email_on_registration_enabled";
private const string DEVICE_LABEL_KEY = "device_label";
private const string BUTTON_SOUND_ENABLED_KEY = "button_sound_enabled";
private const string SCREEN_CODE_REQUIRED_KEY = "screen_code_required";

// Null when the build names no Freedom site, or off Android. Every
// credential and endpoint is then empty. See FreedomSettings.
Expand Down Expand Up @@ -150,6 +151,47 @@ public void SetButtonSoundEnabled(bool enabled)
}
}

// =================================================================
// Screen code for the operator's pages — the tablet's own
// =================================================================

/// <summary>
/// Reads the switch from Preferences. Defaults to <c>true</c> when it
/// has never been written, and fails safe to on if Preferences cannot
/// be read or holds something unreadable: off is the one that lets the
/// room into Administration.
/// </summary>
public bool IsScreenCodeRequired
{
get
{
try
{
var raw = Preferences.Get(SCREEN_CODE_REQUIRED_KEY, string.Empty);
if (string.IsNullOrEmpty(raw)) return true;
return !bool.TryParse(raw, out var value) || value;
}
catch (Exception ex)
{
Logger.Warning(ex, "Failed to read screen-code switch — defaulting to on");
return true;
}
Comment on lines +174 to +178
}
}

public void SetScreenCodeRequired(bool required)
{
try
{
Preferences.Set(SCREEN_CODE_REQUIRED_KEY, required ? "true" : "false");
Logger.Information("Screen code for the operator's pages {State}", required ? "REQUIRED" : "NOT REQUIRED");
}
catch (Exception ex)
{
Logger.Warning(ex, "Failed to save screen-code switch");
}
Comment on lines +189 to +192
}

// =================================================================
// Active intergroup meeting — the tablet's own
// =================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,23 @@ public bool IsButtonSoundEnabled
}
}

/// <summary>
/// Two-way bound to a Switch on SettingsPage. When on, Administration
/// and Settings ask for the tablet's screen code. See
/// <see cref="IConfigurationService.IsScreenCodeRequired"/> and
/// <see cref="OperatorLock"/>. Defaults to on.
/// </summary>
public bool IsScreenCodeRequired
{
get => _configService.IsScreenCodeRequired;
set
{
if (_configService.IsScreenCodeRequired == value) return;
_configService.SetScreenCodeRequired(value);
OnPropertyChanged();
}
}

// =================================================================
// Navigation
// =================================================================
Expand Down
42 changes: 42 additions & 0 deletions TheBleedingDeacons.Intergroup.Register/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,48 @@
Dark={StaticResource Gray300}}" />
</Grid>
</Border>

<Border
Grid.Row="1"
Grid.Column="1"
Padding="15,12"
BackgroundColor="{AppThemeBinding Light=White,
Dark=#2D2D30}"
Stroke="{AppThemeBinding Light=#E0E0E0,
Dark=#424242}"
StrokeShape="RoundRectangle 10"
StrokeThickness="1">
<Grid
ColumnDefinitions="*,Auto"
ColumnSpacing="12"
RowDefinitions="Auto,Auto"
RowSpacing="4">

<Label
Grid.Row="0"
Grid.Column="0"
FontAttributes="Bold"
FontSize="16"
Text="Screen Code for Admin and Settings"
VerticalOptions="Center" />

<Switch
Grid.Row="0"
Grid.Column="1"
IsToggled="{Binding IsScreenCodeRequired, Mode=TwoWay}"
VerticalOptions="Center" />

<Label
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
FontSize="13"
LineBreakMode="WordWrap"
Text="When on, Administration and Settings ask for this tablet's screen code before they open, so the room cannot reset the device or copy the database. Turn off only for a tablet that never leaves the operator. On by default."
TextColor="{AppThemeBinding Light={StaticResource Gray600},
Dark={StaticResource Gray300}}" />
</Grid>
</Border>
</Grid>

<!--
Expand Down
Loading