10
10
*
11
11
* The model's `toArray` method is used to get the user data. This is provided
12
12
* 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 .
15
15
*
16
16
* The fields retrieved from `toArray` are then filtered with any options passed
17
17
* to this processor's constructor before being attached to the error report.
18
18
*
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 .
22
22
*/
23
23
class UserDataProcessor
24
24
{
25
25
/**
26
26
* @var array
27
27
*/
28
28
protected $ options = [
29
+ 'appends ' => [],
29
30
'except ' => [],
30
31
'only ' => [
31
32
'id ' ,
@@ -36,13 +37,14 @@ class UserDataProcessor
36
37
* Make a new UserDataProcessor.
37
38
*
38
39
* Options:
40
+ * - array 'appends': extra fields from the user to include
39
41
* - array 'except': fields from the user not to include
40
42
* - array 'only': the only fields from the user to include, or null to
41
43
* include all fields (other than those removed by 'except')
42
44
*
43
45
* 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.
46
48
*
47
49
* @param array $options
48
50
*/
@@ -55,7 +57,7 @@ public function __construct(array $options = [])
55
57
* Run the processor: attach user data to a Monolog record.
56
58
*
57
59
* @param array $record Monolog record
58
- *
60
+ *
59
61
* @return array $record
60
62
*/
61
63
public function __invoke (array $ record )
@@ -69,9 +71,25 @@ public function __invoke(array $record)
69
71
if (! empty ($ this ->options ['only ' ])) {
70
72
$ data = array_only ($ data , $ this ->options ['only ' ]);
71
73
}
74
+
75
+ foreach ($ this ->options ['appends ' ] as $ key ) {
76
+ $ data [$ key ] = $ user ->{$ key };
77
+ }
72
78
}
73
79
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 ' , []));
75
93
76
94
return $ record ;
77
95
}
0 commit comments