OSDN Git Service

add administrtor commands
authorhylom <hylom@users.sourceforge.jp>
Thu, 16 Aug 2018 11:40:06 +0000 (20:40 +0900)
committerhylom <hylom@users.sourceforge.jp>
Thu, 16 Aug 2018 11:40:06 +0000 (20:40 +0900)
src/newslash_web/lib/Newslash/Command/adduser.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Web.pm

diff --git a/src/newslash_web/lib/Newslash/Command/adduser.pm b/src/newslash_web/lib/Newslash/Command/adduser.pm
new file mode 100644 (file)
index 0000000..b9b8396
--- /dev/null
@@ -0,0 +1,46 @@
+package Newslash::Command::adduser;
+use Mojo::Base 'Mojolicious::Command';
+
+use Mojo::Util 'getopt';
+use Data::Dumper;
+
+has description => 'Add new user';
+#has usage => sub { shift->extract_usage };
+
+sub run {
+    my ($self, @args) = @_;
+    my $app = $self->app;
+
+    my $seclev = 1;
+    my $author = 0;
+
+    getopt \@args,
+      'u|username=s' => \my $nickname,
+      'e|email=s'    => \my $email,
+      'seclev=i'     => \$seclev,
+      'author=i'     => \$author;
+
+    if (!$nickname || !$email) {
+        $app->log->error("adduser: nickname or email not given!");
+        return;
+    }
+
+    my $users = $app->model('users');
+    my $uid = $users->create($nickname, $email);
+    if ($uid) {
+        $app->log->info("create new user '$nickname'. uid is $uid");
+    }
+    else {
+        my $err = $users->last_error;
+        $app->log->error("create new user '$nickname' failed - $err");
+    }
+
+    if ($seclev || $author) {
+        my $rs = $users->update(uid => $uid,
+                                seclev => $seclev,
+                                author => $author);
+        $app->log->info("update user info done ($rs)");
+    }
+}
+
+1;
index 6457b0e..7570bd9 100644 (file)
@@ -10,6 +10,9 @@ use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
 sub startup {
     my $app = shift;
 
+    # add commands in Newslash::Command
+    push @{$app->commands->namespaces}, 'Newslash::Command';
+
     # load config file
     # first, check existence of /etc/newslash.conf
     if ($app->mode eq 'production' && -e CONFIG_FILE) {