From 19ba7686d9e450d91566d49a29ddb4fae563ab31 Mon Sep 17 00:00:00 2001 From: Kaitlyn Parkhurst Date: Mon, 21 Nov 2022 15:30:18 -0800 Subject: [PATCH] Add create_user functionality. --- Web/lib/MJB/Web/Command/create_user.pm | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Web/lib/MJB/Web/Command/create_user.pm diff --git a/Web/lib/MJB/Web/Command/create_user.pm b/Web/lib/MJB/Web/Command/create_user.pm new file mode 100644 index 0000000..2fb834a --- /dev/null +++ b/Web/lib/MJB/Web/Command/create_user.pm @@ -0,0 +1,41 @@ +package MJB::Web::Command::create_user; +use Mojo::Base 'Mojolicious::Command'; + +use Mojo::Util qw( getopt ); + +has description => "Create an user account that can login to the panel."; +has usage => "$0 create_user \"User Name\" email_address\@domain.com SecurePassword\n"; + +sub run { + my ( $self, $name, $email, $password ) = @_; + + die "Error: you must provide an name.\n" + unless $name; + + die "Error: you must provide an email address.\n" + unless $email; + + die "Error: you must provide a valid email address.\n" + unless $email =~ /@/; + + die "Error: you must provide a password.\n" + unless $password; + + my $person = $self->app->db->storage->schema->txn_do( sub { + my $person = $self->app->db->resultset('Person')->create({ + email => $email, + name => $name, + }); + $person->new_related('auth_password', {})->set_password($password); + + # Notify the system about the new account. + $self->app->db->system_notes->create({ + source => 'User Registration (CLI)', + content => "An account was created for $name", + }); + + return $person; + }); +} + +1;