datamapper /examples/application/libraries/login_manager.php

Language PHP Lines 120
MD5 Hash 0a909c4e8c53d991e81f2d86e0e12300 Estimated Cost $2,181 (why?)
Repository https://bitbucket.org/ramo_carlo/datamapper View Raw File View Project SPDX
  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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

/**
 * Simple utility class to handle logins.
 */
class Login_Manager {
	
	var $logged_in_user = NULL;
	
	function __construct($params = array())
	{
		
		$this->CI =& get_instance(); 
		$this->session =& $this->CI->session;
		
		if( ! isset($params['autologin']) || $params['autologin'] !== FALSE)
		{
			$required_group = -1;
			if(isset($params['required_group']))
			{
				$required_group = $params['required_group'];
			}
			$this->check_login($required_group);
		}
	}
	
	function check_login($required_group = -1)
	{
		// Special auto-setup routine
		if( ! $this->CI->db->table_exists('users'))
		{
			redirect('admin/reset_warning');
		}
		else
		{
			// see if there are any users in the system
			$u = new User();
			if($u->count() == 0)
			{
				redirect('admin/init');
			}
		}
		// if not logged in, automatically redirect
		$u = $this->get_user();
		if($u === FALSE)
		{
			$this->session->set_userdata('login_redirect', uri_string());
			redirect('login');
		}
		if($required_group > 0)
		{
			if($u->group->id > $required_group)
			{
				show_error('You do not have access to this section.');
			}
		}
	}
	
	/**
	 * process_login
	 * Validates that a username and password are correct.
	 * 
	 * @param object $user The user containing the login information.
	 * @return FALSE if invalid, TRUE or a redirect string if valid. 
	 */
	function process_login($user)
	{
		// attempt the login
		$success = $user->login();
		if($success)
		{
			// store the userid if the login was successful
			$this->session->set_userdata('logged_in_id', $user->id);
			// store the user for this request
			$this->logged_in_user = $user;
			// if a redirect is necessary, return it.
			$redirect = $this->session->userdata('login_redirect');
			if( ! empty($redirect))
			{
				$success = $redirect;
			}
		}
		return $success;
	}
	
	function logout()
	{
		$this->session->sess_destroy();
		$this->logged_in_user = NULL;
	}
	
	function get_user()
	{
		if(is_null($this->logged_in_user))
		{
			if( ! $this->CI->db->table_exists('users'))
			{
				return FALSE;
			}
			$id = $this->session->userdata('logged_in_id');
			if(is_numeric($id))
			{
				$u = new User();
				$u->get_by_id($id);
				if($u->exists()) {
					$u->group->get();
					$this->logged_in_user = $u;
					return $this->logged_in_user;
				}
			}
			return FALSE;
		}
		else
		{
			return $this->logged_in_user;
		}
	}
	
}
Back to Top