Commit | Line | Data |
d1710b0f |
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 | ## need to add a resultset login test and a search args login test |
109 | |
110 | sub user_logout : Global { |
111 | my ( $self, $c ) = @_; |
112 | |
113 | $c->logout; |
114 | |
115 | if ( ! $c->user ) { |
116 | $c->res->body( 'logged out' ); |
117 | } |
118 | else { |
119 | $c->res->body( 'not logged ok' ); |
120 | } |
121 | } |
122 | |
123 | sub get_session_user : Global { |
124 | my ( $self, $c ) = @_; |
125 | |
126 | if ( $c->user_exists ) { |
127 | $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) ); |
128 | } |
129 | } |
130 | |
131 | sub is_admin : Global { |
132 | my ( $self, $c ) = @_; |
133 | |
134 | eval { |
135 | if ( $c->assert_user_roles( qw/admin/ ) ) { |
136 | $c->res->body( 'ok' ); |
137 | } |
138 | }; |
139 | if ($@) { |
140 | $c->res->body( 'failed' ); |
141 | } |
142 | } |
143 | |
144 | sub is_admin_user : Global { |
145 | my ( $self, $c ) = @_; |
146 | |
147 | eval { |
148 | if ( $c->assert_user_roles( qw/admin user/ ) ) { |
149 | $c->res->body( 'ok' ); |
150 | } |
151 | }; |
152 | if ($@) { |
153 | $c->res->body( 'failed' ); |
154 | } |
155 | } |
156 | |
157 | sub set_usersession : Global { |
158 | my ( $self, $c, $value ) = @_; |
159 | $c->user_session->{foo} = $value; |
160 | $c->res->body( 'ok' ); |
161 | } |
162 | |
163 | sub get_usersession : Global { |
164 | my ( $self, $c ) = @_; |
165 | $c->res->body( $c->user_session->{foo} || '' ); |
166 | } |
167 | |
168 | |
169 | 1; |