More moves / cleanups
[catagits/Catalyst-Authentication-Store-LDAP.git] / t / lib / Test / LDAP / Controller / Auth.pm
1 package Test::LDAP::Controller::Auth;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN {extends 'Catalyst::Controller'; }
6
7 sub login :Path("login") :Args(0) {
8     my ( $self, $c ) = @_;
9
10     my $user = $c->req->params->{'username'};
11     my $pass = $c->req->params->{'password'};
12
13
14     if ($c->user_exists) {
15         $c->response->redirect( $c->uri_for( $c->controller("Auth")->action_for("index") ) )
16     }
17
18     # Got username / pass?
19     if ( defined ($user) && defined ($pass) ) {
20         # Let's auth
21         my $auth = $c->authenticate (
22             { username => $user, password => $pass }, "ldap"
23         );
24
25         # Let's check if we are authed, if we are then we forward to the index.
26         # Else we'll throw an error into message and display the login page
27         if ($auth) {
28             $c->response->redirect( $c->uri_for( $c->controller("Root")->action_for("index") ) );;
29
30             # Since we got auth, let's bind with the model with a dn & pass as well.
31             #$c->model('LDAP')->bind (dn => $
32         } else {
33             $c->res->body("Bad user/password")
34         }
35
36     } else {
37         # If not throw a message c
38         $c->res->body("Missing credentials");
39     }
40 }
41
42 sub logout : Path("logout") {
43     my ( $self, $c ) = @_;
44
45     if ($c->user_exists) {
46         $c->logout;
47     }
48     $c->response->redirect( $c->uri_for( $c->controller("Root")->action_for("index") ) )
49 }
50
51 __PACKAGE__->meta->make_immutable;
52
53 1;