OSDN Git Service

Plugin: add Users helper
authorhylom <hylom@users.sourceforge.jp>
Wed, 10 Oct 2018 14:06:58 +0000 (23:06 +0900)
committerhylom <hylom@users.sourceforge.jp>
Wed, 10 Oct 2018 14:07:47 +0000 (23:07 +0900)
src/newslash_web/lib/Newslash/Plugin/DefaultConfig.pm
src/newslash_web/lib/Newslash/Plugin/Users.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Web.pm

index 5d63fde..6d04ed2 100644 (file)
@@ -9,6 +9,7 @@ my $defaults = {
                             },
 
                Site => { name => "newslash",
+                          admin_email => 'admin@example.com',
                          root => "/",
                          base_url => "https://example.com",
                          description => "this is newslash",
diff --git a/src/newslash_web/lib/Newslash/Plugin/Users.pm b/src/newslash_web/lib/Newslash/Plugin/Users.pm
new file mode 100644 (file)
index 0000000..47fd8e6
--- /dev/null
@@ -0,0 +1,102 @@
+package Newslash::Plugin::Users;
+use Mojo::Base 'Mojolicious::Plugin';
+use Email::Valid;
+
+has 'last_error';
+has 'app';
+
+sub register {
+    my ($self, $app, $conf) = @_;
+    $self->app($app);
+    $app->helper(users => sub { state $users = $self; });
+
+    # default config values
+    #my $cnf = $app->config->{Users} ||= {};
+}
+
+sub validate_new_user {
+    my ($self, $nickname, $email) = @_;
+    my $nick_regex = qr/^[a-zA-Z_][ a-zA-Z0-9\$_.+!*\'(),-]{0,19}$/;
+    my $users = $self->app->model('users');
+
+    my ($id_error, $email_error);
+    $id_error = "BLANK_ID" if !$nickname;
+    $email_error = "BLANK_MAIL" if !$email;
+
+    if (!$id_error) {
+        # nickname is valid?
+        if ($nickname =~ $nick_regex) {
+            my $matchname = $users->nickname_to_matchname($nickname);
+            my $rs = $users->select(matchname => $matchname);
+            if ($rs) {
+                $id_error = "ID_EXISTS";
+            }
+        }
+        else {
+            $id_error = "INVALID_ID";
+        }
+    }
+
+    if (!$email_error) {
+        if (Email::Valid->address($email)) {
+            my $rs = $users->select(realemail => $email);
+            if ($rs) {
+                $email_error = "EMAIL_EXISTS";
+            }
+        }
+        else {
+            $email_error = "INVALID_EMAIL";
+        }
+    }
+
+    return ($id_error, $email_error);
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::Users - Newslash users manipulation plugin
+
+=head1 SYNOPSIS
+
+  # Mojolicious
+  $app->plugin('Newslash::Plugin::Users');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::Users> is 'Business Logic' layer of Newslash.
+
+
+=head1 HELPERS
+
+L<Mojolicious::Plugin::Users> implements the following helpers.
+
+=head2 boxes
+
+  [% helpers.boxes() %]
+
+Fetch box contents for current user and returns them.
+
+=head2 format_timestamp
+
+  $c->format_timestamp(user => $user, epoch => $epoch, format => "user")
+  $c->format_timestamp(datetime => $dt, format => "simple")
+
+Return formated string.
+
+=head1 METHODS
+
+=head2 register
+
+  $plugin->register(Mojolicious->new);
+
+Register helpers in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut
index 0d1e6c1..a08217b 100644 (file)
@@ -214,6 +214,8 @@ sub startup {
     # HTTP Compression
     $app->plugin('Newslash::Plugin::HttpCompression');
 
+    # Users helper
+    $app->plugin('Newslash::Plugin::Users');
 
     ############################################################
     #