Interchange6/Routes/Account.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 96 CC 10 SCAR 41.8
CC
10
Cov
0%
account_routes
110.0
BEGIN
2.0
BEGIN
2.0
CRAP 65.5
1 package Dancer::Plugin::Interchange6::Routes::Account;
2  
3 use strict;
4 use warnings;
5  
6 use Dancer ':syntax';
7 use Dancer::Plugin;
8 use Dancer::Plugin::Interchange6;
9 use Dancer::Plugin::Auth::Extensible;
10 use Try::Tiny;
11  
12 =head1 NAME
13  
14 Dancer::Plugin::Interchange6::Routes::Account - Account routes for Interchange6 Shop Machine
15  
16 =head1 DESCRIPTION
17  
18 The Interchange6 account routes module installs Dancer routes for
19 login and logout
20  
21 =cut
22  
23 register_hook 'before_login_display';
24  
25 =head1 FUNCTIONS
26  
27 =head2 account_routes
28  
29 Returns the account routes based on the passed routes configuration.
30  
31 =cut
32  
33 sub account_routes {
34 my $routes_config = shift;
35 my %routes;
36  
37 $routes{login}->{get} = sub {
38 return redirect '/' if logged_in_user;
39  
40 my %values;
41  
42 if ( vars->{login_failed} ) {
43 $values{error} = "Login failed";
44 }
45  
46 # record return_url in template tokens
47 if (my $return_url = param('return_url')) {
48 $values{return_url} = $return_url;
49 }
50  
51 # call before_login_display route so template tokens
52 # can be injected
53 execute_hook('before_login_display', \%values);
54  
55 # record return_url in the session to reuse it in post route
56 session return_url => $values{return_url};
57  
58 template $routes_config->{account}->{login}->{template}, \%values;
59 };
60  
61 $routes{login}->{post} = sub {
62 return redirect '/' if logged_in_user;
63  
64 my $login_route = '/' . $routes_config->{account}->{login}->{uri};
65  
66 my $user = shop_user->find({ username => params->{username}});
67  
68 my ($success, $realm, $current_cart);
69  
70 if ($user) {
71 # remember current cart object
72 $current_cart = shop_cart;
73  
74 ($success, $realm) = authenticate_user( params->{username}, params->{password} );
75 }
76  
77 if ($success) {
78 session logged_in_user => $user->username;
79 session logged_in_user_id => $user->id;
80 session logged_in_user_realm => $realm;
81  
82 if (! $current_cart->users_id) {
83 $current_cart->set_users_id($user->id);
84 }
85  
86 # now pull back in old cart items from previous authenticated
87 # sessions were sessions_id is undef in db cart
88 $current_cart->load_saved_products;
89  
90 if ( session('return_url') ) {
91 my $url = session('return_url');
92 session return_url => undef;
93 return redirect $url;
94 }
95 else {
96 return redirect '/'
97 . $routes_config->{account}->{login}->{success_uri};
98 }
99 } else {
100 debug "Authentication failed for ", params->{username};
101  
102 var login_failed => 1;
103 return forward $login_route, { return_url => params->{return_url} }, { method => 'get' };
104 }
105 };
106  
107 $routes{logout}->{any} = sub {
108 my $cart = shop_cart;
109 if ( $cart->count > 0 ) {
110 # save our items for next login
111 try {
112 $cart->set_sessions_id(undef);
113 }
114 catch {
115 warning "Failed to set sessions_id to undef for cart id: "
116 . $cart->id;
117 };
118 }
119 # any empty cart with sessions_id matching our session id will be
120 # destroyed here
121 session->destroy;
122 return redirect '/';
123 };
124  
125 return \%routes;
126 }
127  
128 true;