Committed missed changes from 0.1082
[catagits/Catalyst-Authentication-Store-DBIx-Class.git] / t / lib / TestApp.pm
1 package TestApp;
2
3 use strict;
4 use Catalyst;
5 use Data::Dumper;
6
7 TestApp->config( $ENV{TESTAPP_CONFIG} );
8
9 TestApp->setup( @{$ENV{TESTAPP_PLUGINS}} );
10
11 sub user_login : Global {
12     my ( $self, $c ) = @_;
13
14     ## this allows anyone to login regardless of status.
15     eval {
16         $c->authenticate({ username => $c->request->params->{'username'},
17                            password => $c->request->params->{'password'}
18                          });
19         1;
20     } or do {
21         return $c->res->body($@);
22     };
23
24     if ( $c->user_exists ) {
25         if ( $c->req->params->{detach} ) {
26             $c->detach( $c->req->params->{detach} );
27         }
28         $c->res->body( $c->user->get('username') . ' logged in' );
29     }
30     else {
31         $c->res->body( 'not logged in' );
32     }
33 }
34
35
36 sub notdisabled_login : Global {
37     my ( $self, $c ) = @_;
38
39     $c->authenticate({ username => $c->request->params->{'username'},
40                        password => $c->request->params->{'password'},
41                        status => [ 'active', 'registered' ]
42                      });
43
44     if ( $c->user_exists ) {
45         if ( $c->req->params->{detach} ) {
46             $c->detach( $c->req->params->{detach} );
47         }
48         $c->res->body( $c->user->get('username') .  ' logged in' );
49     }
50     else {
51         $c->res->body( 'not logged in' );
52     }
53 }
54
55 sub searchargs_login : Global {
56     my ( $self, $c ) = @_;
57
58     my $username = $c->request->params->{'username'} || '';
59     my $email = $c->request->params->{'email'} || '';
60     
61     $c->authenticate({ 
62                         password => $c->request->params->{'password'},
63                         dbix_class => {
64                             searchargs => [ { "-or" => [ username => $username,
65                                                         email => $email ]}, 
66                                             { prefetch => qw/ map_user_role /}
67                                           ]
68                         }
69                      });
70
71     if ( $c->user_exists ) {
72         if ( $c->req->params->{detach} ) {
73             $c->detach( $c->req->params->{detach} );
74         }
75         $c->res->body( $c->user->get('username') .  ' logged in' );
76     }
77     else {
78         $c->res->body( 'not logged in' );
79     }
80 }
81
82 sub resultset_login : Global {
83     my ( $self, $c ) = @_;
84
85     my $username = $c->request->params->{'username'} || '';
86     my $email = $c->request->params->{'email'} || '';
87     
88     
89     my $rs = $c->model('TestApp::User')->search({ "-or" => [ username => $username,
90                                                               email => $email ]});
91     
92     $c->authenticate({ 
93                         password => $c->request->params->{'password'},
94                         dbix_class => { resultset => $rs }
95                      });
96                      
97     if ( $c->user_exists ) {
98         if ( $c->req->params->{detach} ) {
99             $c->detach( $c->req->params->{detach} );
100         }
101         $c->res->body( $c->user->get('username') .  ' logged in' );
102     }
103     else {
104         $c->res->body( 'not logged in' );
105     }
106 }
107
108 sub bad_login : Global {
109     my ( $self, $c ) = @_;
110
111     ## this allows anyone to login regardless of status.
112     eval {
113         $c->authenticate({ william => $c->request->params->{'username'},
114                            the_bum => $c->request->params->{'password'}
115                          });
116         1;
117     } or do {
118         return $c->res->body($@);
119     };
120
121     if ( $c->user_exists ) {
122         if ( $c->req->params->{detach} ) {
123             $c->detach( $c->req->params->{detach} );
124         }
125         $c->res->body( $c->user->get('username') . ' logged in' );
126     }
127     else {
128         $c->res->body( 'not logged in' );
129     }
130 }
131
132 ## need to add a resultset login test and a search args login test
133
134 sub user_logout : Global {
135     my ( $self, $c ) = @_;
136
137     $c->logout;
138
139     if ( ! $c->user ) {
140         $c->res->body( 'logged out' );
141     }
142     else {
143         $c->res->body( 'not logged ok' );
144     }
145 }
146
147 sub get_session_user : Global {
148     my ( $self, $c ) = @_;
149  
150     if ( $c->user_exists ) {
151         $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) );
152     }
153 }
154
155 sub is_admin : Global {
156     my ( $self, $c ) = @_;
157
158     eval {
159         if ( $c->assert_user_roles( qw/admin/ ) ) {
160             $c->res->body( 'ok' );
161         }
162     };
163     if ($@) {
164         $c->res->body( 'failed' );
165     }
166 }
167
168 sub is_admin_user : Global {
169     my ( $self, $c ) = @_;
170
171     eval {
172         if ( $c->assert_user_roles( qw/admin user/ ) ) {
173             $c->res->body( 'ok' );
174         }
175     };
176     if ($@) {
177         $c->res->body( 'failed' );
178     }
179 }
180
181 sub set_usersession : Global {
182     my ( $self, $c, $value ) = @_;
183     $c->user_session->{foo} = $value;
184     $c->res->body( 'ok' );
185 }
186
187 sub get_usersession : Global {
188     my ( $self, $c ) = @_;
189     $c->res->body( $c->user_session->{foo} || '' );
190 }
191
192
193 1;