| 1 | <?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Auth_LDAP_Driver implements Auth_Driver { |
|---|
| 13 | |
|---|
| 14 | protected $config; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | protected $session; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | public function __construct(array $config) |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | $this->config = $config; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | $this->session = Session::instance(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | public function logged_in($role) |
|---|
| 34 | { |
|---|
| 35 | $status = FALSE; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 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 | |
|---|
| 50 | $user = 'cn='.$user.','.$this->config['base']; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 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 | |
|---|
| 60 | ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | echo "user = $user / password = $password\n"; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | $r = @ldap_bind($ds, $user, $password); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | if ($r) { |
|---|
| 73 | if ($remember === TRUE) { |
|---|
| 74 | |
|---|
| 75 | $token = ORM::factory('user_token'); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | $token->user_id = $user->id; |
|---|
| 79 | $token->expires = time() + $this->config['lifetime']; |
|---|
| 80 | $token->save(); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | cookie::set('authautologin', $token->token, $this->config['lifetime']); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | $this->complete_login($user); |
|---|
| 88 | |
|---|
| 89 | return TRUE; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | return FALSE; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public function force_login($user) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | $_SESSION['auth_forced'] = TRUE; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 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 | |
|---|
| 113 | cookie::get('authautologin') and cookie::delete('authautologin'); |
|---|
| 114 | |
|---|
| 115 | if ($destroy === TRUE) |
|---|
| 116 | { |
|---|
| 117 | |
|---|
| 118 | Session::instance()->destroy(); |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | |
|---|
| 123 | unset($_SESSION['auth_user']); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | $this->session->regenerate(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | return ! isset($_SESSION['auth_user']); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | public function password($user) |
|---|
| 134 | { |
|---|
| 135 | return NULL; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | protected function complete_login(User_Model $user) |
|---|
| 146 | { |
|---|
| 147 | |
|---|
| 148 | $this->session->regenerate(); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | $_SESSION['auth_user'] = $user; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | } |
|---|