/**
* Get the current user.
*
* Will prefer the static user cache over sessions. The static user
* cache is primarily used for stateless authentication. For stateful authentication,
* cookies + sessions will be used.
*
* @param string $key field to retrieve. Leave null to get entire User record
* @return mixed User record. or null if no user is logged in.
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
*/
public static function user($key = null)
{
if (!empty(static::$_user)) {
$user = static::$_user;
} elseif (static::$sessionKey && Session::check(static::$sessionKey)) {
$user = Session::read(static::$sessionKey);
} else {
return null;
}
if ($key === null) {
return $user;
}
return Hash::get($user, $key);
}