|
4 | 4 |
|
5 | 5 | namespace App\Http\Resources\V1\Article; |
6 | 6 |
|
| 7 | +use App\Enums\UserRole; |
7 | 8 | use Illuminate\Http\Request; |
8 | 9 | use Illuminate\Http\Resources\Json\JsonResource; |
9 | 10 |
|
@@ -36,11 +37,12 @@ public function toArray(Request $request): array |
36 | 37 | 'updated_at' => $this->updated_at?->toISOString(), |
37 | 38 |
|
38 | 39 | // Relationships |
39 | | - 'author' => $this->whenLoaded('author', function () { |
| 40 | + // Original Author |
| 41 | + 'author' => $this->whenLoaded('author', function () use ($request) { |
40 | 42 | return $this->author ? [ |
41 | 43 | 'id' => $this->author->id, |
42 | 44 | 'name' => $this->author->name, |
43 | | - 'email' => $this->author->email, |
| 45 | + 'email' => $this->when((bool) $request->user()?->hasRole(UserRole::ADMINISTRATOR->value), $this->author->email), |
44 | 46 | 'avatar_url' => $this->author->avatar_url, |
45 | 47 | 'bio' => $this->author->bio, |
46 | 48 | 'twitter' => $this->author->twitter, |
@@ -77,26 +79,23 @@ public function toArray(Request $request): array |
77 | 79 | })->values()->all(); |
78 | 80 | }), |
79 | 81 |
|
80 | | - 'authors' => $this->whenLoaded('authors', function () { |
| 82 | + // Co-Authors |
| 83 | + 'authors' => $this->whenLoaded('authors', function () use ($request) { |
81 | 84 | /** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $authors */ |
82 | 85 | $authors = $this->authors; |
83 | 86 |
|
84 | | - return $authors->map(function ($author) { |
85 | | - /** @var \Illuminate\Database\Eloquent\Relations\Pivot|null $pivot */ |
86 | | - $pivot = $author->getAttribute('pivot'); |
87 | | - |
| 87 | + return $authors->map(function ($author) use ($request) { |
88 | 88 | return [ |
89 | 89 | 'id' => $author->id, |
90 | 90 | 'name' => $author->name, |
91 | | - 'email' => $author->email, |
| 91 | + 'email' => $this->when((bool) $request->user()?->hasRole(UserRole::ADMINISTRATOR->value), $author->email), |
92 | 92 | 'avatar_url' => $author->avatar_url, |
93 | 93 | 'bio' => $author->bio, |
94 | 94 | 'twitter' => $author->twitter, |
95 | 95 | 'facebook' => $author->facebook, |
96 | 96 | 'linkedin' => $author->linkedin, |
97 | 97 | 'github' => $author->github, |
98 | 98 | 'website' => $author->website, |
99 | | - 'role' => $pivot?->getAttribute('role'), |
100 | 99 | ]; |
101 | 100 | })->values()->all(); |
102 | 101 | }), |
|
0 commit comments