summaryrefslogtreecommitdiffstats
path: root/application/libraries/Duser/Duser.php
blob: 733616b409ceaac456df135db32e4cbfc194f707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
 * Copyright 2013 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

abstract class Duser_Driver extends CI_Driver {

	// List of optional functions or function groups 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 returned array should contain the following keys:
	 *  - username string
	 *  - 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', 'duser_ldap'
	);

	function __construct()
	{
		$CI =& get_instance();

		$this->_adapter = $CI->config->item("authentication_driver");
	}

	// 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"]);
		$CI->session->set_userdata('access_level', 'full');

		return true;
	}

	public function username_exists($username)
	{
		$this->require_implemented(__FUNCTION__);

		if ($username === false) {
			return false;
		}

		return $this->{$this->_adapter}->username_exists($username);
	}

	public function get_email($userid)
	{
		$this->require_implemented(__FUNCTION__);

		return $this->{$this->_adapter}->get_email($userid);
	}
}