From f8ac2f6582001bfa3b42ac4fbdc77ff97137a8f8 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 30 Jan 2013 22:31:11 +0100 Subject: Modularize authentication system This allows to easily add LDAP and other support. Signed-off-by: Florian Pritz --- application/libraries/Duser/Duser.php | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 application/libraries/Duser/Duser.php (limited to 'application/libraries/Duser/Duser.php') diff --git a/application/libraries/Duser/Duser.php b/application/libraries/Duser/Duser.php new file mode 100644 index 000000000..c06f4f723 --- /dev/null +++ b/application/libraries/Duser/Duser.php @@ -0,0 +1,94 @@ + + * + * Licensed under GPLv3 + * (see COPYING for full license text) + * + */ + +abstract class Duser_Driver extends CI_Driver { + + // List of optional functions or function group that are implemented + // + // Possible values are names of functions already implemented in this + // abstract class or the function groups listed below. + // + // Possible function groups are: + // - can_register_new_users + // - can_reset_password + public $optional_functions = array(); + + /* + * The array should contain the following keys: + * - username VARCHAR + * - userid INT > 0 + * + * @return mixed array on success, false on failure + */ + abstract public function login($username, $password); + + public function username_exists($username) { + return false; + } +} + +class Duser extends CI_Driver_Library { + + protected $_adapter = null; + + protected $valid_drivers = array( + 'duser_db' + ); + + function __construct() + { + // TODO: read config + $this->_adapter = 'db'; + } + + // require an optional function to be implemented + public function require_implemented($function) { + if (!$this->is_implemented($function)) { + show_error("" + ."Optional function '".$function."' not implemented in user adapter '".$this->_adapter."'. " + ."Requested functionally unavailable."); + } + } + + // check if an optional function is implemented + public function is_implemented($function) { + if (in_array($function, $this->{$this->_adapter}->optional_functions)) { + return true; + } + + return false; + } + + public function login($username, $password) + { + $login_info = $this->{$this->_adapter}->login($username, $password); + if ($login_info === false) { + return false; + } + + $CI =& get_instance(); + + $CI->session->set_userdata('logged_in', true); + $CI->session->set_userdata('username', $login_info["username"]); + $CI->session->set_userdata('userid', $login_info["userid"]); + + return true; + } + + public function username_exists($username) + { + $this->require_implemented(__FUNCTION__); + + if ($username === false) { + return false; + } + + return $this->{$this->_adapter}->username_exists($username); + } +} -- cgit v1.2.3-24-g4f1b