OSDN Git Service

Plugin::Users: split validate_new_user()
authorhylom <hylom@users.sourceforge.jp>
Wed, 28 Nov 2018 11:14:00 +0000 (20:14 +0900)
committerhylom <hylom@users.sourceforge.jp>
Wed, 28 Nov 2018 11:14:00 +0000 (20:14 +0900)
src/newslash_web/lib/Newslash/Plugin/Users.pm
src/newslash_web/lib/Newslash/Web/Controller/API/User.pm

index d228481..c9ad66d 100644 (file)
@@ -367,7 +367,10 @@ sub create_new_user {
     $options ||= {};
 
     # check $nickname and $email
-    my ($id_error, $email_error) = $self->validate_new_user($nickname, $email);
+    my ($id_error, $email_error) = (0, 0);
+    $id_error = $self->validate_nickname($nickname) ? 0 : $self->last_error;
+    $email_error = $self->validate_email($email) ? 0 : $self->last_error;
+
     if ($id_error || $email_error) {
         $self->last_error({ id_error => $id_error,
                             email_error => $email_error });
@@ -400,49 +403,55 @@ sub create_new_user {
     return $uid;
 }
 
-sub validate_new_user {
-    my ($self, $nickname, $email) = @_;
+sub validate_nickname {
+    my ($self, $nickname) = @_;
     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_ADDRESS" 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 (!$nickname) {
+        $self->last_error("BLANK_ID");
+        return;
     }
 
-    if (!$email_error) {
-        if (Email::Valid->address($email)) {
-            my $rs = $users->select(realemail => $email);
-            if ($rs) {
-                $email_error = "ADDRESS_EXISTS";
-            }
-        }
-        else {
-            $email_error = "INVALID_ADDRESS";
-        }
+    if ($nickname !~ $nick_regex) {
+        $self->last_error("INVALID_ID");
+        return;
     }
 
-    if ($id_error || $email_error) {
-        $self->last_error({id_error => $id_error, email_error => $email_error});
+    my $matchname = $users->nickname_to_matchname($nickname);
+    my $rs = $users->select(matchname => $matchname);
+    if ($rs) {
+        $self->last_error("ID_EXISTS");
         return;
     }
 
     return 1;
 }
 
+sub validate_email {
+    my ($self, $email) = @_;
+    my $users = $self->app->model('users');
+
+    if (!$email) {
+        $self->last_error("BLANK_ADDRESS");
+        return;
+    }
+
+    if (!Email::Valid->address($email)) {
+        $self->last_error("INVALID_ADDRESS");
+        return;
+    }
+
+    my $rs = $users->select(realemail => $email);
+    if ($rs) {
+        $self->last_error("ADDRESS_EXISTS");
+        return;
+    }
+
+    return 1;
+}
+
+
 1;
 
 =encoding utf8
@@ -465,18 +474,11 @@ L<Newslash::Plugin::Users> is 'Business Logic' layer of Newslash.
 
 L<Mojolicious::Plugin::Users> implements the following helpers.
 
-=head2 boxes
-
-  [% helpers.boxes() %]
-
-Fetch box contents for current user and returns them.
+=head2 users
 
-=head2 format_timestamp
+  my $u = helpers.users();
 
-  $c->format_timestamp(user => $user, epoch => $epoch, format => "user")
-  $c->format_timestamp(datetime => $dt, format => "simple")
-
-Return formated string.
+Returns Newslash::Plugin::Users instance.
 
 =head1 METHODS
 
@@ -486,24 +488,31 @@ Return formated string.
 
 Register helpers in L<Mojolicious> application.
 
-=head2 validate_new_user
+=head2 request_change_email
+
+=head2 generate_email_change_token
+
+=head2 verify_and_change_email
+
+=head2 cancel_change_email_request
+
+=head2 rest_password
+
+=head2 generate_password_reset_token
 
-  my ($id_error, $email_error) = $plugin->validate_new_user($nickname, $email);
+=head2 cancel_activation
 
-Check nickname and email address are not registered.
+=head2 update_password_by_token
 
-$id_error is one of below string or undef (no error).
+=head2 update_password
 
-  BLANK_ID
-  ID_EXISTS
-  INVALID_ID
+=head2 activation
 
-$email_error is one of below string or undef (no error).
+=head2 create_new_user
 
-  BLANK_ADDRESS
-  ADDRESS_EXISTS
-  INVALID_ADDRESS
+=head2 validate_nickname
 
+=head2 validate_email
 
 =head1 SEE ALSO
 
index adf10cb..360fce0 100644 (file)
@@ -393,13 +393,14 @@ sub validate_new_user {
     my $email = $params->{'email'};
     $email =~ s/\s//g; # strip whitespace
 
-    my $nick_regex = qr/^[a-zA-Z_][ a-zA-Z0-9\$_.+!*\\'(),-]{0,19}$/;
+    my ($id_error, $email_error) = (0, 0);
+    $id_error = $self->validate_nickname($nickname) ? 0 : $self->last_error;
+    $email_error = $self->validate_email($email) ? 0 : $self->last_error;
 
-    if (!$c->users->validate_new_user($nickname, $email)) {
-        my $error = $c->users->last_error;
+    if ($id_error || $email_error) {
         $c->render(json => { error => 1,
-                             id_error => $error->{id_error} || "",
-                             email_error => $error->{email_error} || "",
+                             id_error => $id_error || "",
+                             email_error => $email_error || "",
                              nickname => $nickname,
                              email => $email });
         $c->rendered(400);