Skip to content

Commit 77acaf6

Browse files
committed
feat(article): show authors and restrict email field to admins
1 parent fe37480 commit 77acaf6

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

app/Http/Resources/V1/Article/ArticleResource.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Http\Resources\V1\Article;
66

7+
use App\Enums\UserRole;
78
use Illuminate\Http\Request;
89
use Illuminate\Http\Resources\Json\JsonResource;
910

@@ -36,11 +37,12 @@ public function toArray(Request $request): array
3637
'updated_at' => $this->updated_at?->toISOString(),
3738

3839
// Relationships
39-
'author' => $this->whenLoaded('author', function () {
40+
// Original Author
41+
'author' => $this->whenLoaded('author', function () use ($request) {
4042
return $this->author ? [
4143
'id' => $this->author->id,
4244
'name' => $this->author->name,
43-
'email' => $this->author->email,
45+
'email' => $this->when((bool) $request->user()?->hasRole(UserRole::ADMINISTRATOR->value), $this->author->email),
4446
'avatar_url' => $this->author->avatar_url,
4547
'bio' => $this->author->bio,
4648
'twitter' => $this->author->twitter,
@@ -77,26 +79,23 @@ public function toArray(Request $request): array
7779
})->values()->all();
7880
}),
7981

80-
'authors' => $this->whenLoaded('authors', function () {
82+
// Co-Authors
83+
'authors' => $this->whenLoaded('authors', function () use ($request) {
8184
/** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $authors */
8285
$authors = $this->authors;
8386

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) {
8888
return [
8989
'id' => $author->id,
9090
'name' => $author->name,
91-
'email' => $author->email,
91+
'email' => $this->when((bool) $request->user()?->hasRole(UserRole::ADMINISTRATOR->value), $author->email),
9292
'avatar_url' => $author->avatar_url,
9393
'bio' => $author->bio,
9494
'twitter' => $author->twitter,
9595
'facebook' => $author->facebook,
9696
'linkedin' => $author->linkedin,
9797
'github' => $author->github,
9898
'website' => $author->website,
99-
'role' => $pivot?->getAttribute('role'),
10099
];
101100
})->values()->all();
102101
}),

app/Services/ArticleService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class ArticleService
2222
public function getArticles(array $params): LengthAwarePaginator
2323
{
2424
$query = Article::query()
25-
->with(['author:id,name,email,avatar_url,bio,twitter,facebook,linkedin,github,website', 'categories:id,name,slug', 'tags:id,name,slug'])
25+
->with([
26+
'author:id,name,email,avatar_url,bio,twitter,facebook,linkedin,github,website',
27+
'categories:id,name,slug',
28+
'tags:id,name,slug',
29+
'authors:id,name,email,avatar_url,bio,twitter,facebook,linkedin,github,website',
30+
])
2631
->withCount('comments');
2732

2833
// Apply filters

0 commit comments

Comments
 (0)