Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Current_User {
- /** Returns the User object for the logged in user. */
- private static $user;
- private function __construct() {}
- public static function user() {
- if(!isset(self::$user)) {
- // we use the $CI object to load the session library because this is a static class.
- $CI =& get_instance();
- if (!$user_id = $CI->session->userdata('user_id')) {
- return FALSE;
- }
- if (!$u = Doctrine::getTable('User')->find($user_id)) {
- return FALSE;
- }
- self::$user = $u;
- }
- return self::$user;
- }
- public static function login($username, $password) {
- // get User object by username
- if ($u = Doctrine::getTable('User')->findOneByUsername($username)) {
- // this mutates (encrypts) the input password
- $u_input = new User();
- $u_input->password = $password;
- // password match (comparing encrypted passwords)
- if ($u->password == $u_input->password) {
- unset($u_input);
- $CI =& get_instance();
- $CI->load->library('session');
- $CI->session->set_userdata('user_id',$u->id);
- self::$user = $u;
- return TRUE;
- }
- unset($u_input);
- }
- // login failed
- return FALSE;
- }
- public function __clone() {
- trigger_error('Clone is not allowed.', E_USER_ERROR);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement