Skip to content

User data processor improvements #17

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

Merged
merged 6 commits into from
Feb 14, 2016
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ Log::error('Oops, Something went wrong', [
]);
```

User data can alternatively be added to the context of any messages sent via a processor, such as the included `UserDataProcessor`; see below.

#### Included optional processors

##### `UserDataProcessor`

This processor attaches data about the current user to the error report.

```php
'processors' => [
Clowdy\Raven\Processors\UserDataProcessor::class,
],
```

Or, to configure the `UserDataProcessor` object:

```php
'processors' => [
new Clowdy\Raven\Processors\UserDataProcessor([
'only' => ['id', 'username', 'email'], // This is ['id'] by default; pass [] or null to include all fields
'appends' => ['normallyHiddenProperty'],
]),
],
```

See the `UserDataProcessor` source for full details.

## Credits

This package was inspired [rcrowe/Raven](https://github.com/rcrowe/Raven).
36 changes: 27 additions & 9 deletions src/Processors/UserDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
*
* The model's `toArray` method is used to get the user data. This is provided
* by Eloquent and can be overridden to provide extra fields or to remove
* fields. Fields can also be removed in Eloquent by populating the `$hidden`
* property.
* fields. Fields can also be removed and added in Eloquent by populating the
* `$hidden` and `$appends` properties.
*
* The fields retrieved from `toArray` are then filtered with any options passed
* to this processor's constructor before being attached to the error report.
*
* If no user is logged in, the data `['id' => null]` is still attached to the
* error object, to differentiate a case where no data on the current user is
* available from a case where it is known that no user is logged in.
* By default only the 'id' field from the user is attached; pass null or an
* empty array as the 'only' option to override this, or configure otherwise as
* you see fit.
*/
class UserDataProcessor
{
/**
* @var array
*/
protected $options = [
'appends' => [],
'except' => [],
'only' => [
'id',
Expand All @@ -36,13 +37,14 @@ class UserDataProcessor
* Make a new UserDataProcessor.
*
* Options:
* - array 'appends': extra fields from the user to include
* - array 'except': fields from the user not to include
* - array 'only': the only fields from the user to include, or null to
* include all fields (other than those removed by 'except')
*
* Note that rather than using these options it may be preferable in certain
* cases to use the User model's `$hidden` property or overriding its
* `toArray` method.
* cases to use the User model's `$hidden` and `$appends` properties, or
* overriding its `toArray` method.
*
* @param array $options
*/
Expand All @@ -55,7 +57,7 @@ public function __construct(array $options = [])
* Run the processor: attach user data to a Monolog record.
*
* @param array $record Monolog record
*
*
* @return array $record
*/
public function __invoke(array $record)
Expand All @@ -69,9 +71,25 @@ public function __invoke(array $record)
if (! empty($this->options['only'])) {
$data = array_only($data, $this->options['only']);
}

foreach ($this->options['appends'] as $key) {
$data[$key] = $user->{$key};
}
}

$record['context']['user'] = array_merge($data, array_get($record, 'context.user', []));
// Nest all but the particular keys Sentry treats specially in a 'data'
// substructure; see
// https://github.com/getsentry/sentry/blob/e7a295784f10a296ace7aa98e1b7216328feac5d/src/sentry/interfaces/user.py#L31
$topLevel = [
'id',
'username',
'email',
'ip_address',
];
$userArray = array_only($data, $topLevel);
$userArray['data'] = array_except($data, $topLevel);

$record['context']['user'] = array_merge($userArray, array_get($record, 'context.user', []));

return $record;
}
Expand Down