From 411dec1d3f295e8fe47ce571a506bd1eb499ee8f Mon Sep 17 00:00:00 2001 From: hylom Date: Wed, 10 Oct 2018 23:06:58 +0900 Subject: [PATCH] Plugin: add Users helper --- .../lib/Newslash/Plugin/DefaultConfig.pm | 1 + src/newslash_web/lib/Newslash/Plugin/Users.pm | 102 +++++++++++++++++++++ src/newslash_web/lib/Newslash/Web.pm | 2 + 3 files changed, 105 insertions(+) create mode 100644 src/newslash_web/lib/Newslash/Plugin/Users.pm diff --git a/src/newslash_web/lib/Newslash/Plugin/DefaultConfig.pm b/src/newslash_web/lib/Newslash/Plugin/DefaultConfig.pm index 5d63fdec..6d04ed20 100644 --- a/src/newslash_web/lib/Newslash/Plugin/DefaultConfig.pm +++ b/src/newslash_web/lib/Newslash/Plugin/DefaultConfig.pm @@ -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 index 00000000..47fd8e6a --- /dev/null +++ b/src/newslash_web/lib/Newslash/Plugin/Users.pm @@ -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 is 'Business Logic' layer of Newslash. + + +=head1 HELPERS + +L 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 application. + +=head1 SEE ALSO + +L, L, L. + +=cut diff --git a/src/newslash_web/lib/Newslash/Web.pm b/src/newslash_web/lib/Newslash/Web.pm index 0d1e6c19..a08217b0 100644 --- a/src/newslash_web/lib/Newslash/Web.pm +++ b/src/newslash_web/lib/Newslash/Web.pm @@ -214,6 +214,8 @@ sub startup { # HTTP Compression $app->plugin('Newslash::Plugin::HttpCompression'); + # Users helper + $app->plugin('Newslash::Plugin::Users'); ############################################################ # -- 2.11.0