1 package TestApp::Controller::Root;
5 BEGIN { extends 'Catalyst::Controller' }
7 __PACKAGE__->config(namespace => '');
9 sub user_login : Global {
10 my ( $self, $c ) = @_;
12 ## this allows anyone to login regardless of status.
14 $c->authenticate({ username => $c->request->params->{'username'},
15 password => $c->request->params->{'password'}
19 return $c->res->body($@);
22 if ( $c->user_exists ) {
23 if ( $c->req->params->{detach} ) {
24 $c->detach( $c->req->params->{detach} );
26 $c->res->body( $c->user->get('username') . ' logged in' );
29 $c->res->body( 'not logged in' );
34 sub notdisabled_login : Global {
35 my ( $self, $c ) = @_;
37 $c->authenticate({ username => $c->request->params->{'username'},
38 password => $c->request->params->{'password'},
39 status => [ 'active', 'registered' ]
42 if ( $c->user_exists ) {
43 if ( $c->req->params->{detach} ) {
44 $c->detach( $c->req->params->{detach} );
46 $c->res->body( $c->user->get('username') . ' logged in' );
49 $c->res->body( 'not logged in' );
53 sub searchargs_login : Global {
54 my ( $self, $c ) = @_;
56 my $username = $c->request->params->{'username'} || '';
57 my $email = $c->request->params->{'email'} || '';
60 password => $c->request->params->{'password'},
62 searchargs => [ { "-or" => [ username => $username,
64 { prefetch => qw/ map_user_role /}
69 if ( $c->user_exists ) {
70 if ( $c->req->params->{detach} ) {
71 $c->detach( $c->req->params->{detach} );
73 $c->res->body( $c->user->get('username') . ' logged in' );
76 $c->res->body( 'not logged in' );
80 sub result_login : Global {
81 my ($self, $ctx) = @_;
83 my $user = $ctx->model('TestApp::User')->find({
84 email => $ctx->request->params->{email},
87 if ($user->password_accessor ne $ctx->request->params->{password}) {
88 $ctx->response->status(403);
89 $ctx->response->body('password mismatch');
94 dbix_class => { result => $user },
95 password => $ctx->request->params->{password},
98 if ($ctx->user_exists) {
99 $ctx->res->body( $ctx->user->get('username') . ' logged in' );
102 $ctx->res->body('not logged in');
106 sub resultset_login : Global {
107 my ( $self, $c ) = @_;
109 my $username = $c->request->params->{'username'} || '';
110 my $email = $c->request->params->{'email'} || '';
113 my $rs = $c->model('TestApp::User')->search({ "-or" => [ username => $username,
117 password => $c->request->params->{'password'},
118 dbix_class => { resultset => $rs }
121 if ( $c->user_exists ) {
122 if ( $c->req->params->{detach} ) {
123 $c->detach( $c->req->params->{detach} );
125 $c->res->body( $c->user->get('username') . ' logged in' );
128 $c->res->body( 'not logged in' );
132 sub bad_login : Global {
133 my ( $self, $c ) = @_;
135 ## this allows anyone to login regardless of status.
137 $c->authenticate({ william => $c->request->params->{'username'},
138 the_bum => $c->request->params->{'password'}
142 return $c->res->body($@);
145 if ( $c->user_exists ) {
146 if ( $c->req->params->{detach} ) {
147 $c->detach( $c->req->params->{detach} );
149 $c->res->body( $c->user->get('username') . ' logged in' );
152 $c->res->body( 'not logged in' );
156 ## need to add a resultset login test and a search args login test
158 sub user_logout : Global {
159 my ( $self, $c ) = @_;
164 $c->res->body( 'logged out' );
167 $c->res->body( 'not logged ok' );
171 sub get_session_user : Global {
172 my ( $self, $c ) = @_;
174 if ( $c->user_exists ) {
175 $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) );
179 sub is_admin : Global {
180 my ( $self, $c ) = @_;
183 if ( $c->assert_user_roles( qw/admin/ ) ) {
184 $c->res->body( 'ok' );
188 $c->res->body( 'failed' );
192 sub is_admin_user : Global {
193 my ( $self, $c ) = @_;
196 if ( $c->assert_user_roles( qw/admin user/ ) ) {
197 $c->res->body( 'ok' );
201 $c->res->body( 'failed' );
205 sub is_any_admin_user : Global {
206 my ( $self, $c ) = @_;
209 if ( $c->assert_any_user_role( qw/admin user/ ) ) {
210 $c->res->body( 'ok' );
214 $c->res->body( 'failed' );
218 sub is_nonexistant_roles: Global {
219 my ( $self, $c ) = @_;
222 if ( $c->assert_user_roles( qw/madeUProle baconHater/ ) ) {
223 $c->res->body( 'ok' );
227 $c->res->body( 'failed' );
231 sub is_any_nonexistant_role: Global {
232 my ( $self, $c ) = @_;
235 if ( $c->assert_any_user_role( qw/madeUProle baconHater/ ) ) {
236 $c->res->body( 'ok' );
240 $c->res->body( 'failed' );
245 sub set_usersession : Global {
246 my ( $self, $c, $value ) = @_;
247 $c->user_session->{foo} = $value;
248 $c->res->body( 'ok' );
251 sub get_usersession : Global {
252 my ( $self, $c ) = @_;
253 $c->res->body( $c->user_session->{foo} || '' );
256 __PACKAGE__->meta->make_immutable;