Ticket #551: LDAP.php

File LDAP.php, 3.2 kB (added by fleximus, 7 months ago)

LDAP Auth driver.

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2/**
3 * LDAP Auth driver.
4 *
5 * $Id$
6 *
7 * @package    Auth
8 * @author     Kohana Team
9 * @copyright  (c) 2007-2008 Kohana Team
10 * @license    http://kohanaphp.com/license.html
11 */
12class Auth_LDAP_Driver implements Auth_Driver {
13
14    protected $config;
15
16    // Session library
17    protected $session;
18
19    /**
20     * Constructor. Loads the Session instance.
21     *
22     * @return  void
23     */
24    public function __construct(array $config)
25    {
26        // Load config
27        $this->config = $config;
28
29        // Load libraries
30        $this->session = Session::instance();
31    }
32
33    public function logged_in($role)
34    {
35        $status = FALSE;
36
37        // Checks if a user is logged in and valid
38        if ( ! empty($_SESSION['auth_user']) AND is_object($_SESSION['auth_user'])
39            AND ($_SESSION['auth_user'] instanceof User_Model) AND $_SESSION['auth_user']->loaded())
40        {
41            $staus = TRUE;
42        }
43
44        return $status;
45    }
46
47    public function login($user, $password, $remember)
48    {
49        // Load the user
50        $user = 'cn='.$user.','.$this->config['base'];
51
52        // Connect to directory server
53                $ds = ldap_connect($this->config['server']);
54
55        if (!$ds) {
56            show_error('Connection error', "Can't connect to server $this->config['server']");
57        }
58
59        // Set LDAP options
60                ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']);
61
62
63        echo "user = $user / password = $password\n";
64
65        // Bind as the user
66                $r  = @ldap_bind($ds, $user, $password);
67
68        // Overwrite the password for security reasons
69                //$password = 'So Long, and Thanks for All the Fish!';
70
71        // If the passwords match, perform a login
72        if ($r) {
73            if ($remember === TRUE) {
74                // Create a new autologin token
75                $token = ORM::factory('user_token');
76
77                // Set token data
78                $token->user_id = $user->id;
79                $token->expires = time() + $this->config['lifetime'];
80                $token->save();
81
82                // Set the autologin cookie
83                cookie::set('authautologin', $token->token, $this->config['lifetime']);
84            }
85
86            // Run the standard completion
87            $this->complete_login($user);
88
89            return TRUE;
90        }
91
92        // Login failed
93        return FALSE;
94    }
95
96    public function force_login($user)
97    {
98        // Mark the session as forced, to prevent users from changing account information
99        $_SESSION['auth_forced'] = TRUE;
100
101        // Run the standard completion
102        $this->complete_login($user);
103    }
104
105    public function auto_login()
106    {
107        return FALSE;
108    }
109
110    public function logout($destroy)
111    {
112        // Delete the autologin cookie if it exists
113        cookie::get('authautologin') and cookie::delete('authautologin');
114
115        if ($destroy === TRUE)
116        {
117            // Destroy the session completely
118            Session::instance()->destroy();
119        }
120        else
121        {
122            // Remove the user object from the session
123            unset($_SESSION['auth_user']);
124
125            // Regenerate session_id
126            $this->session->regenerate();
127        }
128
129        // Double check
130        return ! isset($_SESSION['auth_user']);
131    }
132
133    public function password($user)
134    {
135        return NULL;
136    }
137
138    /**
139     * Complete the login for a user by setting the
140     * session data: user_id, username
141     *
142     * @param   object   user model object
143     * @return  void
144     */
145    protected function complete_login(User_Model $user)
146    {
147        // Regenerate session_id
148        $this->session->regenerate();
149
150        // Store session data
151        $_SESSION['auth_user'] = $user;
152    }
153
154} // End Auth_LDAP_Driver Class