Skip to content

Commit

Permalink
Merge pull request #256 from Autive/feature/disable-user-registration
Browse files Browse the repository at this point in the history
Feature/disable user registration
  • Loading branch information
Cannonb4ll authored Jan 10, 2024
2 parents f064b32 + 8d8013b commit 4956b2b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 deletions.
10 changes: 7 additions & 3 deletions app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace App\Filament\Pages;

use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Support\Enums\Alignment;
use Storage;
use App\Models\Board;
use App\Enums\UserRole;
use App\Models\Project;
use Filament\Forms\Form;
use Illuminate\Support\Str;
use App\Enums\InboxWorkflow;
use Filament\Actions\Action;
use App\Services\GitHubService;
use Filament\Pages\SettingsPage;
use App\Settings\GeneralSettings;
Expand All @@ -21,11 +19,13 @@
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Support\Enums\Alignment;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Forms\Components\RichEditor;

class Settings extends SettingsPage
Expand Down Expand Up @@ -113,6 +113,10 @@ public function form(Form $form): Form
->label('Disallow users to upload files or images via the markdown editors.')
->columnSpan(1),

Toggle::make('disable_user_registration')
->label('Disable user registration')
->columnSpan(1),

Toggle::make('show_github_link')
->label('Show a link to the linked GitHub issue on the item page')
->columnSpan(1)
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Settings\GeneralSettings;
use App\Http\Controllers\Controller;
use App\SocialProviders\SsoProvider;
use Illuminate\Support\Facades\Hash;
Expand Down Expand Up @@ -36,6 +37,10 @@ protected function validator(array $data)

protected function create(array $data)
{
if (app(GeneralSettings::class)->disable_user_registration) {
abort(301, 'User registration is disabled.');
}

return User::create([
'name' => $data['name'],
'email' => $data['email'],
Expand All @@ -45,6 +50,10 @@ protected function create(array $data)

public function showRegistrationForm()
{
if (app(GeneralSettings::class)->disable_user_registration) {
return redirect()->route('home');
}

if (SsoProvider::isForced()) {
return to_route('oauth.login');
}
Expand Down
1 change: 1 addition & 0 deletions app/Settings/GeneralSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GeneralSettings extends Settings
public bool $show_changelog_author;
public bool $show_changelog_related_items;
public bool $disable_file_uploads;
public bool $disable_user_registration;
public array $excluded_matching_search_words;
public array $profanity_words;
public bool $show_github_link;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration {
public function up(): void
{
$this->migrator->add('general.disable_user_registration', false);
}
};
14 changes: 8 additions & 6 deletions resources/views/livewire/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
{{ trans('auth.login') }}
</a>
</li>
<li>
<a class="flex items-center justify-center text-white hover:text-gray-50 focus:outline-none"
href="{{ route('register') }}">
{{ trans('auth.register') }}
</a>
</li>
@if(! app(App\Settings\GeneralSettings::class)->disable_user_registration)
<li>
<a class="flex items-center justify-center text-white hover:text-gray-50 focus:outline-none"
href="{{ route('register') }}">
{{ trans('auth.register') }}
</a>
</li>
@endif
@endguest

@auth
Expand Down
14 changes: 8 additions & 6 deletions resources/views/partials/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
{{ trans('auth.login') }}
</a>
</li>
<li>
<a class="flex items-center justify-center text-white hover:text-gray-50 focus:outline-none"
href="{{ route('register') }}">
{{ trans('auth.register') }}
</a>
</li>
@if(! app(App\Settings\GeneralSettings::class)->disable_user_registration)
<li>
<a class="flex items-center justify-center text-white hover:text-gray-50 focus:outline-none"
href="{{ route('register') }}">
{{ trans('auth.register') }}
</a>
</li>
@endif
@endguest

@auth
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use App\Http\Controllers\SitemapController;
use App\Settings\GeneralSettings;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MyController;
use App\Http\Controllers\ItemController;
use App\Http\Controllers\BoardsController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\ChangelogController;
use App\Http\Controllers\Auth\VerificationController;
use App\Http\Controllers\ItemEmailUnsubscribeController;
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/Auth/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Tests\Feature\Auth;

use App\Settings\GeneralSettings;

test('registration screen can be rendered', function () {
$response = $this->get(route('register'));

Expand Down Expand Up @@ -43,3 +45,15 @@

$this->assertAuthenticatedAs($user);
});

test('guests cannot access /register when this feature is disabled', function () {
GeneralSettings::fake([
'disable_user_registration' => true
]);

$response = $this->get(route('register'));

$response->assertRedirect(route('home'));
$response->assertStatus(302);
});

0 comments on commit 4956b2b

Please sign in to comment.