--- /dev/null
+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