lib/Bracket/Controller/Auth.pm

filter: Statementstmt 62.5% 92 / 147 Branchbran 29.1% 7 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 28 / 36 Podpod 30.0% 3 / 10 totaltotal 59.9% 130 / 217 CC 12 SCAR 11.9
CC
12
Cov
58%
login
10.0
reset_password
8.1
email_reset_password_link
6.6
CRAP 3.3
« lib/Bracket/Form/Password/Change.pm ↑ Summary lib/Bracket/Form/Password/ResetEmail.pm »
1 package Bracket::Controller::Auth;
2  
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶BEGIN { extends 'Catalyst::Controller' }
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Time::HiRes qw/ time /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Bracket::Form::Register;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Bracket::Form::Login;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Bracket::Form::Password::Change;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Bracket::Form::Password::ResetEmail;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Bracket::Form::Password::Reset;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶sub debug { 0 }
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
13 4 ▶require Data::Dumper if debug;
Branch: 1 branch
BranchTrueFalseTotal
if debug() 0 4 4
14  
15  
16 =head1 Name
17  
18 Bracket::Controller::Admin - Functions for admin users
19
20 =head1 Description
21  
22 Controller with authentication related actions:
23  
24 * register
25 * login/logout
26 * change/reset password
27  
28 =cut
29  
30 has 'register_form' => (
31 isa => 'Bracket::Form::Register',
32 is => 'rw',
33 lazy => 1,
34 1 ▶ default => sub { Bracket::Form::Register->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 4 );
36  
37 has 'login_form' => (
38 isa => 'Bracket::Form::Login',
39 is => 'rw',
40 lazy => 1,
41 2 ▶ default => sub { Bracket::Form::Login->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 4 );
43  
44 has 'change_password_form' => (
45 isa => 'Bracket::Form::Password::Change',
46 is => 'rw',
47 lazy => 1,
48 0 ▶ default => sub { Bracket::Form::Password::Change->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 4 );
50  
51 has 'email_reset_password_link_form' => (
52 isa => 'Bracket::Form::Password::ResetEmail',
53 is => 'rw',
54 lazy => 1,
55 1 ▶ default => sub { Bracket::Form::Password::ResetEmail->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 4 );
57  
58 has 'reset_password_form' => (
59 isa => 'Bracket::Form::Password::Reset',
60 is => 'rw',
61 lazy => 1,
62 0 ▶ default => sub { Bracket::Form::Password::Reset->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 4 );
64  
65 1 ▶sub register : Global {
Subroutine register: called CC=2 SCAR=7.6
Pod undocumented
66 1 my ($self, $c) = @_;
67  
68 1 $c->stash(
69 template => 'form/auth/register.tt',
70 form => $self->register_form,
71 );
72  
73 1 my $new_player = $c->model('DBIC::Player')->new_result({});
74 1 $self->register_form->process(
75 item => $new_player,
76 params => $c->request->parameters,
77 );
78  
79 # This return on GET (new form) and a POSTed form that's invalid.
80 1 ▶ return if !$self->register_form->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->register_form->is_valid 1 0 1
81  
82 # At this stage the form has validated
83 0 $c->flash->{status_msg} = 'Registration succeeded';
84 0 $c->response->redirect($c->uri_for('/login'));
85 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 =head2 login
88  
89 Log in through the authentication system.
90  
91 =cut
92  
93 2 ▶sub login : Global {
Subroutine login: called CC=4 SCAR=23.1
Pod documented
94 2 my ($self, $c) = @_;
95  
96 2 $c->stash(
97 template => 'form/auth/login.tt',
98 form => $self->login_form,
99 );
100  
101 2 $self->login_form->process(params => $c->request->parameters,);
102  
103 # This return on GET (new form) and a POSTed form that's invalid.
104 2 ▶ return if !$self->login_form->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->login_form->is_valid 2 0 2
105  
106 0 my $is_authenticated = $c->authenticate(
107 {
108 email => $self->login_form->field('email')->value,
109 password => $self->login_form->field('password')->value,
110 }
111 );
112 0 ▶ if (!$is_authenticated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_authenticated) 0 0 0
113 0 my $login_URI = $c->uri_for('/login');
114 0 $c->response->body("Could not <big><a href='$login_URI'>login</a></big>");
115 0 $c->detach();
116 }
117  
118 0 my $user_id = $c->user->id;
119 0 ▶ warn "USER ID: $user_id" if debug;
Branch: 1 branch
BranchTrueFalseTotal
if debug() 0 0 0
120  
121 # At this stage the form has validated
122 0 $c->response->redirect(
123 $c->uri_for($c->controller('Player')->action_for('home')) . "/${user_id}",
124 );
125  
126 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 =head2 logout
129
130 Log in through the authentication system.
131  
132 =cut
133  
134 0 ▶sub logout : Global {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
135 0 my ($self, $c) = @_;
136  
137 0 $c->logout;
138 0 $c->response->redirect($c->uri_for('/login'));
139  
140 0 return;
141 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 =head2 change_password
144  
145 Change player password
146  
147 =cut
148  
149 0 ▶sub change_password : Global {
Subroutine change_password: not called CC=2 SCAR=17.9
Pod documented
150 0 my ($self, $c) = @_;
151  
152 0 my $form = $self->change_password_form;
153  
154 0 $c->stash(
155 template => 'form/auth/change_password.tt',
156 form => $form,
157 );
158 0 $form->process(
159 item_id => $c->user->id,
160 params => $c->request->parameters,
161 schema => $c->model('DBIC')->schema,
162 );
163  
164 0 ▶ return if !$form->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $form->is_valid 0 0 0
165  
166 0 $c->flash->{status_msg} = 'Password changed';
167 0 $c->response->redirect($c->uri_for('/account'));
168  
169 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 1 ▶sub email_reset_password_link : Global {
Subroutine email_reset_password_link: called CC=3 SCAR=18.9
Pod undocumented
172 1 my ($self, $c) = @_;
173  
174 1 my $form = $self->email_reset_password_link_form;
175 1 $c->stash(
176 template => 'form/auth/reset_password.tt',
177 form => $form,
178 );
179 1 $form->process(
180 params => $c->request->parameters,
181 schema => $c->model('DBIC')->schema,
182 );
183 1 ▶ return if !$form->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $form->is_valid 1 0 1
184  
185 # Get user based on email.
186 0 my $to_email = $form->field('email')->value;
187 0 my $user = $c->model('DBIC::Player')->find({ email => $to_email });
188  
189 # create and email password reset link
190 0 my $token = create_token($user->id);
191 0 ▶ my $create_token_coderef = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 $c->model('DBIC::Token')->create(
193 {
194 player => $user->id,
195 token => $token,
196 type => 'reset_password',
197 }
198 );
199 0 };
200 0 eval { $c->model('DBIC')->schema->txn_do($create_token_coderef); };
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
202 0 my $message = "Not able to create token\n";
203 0 warn $message . $@;
204 0 $c->flash->{status_msg} = $message;
205 }
206 else {
207 0 $c->forward($self->action_for('email_link'), [ $to_email, $token ]);
208 $c->flash->{status_msg} =
209 0 "A password reset </strong>link</strong> has been <strong>emailed to you.</strong>";
210 0 $c->response->redirect($c->uri_for('/message'));
211 }
212 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 1 ▶sub reset_password : Global {
Subroutine reset_password: called CC=5 SCAR=20.9
Pod undocumented
215 1 my ($self, $c) = @_;
216  
217 1 my $token = $c->request->query_parameters->{reset_password_token};
218 1 ▶ warn "TOKEN: $token\n" if debug;
Branch: 1 branch
BranchTrueFalseTotal
if debug() 0 1 1
219 1 my ($user_id) = $token =~ /_(\d+)$/;
220 1 ▶ warn "USER ID for RESET: $user_id\n" if debug;
Branch: 1 branch
BranchTrueFalseTotal
if debug() 0 1 1
221 1 my $token_row_object =
222 $c->model('DBIC::Token')
223 ->search({ player => $user_id, token => $token, type => 'reset_password' })->first;
224 1 ▶ if (!$token_row_object) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token_row_object) 1 0 1
225 1 $c->response->body("Token not found.");
226 1 $c->detach();
227 }
228  
229 0 my $form = $self->reset_password_form;
230 0 my $player_object = $token_row_object->player;
231 0 $c->stash(
232 template => 'form/auth/reset_password.tt',
233 form => $form,
234 );
235 0 $form->process(
236 item => $player_object,
237 params => $c->request->body_parameters,
238 schema => $c->model('DBIC')->schema,
239 );
240 0 ▶ return if !$form->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $form->is_valid 0 0 0
241  
242 0 $c->flash->{status_msg} = "Password has been reset.";
243 0 $c->response->redirect($c->uri_for('/login'));
244 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 0 ▶sub create_token {
Subroutine create_token: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my $user_id = shift;
248 0 return time . rand(10000) . "_${user_id}";
249 }
250  
251 0 ▶sub email_link : Private {
Subroutine email_link: not called CC=1 SCAR=0.5
Pod undocumented
252 0 my ($self, $c, $to_email, $token) = @_;
253  
254 4 ▶ use Email::Sender::Simple qw(try_to_sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
255 4 ▶ use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 4 ▶ use Email::Simple::Creator;
Subroutine BEGIN: called CC=1 SCAR=0.0
257 4 ▶ use Email::Sender::Transport::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 0 my $link = $c->request->base . 'reset_password?reset_password_token=' . $token;
260 0 my $admin_email = 'hunter@missoula.org';
261 0 my $subject = 'Reset password link';
262 0 my $message = <<"END_MESSAGE";
263 Use the following link to reset your password:
264 $link
265 END_MESSAGE
266  
267 0 my $email = Email::Simple->create(
268 header => [
269 To => $to_email,
270 From => $admin_email,
271 Subject => $subject,
272 ],
273 body => $message,
274 );
275 0 my $success = try_to_sendmail($email);
276 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 0 ▶sub message : Global {
Subroutine message: not called CC=1 SCAR=6.9
Pod undocumented
279 0 my ($self, $c) = @_;
280  
281 0 $c->stash->{template} = 'empty.tt';
282 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 4 1
« lib/Bracket/Form/Password/Change.pm ↑ Summary lib/Bracket/Form/Password/ResetEmail.pm »