Skip to content

Commit 08e5d5e

Browse files
committed
Merge pull request #17 from tremby/user-data-processor-improvements
2 parents 8906487 + c676a7c commit 08e5d5e

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ Log::error('Oops, Something went wrong', [
102102
]);
103103
```
104104

105+
User data can alternatively be added to the context of any messages sent via a processor, such as the included `UserDataProcessor`; see below.
106+
107+
#### Included optional processors
108+
109+
##### `UserDataProcessor`
110+
111+
This processor attaches data about the current user to the error report.
112+
113+
```php
114+
'processors' => [
115+
Clowdy\Raven\Processors\UserDataProcessor::class,
116+
],
117+
```
118+
119+
Or, to configure the `UserDataProcessor` object:
120+
121+
```php
122+
'processors' => [
123+
new Clowdy\Raven\Processors\UserDataProcessor([
124+
'only' => ['id', 'username', 'email'], // This is ['id'] by default; pass [] or null to include all fields
125+
'appends' => ['normallyHiddenProperty'],
126+
]),
127+
],
128+
```
129+
130+
See the `UserDataProcessor` source for full details.
131+
105132
## Credits
106133

107134
This package was inspired [rcrowe/Raven](https://github.com/rcrowe/Raven).

src/Processors/UserDataProcessor.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010
*
1111
* The model's `toArray` method is used to get the user data. This is provided
1212
* by Eloquent and can be overridden to provide extra fields or to remove
13-
* fields. Fields can also be removed in Eloquent by populating the `$hidden`
14-
* property.
13+
* fields. Fields can also be removed and added in Eloquent by populating the
14+
* `$hidden` and `$appends` properties.
1515
*
1616
* The fields retrieved from `toArray` are then filtered with any options passed
1717
* to this processor's constructor before being attached to the error report.
1818
*
19-
* If no user is logged in, the data `['id' => null]` is still attached to the
20-
* error object, to differentiate a case where no data on the current user is
21-
* available from a case where it is known that no user is logged in.
19+
* By default only the 'id' field from the user is attached; pass null or an
20+
* empty array as the 'only' option to override this, or configure otherwise as
21+
* you see fit.
2222
*/
2323
class UserDataProcessor
2424
{
2525
/**
2626
* @var array
2727
*/
2828
protected $options = [
29+
'appends' => [],
2930
'except' => [],
3031
'only' => [
3132
'id',
@@ -36,13 +37,14 @@ class UserDataProcessor
3637
* Make a new UserDataProcessor.
3738
*
3839
* Options:
40+
* - array 'appends': extra fields from the user to include
3941
* - array 'except': fields from the user not to include
4042
* - array 'only': the only fields from the user to include, or null to
4143
* include all fields (other than those removed by 'except')
4244
*
4345
* Note that rather than using these options it may be preferable in certain
44-
* cases to use the User model's `$hidden` property or overriding its
45-
* `toArray` method.
46+
* cases to use the User model's `$hidden` and `$appends` properties, or
47+
* overriding its `toArray` method.
4648
*
4749
* @param array $options
4850
*/
@@ -55,7 +57,7 @@ public function __construct(array $options = [])
5557
* Run the processor: attach user data to a Monolog record.
5658
*
5759
* @param array $record Monolog record
58-
*
60+
*
5961
* @return array $record
6062
*/
6163
public function __invoke(array $record)
@@ -69,9 +71,25 @@ public function __invoke(array $record)
6971
if (! empty($this->options['only'])) {
7072
$data = array_only($data, $this->options['only']);
7173
}
74+
75+
foreach ($this->options['appends'] as $key) {
76+
$data[$key] = $user->{$key};
77+
}
7278
}
7379

74-
$record['context']['user'] = array_merge($data, array_get($record, 'context.user', []));
80+
// Nest all but the particular keys Sentry treats specially in a 'data'
81+
// substructure; see
82+
// https://github.com/getsentry/sentry/blob/e7a295784f10a296ace7aa98e1b7216328feac5d/src/sentry/interfaces/user.py#L31
83+
$topLevel = [
84+
'id',
85+
'username',
86+
'email',
87+
'ip_address',
88+
];
89+
$userArray = array_only($data, $topLevel);
90+
$userArray['data'] = array_except($data, $topLevel);
91+
92+
$record['context']['user'] = array_merge($userArray, array_get($record, 'context.user', []));
7593

7694
return $record;
7795
}

0 commit comments

Comments
 (0)