Ticket #551: LDAP_Auth.php

File LDAP_Auth.php, 1.5 kB (added by fleximus, 8 months ago)

LDAP Auth library

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2
3class LDAP_Auth extends Auth_Core {
4
5    protected $ldap_version = 3;
6    protected $ldap_host    = 'localhost';
7    protected $ldap_base    = '';
8
9    public function protocol($version)
10    {
11        if ($version == 2 OR $version == 3)
12            $this->ldap_version = $version;
13    }
14
15    public function host($host)
16    {
17        $this->ldap_host = $host;
18    }
19
20    public function base($base)
21    {
22        $this->ldap_base = $base;
23    }
24
25    public function login($user, $password, $remember = FALSE)
26    {
27        if (empty($password))
28            return FALSE;
29
30        $user = 'cn='.$user.','.$this->ldap_base;
31
32
33        $ds = ldap_connect($this->ldap_host);
34        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $this->ldap_version);
35        $r  = @ldap_bind($ds, $user, $password);
36        $password = NULL;
37
38        // If the user has the "login" role and the passwords match, perform a login
39        if ($r)
40        {
41/* TODO
42            if ($remember == TRUE)
43            {
44                // Create a new autologin token
45                $token = new User_Token_Model;
46
47                // Set token data
48                $token->user_id = $user->id;
49                $token->expires = time() + $this->config['lifetime'];
50                $token->save();
51
52                // Set the autologin cookie
53                cookie::set('autologin', $token->token, $this->config['lifetime']);
54            }
55*/
56
57            // Finish the login
58            $this->complete_login($user);
59
60            return TRUE;
61        }
62
63        return FALSE;
64    }
65
66        protected function complete_login($user)
67        {
68                // Store session data
69                $this->session->set(array
70                (
71                        'username' => $user
72                ));
73        }
74
75} // End Auth