ReviewOS

also looking at this

mail-os/mail

feat: mailbox recovery addresses and password reset by email

#13
Merged chris wants to merge feat/mailbox-recovery into main
21 files +1503 -10
packages/zig/src/cli/user_local.zigmodified+37-0
Changes to packages/zig/src/cli/user_local.zig
@@ -4,6 +4,7 @@ const user_cli = @import("../user_cli.zig");
44const database = @import("../storage/database.zig");
55const auth = @import("../auth/auth.zig");
66const env = @import("../core/env.zig");
7const recovery = @import("../api/webmail_recovery.zig");
78
89fn withDb(comptime action: fn (*cli.BaseCommand.ParseContext, *database.Database, *auth.AuthBackend) anyerror!void) cli.BaseCommand.CommandAction {
910 return struct {
@@ -130,6 +131,29 @@ fn changePasswordAction(ctx: *cli.BaseCommand.ParseContext, _: *database.Databas
130131 try user_cli.changePassword(auth_backend, username, password);
131132}
132133
134fn recoveryAction(ctx: *cli.BaseCommand.ParseContext, db: *database.Database, _: *auth.AuthBackend) !void {
135 const usage = "Usage: mail user:local recovery <username> [<address>] [--policy initial|managed] [--clear]\n";
136 const username = ctx.getArgument(0) orelse {
137 std.debug.print("Error: username is required\n{s}", .{usage});
138 return error.MissingArgument;
139 };
140 const address = ctx.getArgument(1);
141 if (ctx.hasOption("clear")) {
142 if (address != null) {
143 std.debug.print("Error: give an address or --clear, not both\n{s}", .{usage});
144 return error.InvalidArgument;
145 }
146 return user_cli.recoveryCommand(db, ctx.allocator, username, .clear);
147 }
148 const wanted = address orelse return user_cli.recoveryCommand(db, ctx.allocator, username, .show);
149 const policy_name = ctx.getOption("policy") orelse "managed";
150 const policy = recovery.Policy.parse(policy_name) orelse {
151 std.debug.print("Error: --policy must be 'initial' or 'managed'\n{s}", .{usage});
152 return error.InvalidArgument;
153 };
154 return user_cli.recoveryCommand(db, ctx.allocator, username, .{ .apply = .{ .address = wanted, .policy = policy } });
155}
156
133157pub fn setup(allocator: std.mem.Allocator) !*cli.BaseCommand {
134158 const cmd = try cli.BaseCommand.init(allocator, "user:local", "Local user management (direct DB access)");
135159
@@ -190,5 +214,18 @@ pub fn setup(allocator: std.mem.Allocator) !*cli.BaseCommand {
190214 _ = change_pw_cmd.setAction(withDb(changePasswordAction));
191215 _ = try cmd.addCommand(change_pw_cmd);
192216
217 // recovery
218 const recovery_cmd = try cli.BaseCommand.init(allocator, "recovery", "Show, set or clear a mailbox's password recovery address");
219 _ = try recovery_cmd.addArgument(cli.Argument.init("username", "Mailbox (full address)", .string));
220 _ = try recovery_cmd.addArgument(cli.Argument.init("address", "Recovery address to set (omit to show the current one)", .string).withRequired(false));
221 _ = try recovery_cmd.addOption(
222 cli.Option.init("policy", "policy", "managed (default): always set it; initial: keep an address the mailbox owner set in webmail", .string),
223 );
224 _ = try recovery_cmd.addOption(
225 cli.Option.init("clear", "clear", "Remove the recovery address", .bool),
226 );
227 _ = recovery_cmd.setAction(withDb(recoveryAction));
228 _ = try cmd.addCommand(recovery_cmd);
229
193230 return cmd;
194231}