Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue using optional() on a RemoteUser in Laravel 5.6+. #129

Merged
merged 4 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
42 changes: 38 additions & 4 deletions src/Server/RemoteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +52 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌


// 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.
Expand All @@ -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)) {
Expand All @@ -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;
}
}