Skip to content
Open
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost:8000
ADMINS=
MODERATORS=

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function show(ShowUserRequest $request, #[CurrentUser] ?User $currentUser
// viewer's relationship to them on the viewer's own.
$profile = new Channel('profiles.'.$user->id);
$relationship = fn (Closure $value): Closure|LiveProp => $currentUser instanceof User ? Inertia::live($value, UserUpdate::Account->value, new PrivateChannel($currentUser)) : $value;
$canSeeModerationHistory = $currentUser instanceof User && ($currentUser->is($user) || ($currentUser->isAdmin() && ! $user->isAdmin()));
$canSeeModerationHistory = $currentUser instanceof User && ($currentUser->is($user) || ($currentUser->canModerate() && ! $user->isAdmin()));

return Inertia::render('user/show', [
'user' => Inertia::live(fn (): ?array => ShownStreams::user($getCountedUser()), 'profile', $profile),
Expand All @@ -81,7 +81,7 @@ public function show(ShowUserRequest $request, #[CurrentUser] ?User $currentUser
'activity' => fn (): array => $activity->get($user, $currentUser),
'streaks' => $getStreaks,
'achievements' => fn (): array => $achievements->get($user, $currentUser, $getStreaks()),
'moderation' => $currentUser?->isAdmin() === true && ! $user->isAdmin()
'moderation' => $currentUser?->canModerate() === true && ! $user->isAdmin()
? fn (): array => $moderation->get([$user])[$user->id]
: null,
'moderationHistory' => match (true) {
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function onVersionChange(Request $request, Response $response): Response
public function share(Request $request): array
{
$isAdmin = $request->user()?->isAdmin() ?? false;
$canModerate = $request->user()?->canModerate() ?? false;
$user = $request->user();
$showsWhoToFollow = $user instanceof User && $request->cookie('who_to_follow') !== 'false';
$context = match (true) {
Expand Down Expand Up @@ -140,6 +141,7 @@ public function share(Request $request): array
->map(fn (Organization $organization): array => ['type' => 'organization', ...$organization->toArray()])
->all() : [], UserUpdate::Account),
'isAdmin' => $isAdmin,
'canModerate' => $canModerate,
'automaticTranslation' => $live(fn (): bool => $user->automatic_translation ?? true, UserUpdate::Account),
// The language translations are read in, saved or from the browser, so a change saved elsewhere reaches
// the translations already shown.
Expand All @@ -163,7 +165,7 @@ public function share(Request $request): array
],
// Guests see how many developers are here, next to the sign-in button...
'developersCount' => fn (): ?int => $user instanceof User ? null : resolve(DevelopersCountQuery::class)->get(),
'hasUnmoderatedCommits' => $isAdmin ? $live(fn (): bool => Commit::query()->awaitingModerator()->exists(), UserUpdate::Moderation) : false,
'hasUnmoderatedCommits' => $canModerate ? $live(fn (): bool => Commit::query()->awaitingModerator()->exists(), UserUpdate::Moderation) : false,
// The lite mobile app, which draws its own tab bar (see resources/js/lib/lite-mobile-app.ts). Any browser can send this
// token, so it only ever changes the UI...
'isLiteMobileApp' => str_contains((string) $request->userAgent(), 'DevNullLite/'),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/CreateCommitApprovalRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function authorize(): bool
$user = $this->user();
assert($user instanceof User);

return $user->isAdmin();
return $user->canModerate();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/CreateCommitRejectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function authorize(): bool
$user = $this->user();
assert($user instanceof User);

return $user->isAdmin();
return $user->canModerate();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/DeleteCommitReportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function authorize(): Response
$user = $this->user();
assert($user instanceof User);

if (! $user->isAdmin()) {
if (! $user->canModerate()) {
return Response::deny();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/DeleteGuildReportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class DeleteGuildReportRequest extends FormRequest
{
public function authorize(Features $features): Response
{
return $this->user()?->isAdmin() && $features->available(Feature::Guilds, $this->user())
return $this->user()?->canModerate() && $features->available(Feature::Guilds, $this->user())
? Response::allow() : Response::denyAsNotFound();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ListGuildReportsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class ListGuildReportsRequest extends FormRequest
{
public function authorize(Features $features): Response
{
return $this->user()?->isAdmin() && $features->available(Feature::Guilds, $this->user())
return $this->user()?->canModerate() && $features->available(Feature::Guilds, $this->user())
? Response::allow() : Response::denyAsNotFound();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ListModeratedUsersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function authorize(): bool
$user = $this->user();
assert($user instanceof User);

return $user->isAdmin();
return $user->canModerate();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ListReportedCommitsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function authorize(): bool
$user = $this->user();
assert($user instanceof User);

return $user->isAdmin();
return $user->canModerate();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ShowCommitReportImageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function authorize(): Response
assert($report instanceof CommitReport);
$image = $report->images[(int) $this->route('image')] ?? null;

return ($user->isAdmin() || $report->reporter_id === $user->id) && $image !== null && Storage::disk()->exists($image['path'])
return ($user->canModerate() || $report->reporter_id === $user->id) && $image !== null && Storage::disk()->exists($image['path'])
? Response::allow()
: Response::denyAsNotFound();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ShowModerationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function authorize(): bool
$user = $this->user();
assert($user instanceof User);

return $user->isAdmin();
return $user->canModerate();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,22 @@ public function isAdmin(): bool
return in_array(mb_strtolower($this->handle), array_map(mb_strtolower(...), $admins), true);
}

/**
* Moderators review the pending queue and user reports, but cannot timeout or ban anyone.
*/
public function isModerator(): bool
{
/** @var list<string> $moderators */
$moderators = config('app.moderators');

return in_array(mb_strtolower($this->handle), array_map(mb_strtolower(...), $moderators), true);
}

public function canModerate(): bool
{
return $this->isAdmin() || $this->isModerator();
}

/**
* Unlike loadCount(), counts follows on the pivot tables alone: the self-referencing
* relations would otherwise join the users table for every follow. Commits are
Expand Down
12 changes: 11 additions & 1 deletion app/Services/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ public function adminOnly(Feature $feature): bool

public function available(Feature $feature, ?User $user): bool
{
return $this->enabled($feature) && (! $this->adminOnly($feature) || ($user?->isAdmin() ?? false));
if (! $this->enabled($feature)) {
return false;
}

if (! $this->adminOnly($feature)) {
return true;
}

return $feature === Feature::Guilds
? ($user?->canModerate() ?? false)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
: ($user?->isAdmin() ?? false);
}

/** @return list<array{name: string, enabled: bool, admin_only: bool}> */
Expand Down
16 changes: 16 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
explode(',', (string) env('ADMINS', '')),
))),

/*
|--------------------------------------------------------------------------
| Application Moderators
|--------------------------------------------------------------------------
|
| The GitHub usernames listed here can review the pending queue and user
| reports, without the power to timeout or ban anyone. Admins can do
| everything moderators can, so they never need to be listed here too.
|
*/

'moderators' => array_values(array_filter(array_map(
trim(...),
explode(',', (string) env('MODERATORS', '')),
))),

/*
|--------------------------------------------------------------------------
| Application Debug Mode
Expand Down
16 changes: 10 additions & 6 deletions resources/js/components/app-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,23 @@ function useNavItems(): (NavItem & { isActive: boolean })[] {
...(guilds !== null && guilds !== undefined
? [{ title: 'Guilds', href: guildIndex(), icon: UsersRound }]
: []),
...(auth.isAdmin
...(auth.canModerate
? [
{
title: 'Moderation',
href: ModerationController.show(),
icon: ShieldCheck,
hasIndicator: hasUnmoderatedCommits,
},
{
title: 'Operations',
href: OperationsController.show(),
icon: Activity,
},
...(auth.isAdmin
? [
{
title: 'Operations',
href: OperationsController.show(),
icon: Activity,
},
]
: []),
]
: []),
{
Expand Down
30 changes: 19 additions & 11 deletions resources/js/components/moderation-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function ModerationTabs({
}: {
active: 'pending' | 'reports' | 'people' | 'bulk' | 'guilds';
}) {
const { auth } = usePage().props;
const hasGuilds = usePage().props.guilds != null;
return (
<PageTabs
Expand All @@ -27,17 +28,24 @@ export function ModerationTabs({
href: index(),
isActive: active === 'reports',
},
{
title: 'Timeouts & bans',
shortTitle: 'People',
href: people(),
isActive: active === 'people',
},
{
title: 'Bulk',
href: bulk(),
isActive: active === 'bulk',
},
...(auth.canModerate
? [
{
title: 'Timeouts & bans',
href: people(),
isActive: active === 'people',
},
]
: []),
...(auth.isAdmin
? [
{
title: 'Bulk',
href: bulk(),
isActive: active === 'bulk',
},
]
: []),
...(hasGuilds
? [
{
Expand Down
2 changes: 1 addition & 1 deletion resources/js/layouts/app-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default function AppLayout({ children }: AppLayoutProps) {
<KeyboardShortcutsDialog
open={isShowingShortcuts}
onOpenChange={setIsShowingShortcuts}
canModerate={auth.isAdmin}
canModerate={auth.canModerate}
hasRemotes={auth.remotes !== null}
/>

Expand Down
43 changes: 24 additions & 19 deletions resources/js/pages/moderation/people.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Head, Link, router } from '@inertiajs/react';
import { Head, Link, router, usePage } from '@inertiajs/react';
import { Undo2 } from 'lucide-react';
import type { ReactNode } from 'react';
import BanController from '@/actions/App/Http/Controllers/BanController';
Expand Down Expand Up @@ -35,6 +35,7 @@ export default function People({
timeouts: Timeout[];
bans: Ban[];
}) {
const { auth } = usePage().props;
const endTimeout = (timeout: Timeout) =>
router
.optimistic((props: { timeouts: Timeout[] }) => ({
Expand Down Expand Up @@ -82,15 +83,17 @@ export default function People({
</>
}
action={
<Button
variant="outline"
size="sm"
onClick={() => endTimeout(timeout)}
data-test="end-timeout-button"
>
<Undo2 />
End timeout
</Button>
auth.isAdmin ? (
<Button
variant="outline"
size="sm"
onClick={() => endTimeout(timeout)}
data-test="end-timeout-button"
>
<Undo2 />
End timeout
</Button>
) : null
}
/>
))}
Expand All @@ -115,15 +118,17 @@ export default function People({
</>
}
action={
<Button
variant="outline"
size="sm"
onClick={() => liftBan(ban)}
data-test="unban-button"
>
<Undo2 />
{ban.user ? 'Unban' : 'Lift ban'}
</Button>
auth.isAdmin ? (
<Button
variant="outline"
size="sm"
onClick={() => liftBan(ban)}
data-test="unban-button"
>
<Undo2 />
{ban.user ? 'Unban' : 'Lift ban'}
</Button>
) : null
}
/>
))}
Expand Down
2 changes: 1 addition & 1 deletion resources/js/pages/onboarding/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ export default function Show({
<KeyboardShortcutsDialog
open={isShowingShortcuts}
onOpenChange={setIsShowingShortcuts}
canModerate={auth.isAdmin}
canModerate={auth.canModerate}
hasRemotes={auth.remotes !== null}
/>
</>
Expand Down
2 changes: 1 addition & 1 deletion resources/js/pages/user/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export default function Show({
</>
) : (
<>
{moderation && (
{moderation && auth.isAdmin && (
<UserModerationMenu
user={user}
moderation={moderation}
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type Auth = {
*/
organizations: OrganizationAuthor[];
isAdmin: boolean;
canModerate: boolean;
automaticTranslation: boolean;
/**
* The language the reader reads translations in, saved or from the browser.
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/Controllers/CommitApprovalControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,18 @@

expect($commit->refresh()->status)->toBe(CommitStatus::Pending);
});

it('may approve a commit as a moderator', function (): void {
config(['app.moderators' => ['maude']]);

$moderator = User::factory()->create(['handle' => 'maude']);
$commit = Commit::factory()->pending()->create();

$response = $this->actingAs($moderator)
->fromRoute('moderation.show')
->post(route('commit-approval.store', $commit));

$response->assertRedirectToRoute('moderation.show');

expect($commit->refresh()->status)->toBe(CommitStatus::Approved);
});
Loading