Ticket #551: ldap_auth.php

File ldap_auth.php, 1.5 kB (added by fleximus, 9 months ago)

Demo controller

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2/**
3 * LDAP Auth module demo controller. This controller should NOT be used in production.
4 * It is for demonstration purposes only!
5 *
6 * $Id$
7 *
8 * @package    Auth
9 * @author     Kohana Team
10 * @copyright  (c) 2007-2008 Kohana Team
11 * @license    http://kohanaphp.com/license.html
12 */
13class LDAP_Auth_Controller extends Auth_Controller {
14
15    public function __construct()
16    {
17        parent::__construct();
18
19        // Load auth library
20        $this->auth = new LDAP_Auth();
21
22        $this->auth->host('openldap.localdomain');
23        $this->auth->base('ou=users,dc=example,dc=com');
24    }
25
26    public function index()
27    {
28        echo '<h1>LDAP Auth Demo</h1>';
29        echo '<p>Go to your login page!</p>';
30    }
31
32    public function login()
33    {
34        if (Session::instance()->get('user_id'))
35        {
36            $form = new Forge('auth/logout', 'Log Out');
37
38            $form->submit('Logout Now');
39        }
40        else
41        {
42            $form = new Forge(NULL, 'LDAP User Login');
43
44            $form->input('username')->label(TRUE)->rules('required|length[4,32]');
45            $form->password('password')->label(TRUE)->rules('required|length[4,16]');
46            $form->submit('Attempt Login');
47
48            if ($form->validate())
49            {
50                // Attempt a login
51                if ($this->auth->login($form->username->value, $form->password->value))
52                {
53                    echo '<h4>Login Success!</h4>';
54                    return;
55                }
56                else
57                {
58                    $form->password->add_error('login_failed', 'Invalid username or password.');
59                }
60            }
61        }
62
63        // Display the form
64        echo $form->html();
65    }
66
67} // End LDAP_Auth Demo Controller