Commit | Line | Data |
5da987e8 |
1 | package TestApp::Controller::Root; |
2 | |
3 | use Moose; |
4 | |
5 | BEGIN { extends 'Catalyst::Controller' } |
6 | |
7 | __PACKAGE__->config(namespace => ''); |
8 | |
9 | sub user_login : Global { |
10 | my ( $self, $c ) = @_; |
11 | |
12 | ## this allows anyone to login regardless of status. |
13 | eval { |
14 | $c->authenticate({ username => $c->request->params->{'username'}, |
15 | password => $c->request->params->{'password'} |
16 | }); |
17 | 1; |
18 | } or do { |
19 | return $c->res->body($@); |
20 | }; |
21 | |
22 | if ( $c->user_exists ) { |
23 | if ( $c->req->params->{detach} ) { |
24 | $c->detach( $c->req->params->{detach} ); |
25 | } |
26 | $c->res->body( $c->user->get('username') . ' logged in' ); |
27 | } |
28 | else { |
29 | $c->res->body( 'not logged in' ); |
30 | } |
31 | } |
32 | |
33 | |
34 | sub notdisabled_login : Global { |
35 | my ( $self, $c ) = @_; |
36 | |
37 | $c->authenticate({ username => $c->request->params->{'username'}, |
38 | password => $c->request->params->{'password'}, |
39 | status => [ 'active', 'registered' ] |
40 | }); |
41 | |
42 | if ( $c->user_exists ) { |
43 | if ( $c->req->params->{detach} ) { |
44 | $c->detach( $c->req->params->{detach} ); |
45 | } |
46 | $c->res->body( $c->user->get('username') . ' logged in' ); |
47 | } |
48 | else { |
49 | $c->res->body( 'not logged in' ); |
50 | } |
51 | } |
52 | |
53 | sub searchargs_login : Global { |
54 | my ( $self, $c ) = @_; |
55 | |
56 | my $username = $c->request->params->{'username'} || ''; |
57 | my $email = $c->request->params->{'email'} || ''; |
58 | |
59 | $c->authenticate({ |
60 | password => $c->request->params->{'password'}, |
61 | dbix_class => { |
62 | searchargs => [ { "-or" => [ username => $username, |
63 | email => $email ]}, |
64 | { prefetch => qw/ map_user_role /} |
65 | ] |
66 | } |
67 | }); |
68 | |
69 | if ( $c->user_exists ) { |
70 | if ( $c->req->params->{detach} ) { |
71 | $c->detach( $c->req->params->{detach} ); |
72 | } |
73 | $c->res->body( $c->user->get('username') . ' logged in' ); |
74 | } |
75 | else { |
76 | $c->res->body( 'not logged in' ); |
77 | } |
78 | } |
79 | |
6bd97524 |
80 | sub result_login : Global { |
81 | my ($self, $ctx) = @_; |
82 | |
83 | my $user = $ctx->model('TestApp::User')->find({ |
84 | email => $ctx->request->params->{email}, |
85 | }); |
86 | |
87 | if ($user->password_accessor ne $ctx->request->params->{password}) { |
88 | $ctx->response->status(403); |
89 | $ctx->response->body('password mismatch'); |
90 | $ctx->detach; |
91 | } |
92 | |
93 | $ctx->authenticate({ |
94 | dbix_class => { result => $user }, |
95 | password => $ctx->request->params->{password}, |
96 | }); |
97 | |
98 | if ($ctx->user_exists) { |
99 | $ctx->res->body( $ctx->user->get('username') . ' logged in' ); |
100 | } |
101 | else { |
102 | $ctx->res->body('not logged in'); |
103 | } |
104 | } |
105 | |
5da987e8 |
106 | sub resultset_login : Global { |
107 | my ( $self, $c ) = @_; |
108 | |
109 | my $username = $c->request->params->{'username'} || ''; |
110 | my $email = $c->request->params->{'email'} || ''; |
111 | |
112 | |
113 | my $rs = $c->model('TestApp::User')->search({ "-or" => [ username => $username, |
114 | email => $email ]}); |
115 | |
116 | $c->authenticate({ |
117 | password => $c->request->params->{'password'}, |
118 | dbix_class => { resultset => $rs } |
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 | sub bad_login : Global { |
133 | my ( $self, $c ) = @_; |
134 | |
135 | ## this allows anyone to login regardless of status. |
136 | eval { |
137 | $c->authenticate({ william => $c->request->params->{'username'}, |
138 | the_bum => $c->request->params->{'password'} |
139 | }); |
140 | 1; |
141 | } or do { |
142 | return $c->res->body($@); |
143 | }; |
144 | |
145 | if ( $c->user_exists ) { |
146 | if ( $c->req->params->{detach} ) { |
147 | $c->detach( $c->req->params->{detach} ); |
148 | } |
149 | $c->res->body( $c->user->get('username') . ' logged in' ); |
150 | } |
151 | else { |
152 | $c->res->body( 'not logged in' ); |
153 | } |
154 | } |
155 | |
156 | ## need to add a resultset login test and a search args login test |
157 | |
158 | sub user_logout : Global { |
159 | my ( $self, $c ) = @_; |
160 | |
161 | $c->logout; |
162 | |
163 | if ( ! $c->user ) { |
164 | $c->res->body( 'logged out' ); |
165 | } |
166 | else { |
167 | $c->res->body( 'not logged ok' ); |
168 | } |
169 | } |
170 | |
171 | sub get_session_user : Global { |
172 | my ( $self, $c ) = @_; |
173 | |
174 | if ( $c->user_exists ) { |
175 | $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) ); |
176 | } |
177 | } |
178 | |
179 | sub is_admin : Global { |
180 | my ( $self, $c ) = @_; |
181 | |
182 | eval { |
183 | if ( $c->assert_user_roles( qw/admin/ ) ) { |
184 | $c->res->body( 'ok' ); |
185 | } |
186 | }; |
187 | if ($@) { |
188 | $c->res->body( 'failed' ); |
189 | } |
190 | } |
191 | |
192 | sub is_admin_user : Global { |
193 | my ( $self, $c ) = @_; |
194 | |
195 | eval { |
196 | if ( $c->assert_user_roles( qw/admin user/ ) ) { |
197 | $c->res->body( 'ok' ); |
198 | } |
199 | }; |
200 | if ($@) { |
201 | $c->res->body( 'failed' ); |
202 | } |
203 | } |
204 | |
b3c995e9 |
205 | sub is_any_admin_user : Global { |
206 | my ( $self, $c ) = @_; |
207 | |
208 | eval { |
209 | if ( $c->assert_any_user_role( qw/admin user/ ) ) { |
210 | $c->res->body( 'ok' ); |
211 | } |
212 | }; |
213 | if ($@) { |
214 | $c->res->body( 'failed' ); |
215 | } |
216 | } |
217 | |
218 | sub is_nonexistant_roles: Global { |
219 | my ( $self, $c ) = @_; |
220 | |
221 | eval { |
222 | if ( $c->assert_user_roles( qw/madeUProle baconHater/ ) ) { |
223 | $c->res->body( 'ok' ); |
224 | } |
225 | }; |
226 | if ($@) { |
227 | $c->res->body( 'failed' ); |
228 | } |
229 | } |
230 | |
231 | sub is_any_nonexistant_role: Global { |
232 | my ( $self, $c ) = @_; |
233 | |
234 | eval { |
235 | if ( $c->assert_any_user_role( qw/madeUProle baconHater/ ) ) { |
236 | $c->res->body( 'ok' ); |
237 | } |
238 | }; |
239 | if ($@) { |
240 | $c->res->body( 'failed' ); |
241 | } |
242 | } |
243 | |
244 | |
5da987e8 |
245 | sub set_usersession : Global { |
246 | my ( $self, $c, $value ) = @_; |
247 | $c->user_session->{foo} = $value; |
248 | $c->res->body( 'ok' ); |
249 | } |
250 | |
251 | sub get_usersession : Global { |
252 | my ( $self, $c ) = @_; |
253 | $c->res->body( $c->user_session->{foo} || '' ); |
254 | } |
255 | |
256 | __PACKAGE__->meta->make_immutable; |
257 | |
258 | 1; |