also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -4,6 +4,7 @@ const user_cli = @import("../user_cli.zig"); | ||
| 4 | 4 | const database = @import("../storage/database.zig"); |
| 5 | 5 | const auth = @import("../auth/auth.zig"); |
| 6 | 6 | const env = @import("../core/env.zig"); |
| 7 | const recovery = @import("../api/webmail_recovery.zig"); | |
| 7 | 8 | |
| 8 | 9 | fn withDb(comptime action: fn (*cli.BaseCommand.ParseContext, *database.Database, *auth.AuthBackend) anyerror!void) cli.BaseCommand.CommandAction { |
| 9 | 10 | return struct { |
| @@ -130,6 +131,29 @@ fn changePasswordAction(ctx: *cli.BaseCommand.ParseContext, _: *database.Databas | ||
| 130 | 131 | try user_cli.changePassword(auth_backend, username, password); |
| 131 | 132 | } |
| 132 | 133 | |
| 134 | fn 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 | ||
| 133 | 157 | pub fn setup(allocator: std.mem.Allocator) !*cli.BaseCommand { |
| 134 | 158 | const cmd = try cli.BaseCommand.init(allocator, "user:local", "Local user management (direct DB access)"); |
| 135 | 159 | |
| @@ -190,5 +214,18 @@ pub fn setup(allocator: std.mem.Allocator) !*cli.BaseCommand { | ||
| 190 | 214 | _ = change_pw_cmd.setAction(withDb(changePasswordAction)); |
| 191 | 215 | _ = try cmd.addCommand(change_pw_cmd); |
| 192 | 216 | |
| 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 | ||
| 193 | 230 | return cmd; |
| 194 | 231 | } |