diff --git a/src/Common/HasAttributes.php b/src/Common/HasAttributes.php index c687ed2..4439fc4 100644 --- a/src/Common/HasAttributes.php +++ b/src/Common/HasAttributes.php @@ -27,7 +27,7 @@ trait HasAttributes */ public function __isset($key) { - return (isset($this->attributes[$key]) || isset($this->relations[$key])) || + return isset($this->attributes[$key]) || ($this->hasGetMutator($key) && ! is_null($this->getAttributeValue($key))); } diff --git a/src/Server/RemoteUser.php b/src/Server/RemoteUser.php index 69aa424..25c1a61 100644 --- a/src/Server/RemoteUser.php +++ b/src/Server/RemoteUser.php @@ -39,6 +39,29 @@ public function __construct($id) } } + /** + * Is this attribute specified on the user? + * + * @param string $key + * @return bool + */ + public function __isset($key) + { + // We read some fields directly from the OAuth token, and so + // they can always be considered "set" (even if they're null): + if (in_array($key, ['id', 'northstar_id', 'role'])) { + return true; + } + + // Otherwise, we'll need to load the user's full user profile + // attributes & check if requested key is set there: + if (! $this->loaded) { + $this->loadAttributes(); + } + + return isset($this->attributes[$key]); + } + /** * Get an attribute, either from the token on the request or by * lazy-loading the full profile from the authorization server. @@ -59,10 +82,7 @@ public function __get($key) // duration of this request. (This instance is kept by the // user provider.) if (! $this->loaded) { - $user = gateway('northstar')->withToken($this->token)->getUser($this->id); - - $this->attributes = $user->toArray(); - $this->loaded = true; + $this->loadAttributes(); } if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) { @@ -71,4 +91,18 @@ public function __get($key) return null; } + + /** + * Load the attributes on this 'RemoteUser' by requesting + * the corresponding user profile in Northstar. + * + * @return void + */ + private function loadAttributes() + { + $user = gateway('northstar')->withToken($this->token)->getUser($this->id); + + $this->attributes = $user->toArray(); + $this->loaded = true; + } }