Catalyst RDBO Store for authentication.
[blog.pm-common-perl-mods.git] / Catalyst-Authentication-Store-RDBO / t / lib / TestApp.pm
blobeaea2b2aed752e7915dbb02c8be2bb212326a392
1 package TestApp;
3 use strict;
4 use Catalyst;
5 use Data::Dumper;
7 TestApp->config( $ENV{TESTAPP_CONFIG} );
9 TestApp->setup( @{$ENV{TESTAPP_PLUGINS}} );
11 sub user_login : Global {
12 my ( $self, $c ) = @_;
14 ## this allows anyone to login regardless of status.
15 $c->authenticate({ username => $c->request->params->{'username'},
16 password => $c->request->params->{'password'}
17 });
19 if ( $c->user_exists ) {
20 if ( $c->req->params->{detach} ) {
21 $c->detach( $c->req->params->{detach} );
23 $c->res->body( $c->user->get('username') . ' logged in' );
25 else {
26 $c->res->body( 'not logged in' );
30 sub notdisabled_login : Global {
31 my ( $self, $c ) = @_;
33 $c->authenticate({ username => $c->request->params->{'username'},
34 password => $c->request->params->{'password'},
35 status => [ 'active', 'registered' ]
36 });
38 if ( $c->user_exists ) {
39 if ( $c->req->params->{detach} ) {
40 $c->detach( $c->req->params->{detach} );
42 $c->res->body( $c->user->get('username') . ' logged in' );
44 else {
45 $c->res->body( 'not logged in' );
49 sub searchargs_login : Global {
50 my ( $self, $c ) = @_;
52 my $username = $c->request->params->{'username'} || '';
53 my $email = $c->request->params->{'email'} || '';
55 $c->authenticate({
56 password => $c->request->params->{'password'},
57 dbix_class => {
58 searchargs => [ { "-or" => [ username => $username,
59 email => $email ]},
60 { prefetch => qw/ map_user_role /}
63 });
65 if ( $c->user_exists ) {
66 if ( $c->req->params->{detach} ) {
67 $c->detach( $c->req->params->{detach} );
69 $c->res->body( $c->user->get('username') . ' logged in' );
71 else {
72 $c->res->body( 'not logged in' );
76 sub resultset_login : Global {
77 my ( $self, $c ) = @_;
79 my $username = $c->request->params->{'username'} || '';
80 my $email = $c->request->params->{'email'} || '';
83 my $object = $c->model( 'User' )->search(
84 query => [
85 or => [
86 username => $username,
87 email => $email
90 limit => 1
91 )->[ 0 ];
93 $c->authenticate({
94 password => $c->request->params->{'password'},
95 rdbo => { object => $object }
96 });
98 if ( $c->user_exists ) {
99 if ( $c->req->params->{detach} ) {
100 $c->detach( $c->req->params->{detach} );
102 $c->res->body( $c->user->get('username') . ' logged in' );
104 else {
105 $c->res->body( 'not logged in' );
109 ## need to add a resultset login test and a search args login test
111 sub user_logout : Global {
112 my ( $self, $c ) = @_;
114 $c->logout;
116 if ( ! $c->user ) {
117 $c->res->body( 'logged out' );
119 else {
120 $c->res->body( 'not logged ok' );
124 sub get_session_user : Global {
125 my ( $self, $c ) = @_;
127 if ( $c->user_exists ) {
128 $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) );
132 sub is_admin : Global {
133 my ( $self, $c ) = @_;
135 eval {
136 if ( $c->assert_user_roles( qw/admin/ ) ) {
137 $c->res->body( 'ok' );
140 if ($@) {
141 $c->res->body( 'failed' );
145 sub is_admin_user : Global {
146 my ( $self, $c ) = @_;
148 eval {
149 if ( $c->assert_user_roles( qw/admin user/ ) ) {
150 $c->res->body( 'ok' );
153 if ($@) {
154 $c->res->body( 'failed' );
158 sub set_usersession : Global {
159 my ( $self, $c, $value ) = @_;
160 $c->user_session->{foo} = $value;
161 $c->res->body( 'ok' );
164 sub get_usersession : Global {
165 my ( $self, $c ) = @_;
166 $c->res->body( $c->user_session->{foo} || '' );