blib/lib/WebPrototypes/Registration.pm

filter: Statementstmt 94.6% 71 / 75 Branchbran 75.0% 9 / 12 Conditioncond 41.6% 5 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 86.3% 19 / 22 Podpod 100% 8 / 8 totaltotal 83.7% 113 / 135 CC 12 SCAR 13.3
CC
12
Cov
86%
_send_pass_token
10.5
call
6.0
prepare_app
2.1
CRAP 3.8
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use 5.0100;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package WebPrototypes::Registration;
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $WebPrototypes::Registration::VERSION = '0.002';
9 }
10 1 ▶use parent qw(Plack::Component);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URL::Encode 'url_encode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use String::Random 'random_regex';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Email::Sender::Simple qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Email::Simple::Creator;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Plack::Util::Accessor qw( email_validator );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=7.2
Pod documented
22 1 my $self = shift;
23 1 ▶ $self->email_validator( Email::Valid->new() ) if !defined $self->email_validator;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->email_validator 1 0 1
24 }
25  
26 0 ▶sub find_user { die 'find_user needs to be implemented in subclass' }
Subroutine find_user: not called CC=1 SCAR=6.9
Pod documented
27  
28 0 ▶sub create_user { die 'find_user needs to be implemented in subclass' }
Subroutine create_user: not called CC=1 SCAR=6.9
Pod documented
29  
30 3 ▶sub wrap_text{
Subroutine wrap_text: called CC=1 SCAR=0.0
Pod documented
31 3 my( $self, $text ) = @_;
32 3 return "<html><body>$text</body></html>";
33 }
34  
35 3 ▶sub build_reply{
Subroutine build_reply: called CC=1 SCAR=0.0
Pod documented
36 3 my( $self, $text ) = @_;
37 3 return [ 200, [ 'Content-Type' => 'text/html' ], [ $self->wrap_text( $text ) ] ];
38 }
39  
40 3 ▶sub call {
Subroutine call: called CC=6 SCAR=17.9
Pod documented
41 3 my($self, $env) = @_;
42 3 my $req = Plack::Request->new( $env );
43 3 my $uerror = '';
44 3 my $eerror = '';
45 3 my $username = '';
46 3 my $email = '';
47 3 ▶ if( $req->method eq 'POST' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq "POST") 2 1 3
48 2 $username = $req->param( 'username' );
49 2 $email = $req->param( 'email' );
50 2 ▶ if( $self->find_user( $username ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_user($username)) 1 1 2
51 1 $uerror = '<span class="error">This username is already registered</span>';
52 }
53 2 ▶ if( !$self->email_validator->address( $email ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->email_validator->address($email)) 1 1 2
54 1 $eerror = '<span class="error">Wrong format of email</span>';
55 }
56 2 ▶ if( !$uerror && !$eerror ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($uerror or $eerror) 1 1 2
Condition66%2/3
$uerror or $eerror
l!l&&r!l&&!r
101
MC/DC50%1/2
$uerror or $eerror
$uerror $eerror
Truth table66%2/3
A or B
A= $uerrorB= $eerror
ABresult
1X1
011
000
57 1 my $pass_token = random_regex( '\w{40}' );
58 1 my $user = $self->create_user( username => $username, email => $email, pass_token => $pass_token );
59 1 $self->_send_pass_token( $env, $user, $username, $email, $pass_token );
60 1 return $self->build_reply( "Email sent" );
61 }
62 }
63 2 my $encoded_username = url_encode_utf8( $username );
64 2 my $encoded_email = url_encode_utf8( $email );
65 2 return $self->build_reply( <<END );
66 <form method="POST">
67 Username: <input type="text" name="username" value="$encoded_username"> $uerror
68 Email: <input type="text" name="email" value="$encoded_email"> $eerror
69 <input type="submit">
70 </form>
71 END
72  
73 }
74  
75 1 ▶sub build_email {
Subroutine build_email: called CC=1 SCAR=0.0
Pod documented
76 1 my( $self, $to, $reset_url ) = @_;
77 1 return Email::Simple->create(
78 header => [
79 To => $to,
80 From => 'root@localhost',
81 Subject => "Password reset",
82 ],
83 body => $reset_url,
84 );
85 }
86  
87 0 ▶sub send_mail {
Subroutine send_mail: not called CC=1 SCAR=6.9
Pod documented
88 0 my( $self, $mail ) = @_;
89 0 sendmail( $mail );
90 }
91  
92 1 ▶sub _send_pass_token {
Subroutine _send_pass_token: called CC=6 SCAR=23.5
93 1 my( $self, $env, $user, $username, $email, $pass_token ) = @_;
94 my $my_server = $env->{HTTP_ORIGIN} //
95 ( $env->{'psgi.url_scheme'} // 'http' ) . '://' .
96 ( $env->{HTTP_HOST} //
97 $env->{SERVER_NAME} .
98 1 ▶ ( $env->{SERVER_PORT} && $env->{SERVER_PORT} != 80 ? ':' . $env->{SERVER_PORT} : '' )
Branch: 1 branch
BranchTrueFalseTotal
$env->{'SERVER_PORT'} && $env->{'SERVER_PORT'} != 80 ? : 0 0 0
Condition33%3/9
$env->{"HTTP_ORIGIN"} // ($env->{"psgi.url_scheme"} // "http") . '://' . ($env->{"HTTP_HOST"} // $env->{"SERVER_NAME"} . ($env->{"SERVER_PORT"} && $env->{"SERVER_PORT"} != 80 ? ':' . $env->{"SERVER_PORT"} : ""))
l!l
01
$env->{"psgi.url_scheme"} // "http"
l!l
10
$env->{"HTTP_HOST"} // $env->{"SERVER_NAME"} . ($env->{"SERVER_PORT"} && $env->{"SERVER_PORT"} != 80 ? ':' . $env->{"SERVER_PORT"} : "")
l!l
10
$env->{"SERVER_PORT"} and $env->{"SERVER_PORT"} != 80
!ll&&!rl&&r
000
MC/DC0%0/1
$env->{"HTTP_ORIGIN"} // ($env->{"psgi.url_scheme"} // "http") . '://' . ($env->{"HTTP_HOST"} // $env->{"SERVER_NAME"} . ($env->{"SERVER_PORT"} && $env->{"SERVER_PORT"} != 80 ? ':' . $env->{"SERVER_PORT"} : ""))
$env->{"HTTP_ORIGIN"}
MC/DC0%0/1
$env->{"HTTP_HOST"} // $env->{"SERVER_NAME"} . ($env->{"SERVER_PORT"} && $env->{"SERVER_PORT"} != 80 ? ':' . $env->{"SERVER_PORT"} : "")
$env->{"HTTP_HOST"}
MC/DC0%0/2
$env->{"SERVER_PORT"} and $env->{"SERVER_PORT"} != 80
$env->{"SERVER_PORT"} $env->{"SERVER_PORT"} != 80
Truth table50%1/2
A
A= $env->{"HTTP_ORIGIN"}
Aresult
11
00
Truth table50%1/2
A
A= $env->{"HTTP_HOST"}
Aresult
11
00
Truth table0%0/3
A and B
A= $env->{"SERVER_PORT"}B= $env->{"SERVER_PORT"} != 80
ABresult
0X0
100
111
99 );
100 1 my $reset_url = URI->new( $my_server );
101 1 $reset_url->path( "/ResetPass/reset/$username/$pass_token" );
102 1 $self->send_mail( $self->build_email( $email, $reset_url ) );
103 }
104  
105  
106 1 1;
107  
108  
109  
110 =pod
111  
112 =head1 NAME
113  
114 WebPrototypes::Registration - (Experimental) Plack application for registering a new user
115  
116 =head1 VERSION
117  
118 version 0.002
119  
120 =head1 SYNOPSIS
121  
122 # connecting with DBIx::Class
123 {
124 package My::Register;
125 use parent 'WebPrototypes::Registration';
126 use Plack::Util::Accessor qw( schema );
127  
128 sub find_user {
129 my( $self, $name ) = @_;
130 return $self->schema->resultset( 'User' )->search({ username => $name })->next;
131 }
132  
133 sub create_user {
134 my( $self, %fields ) = @_;
135 return $self->schema->resultset( 'User' )->create({ %fields });
136 }
137 }
138  
139 use Plack::Builder;
140  
141 my $app = My::Register->new( schema => $schema );
142  
143 builder {
144 mount "/register" => builder {
145 $app->to_app;
146 };
147 };
148  
149 =head1 DESCRIPTION
150  
151 This application implements a user registration mechanism. After the registration
152 and email address verification letter is sent.
153  
154 The examples here are with DBIx::Class
155 but they can be easily ported to other storage layers.
156  
157 This application uses the Template Method design pattern.
158  
159 =head2 PURE VIRTUAL METHODS
160  
161 These methods need to be overriden in subclass.
162  
163 =over 4
164  
165 =item find_user ( name )
166  
167 Should return a true value if the name is already registered
168  
169 =item create_user ( attributes )
170  
171 Should create the user object.
172  
173 =back
174  
175 =head2 VIRTUAL METHODS
176  
177 These methods have defaults - but should probably be overriden anyway.
178  
179 =over 4
180  
181 =item wrap_text ( text )
182  
183 Should return the html page containing the passed text fragment. By default it just adds
184 the html and body tags.
185  
186 =item build_reply ( page_body )
187  
188 Should return the PSGI response data structure.
189  
190 =item build_email ( to_address, link_to_the_reset_page )
191  
192 Should create the email containing the link.
193  
194 =item send_mail ( mail )
195  
196 Should send the mail (created by build_mail).
197  
198 =back
199  
200 =head2 OTHER METHODS
201  
202 =over 4
203  
204 =item call ( env )
205  
206 =back
207  
208 =head1 SEE ALSO
209  
210 L<Plack>
211 L<Plack::Middleware::Auth::Form>
212  
213 =head1 AUTHOR
214  
215 Zbigniew Lukasiak <zby@cpan.org>
216  
217 =head1 COPYRIGHT AND LICENSE
218  
219 This software is Copyright (c) 2011 by Zbigniew Lukasiak <zby@cpan.org>.
220  
221 This is free software, licensed under:
222  
223 The Artistic License 2.0 (GPL Compatible)
224  
225 =cut
226  
227  
228 __END__
↑ Summary