<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class extends Auth_ORM to enable external authentication
 *
 * @package    OAuth
 * @category   Classes
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class Auth_ORM_External extends Auth_ORM {
	// Our Authentication Object
	protected $ao;
	// Our OAuth Model Object
	protected $oo;

	public function __construct(Model_Oauth $oo) {
		parent::__construct((array)Kohana::$config->load('auth'));

		$this->oo = $oo;
	}

	protected function _login($user,$password,$remember) {
		$this->complete_login($user);

		if ($remember) {
			$aoo = $this->oo->account_oauth->where('account_id','=',$user->id)->find();

			// Record our user in the DB
			$aoo->account_id = $user->id;
			$aoo->oauth_id = $this->oo->id;
			$aoo->userid = $remember->user_id();

			if ($user instanceof Auth_ORM_External_OAuth2 OR $user instanceof Auth_ORM_External_OAuth)
				$aoo->oauth_data = array(
					'token'=>$remember->token,
				);
			elseif ($user instanceof Auth_ORM_External)
				$aoo->oauth_data = array(
					'token'=>$remember->ao->getAccessToken(),
				);

			return $aoo->save();
		}

		return TRUE;
	}
}
?>
