Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable registration #300

Merged
merged 9 commits into from
Feb 6, 2025
Merged
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.ci
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
APP_FORCE_HTTPS=false
APP_ENABLE_REGISTRATION=true
SESSION_SECURE_COOKIE=false

# Logging
Expand Down
29 changes: 12 additions & 17 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Application
APP_NAME=solidtime
APP_ENV=local
APP_KEY=base64:UNQNf1SXeASNkWux01Rj8EnHYx8FO0kAxWNDwktclkk=
APP_DEBUG=true
APP_URL=https://solidtime.test
AUDITING_ENABLED=true
APP_ENABLE_REGISTRATION=true
[email protected]
PAGINATION_PER_PAGE_DEFAULT=500

# Logging
LOG_CHANNEL=single
Expand All @@ -25,9 +28,16 @@ DB_TEST_DATABASE=laravel
DB_TEST_USERNAME=root
DB_TEST_PASSWORD=root

BROADCAST_DRIVER=log
# Broadcasting
BROADCAST_DRIVER=null

# Cache
CACHE_DRIVER=file

# Queue
QUEUE_CONNECTION=sync

# Session
SESSION_DRIVER=database
SESSION_LIFETIME=120

Expand All @@ -41,14 +51,6 @@ MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

# Filesystems
FILESYSTEM_DISK=s3
PUBLIC_FILESYSTEM_DISK=s3
Expand All @@ -65,16 +67,9 @@ GOTENBERG_URL=http://gotenberg:3000

VITE_HOST_NAME=vite.solidtime.test
VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

# Local setup
NGINX_HOST_NAME=solidtime.test
NETWORK_NAME=reverse-proxy-docker-traefik_routing

FORWARD_DB_PORT=5432
FORWARD_WEB_PORT=8083

PAGINATION_PER_PAGE_DEFAULT=500
42 changes: 16 additions & 26 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace App\Actions\Fortify;

use App\Enums\Role;
use App\Enums\Weekday;
use App\Events\NewsletterRegistered;
use App\Models\Organization;
use App\Models\User;
use App\Service\IpLookup\IpLookupServiceContract;
use App\Service\TimezoneService;
use App\Service\UserService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
Expand All @@ -34,6 +32,12 @@ class CreateNewUser implements CreatesNewUsers
*/
public function create(array $input): User
{
if (! config('app.enable_registration')) {
throw ValidationException::withMessages([
'email' => [__('Registration is disabled.')],
]);
}

Validator::make($input, [
'name' => [
'required',
Expand Down Expand Up @@ -81,30 +85,16 @@ public function create(array $input): User
$currency = $ipLookupResponse->currency;
}
$user = null;
$organization = null;
DB::transaction(function () use (&$user, &$organization, $input, $timezone, $startOfWeek, $currency): void {
$user = User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'timezone' => $timezone ?? 'UTC',
'week_start' => $startOfWeek,
]);

$organization = new Organization;
$organization->name = explode(' ', $user->name, 2)[0]."'s Organization";
$organization->personal_team = true;
$organization->currency = $currency ?? 'EUR';
$organization->owner()->associate($user);
$organization->save();

$organization->users()->attach(
$user, [
'role' => Role::Owner->value,
]
DB::transaction(function () use (&$user, $input, $timezone, $startOfWeek, $currency): void {
$userService = app(UserService::class);
$user = $userService->createUser(
$input['name'],
$input['email'],
$input['password'],
$timezone ?? 'UTC',
$startOfWeek,
$currency ?? 'EUR',
);

$user->ownedTeams()->save($organization);
});

$newsletterConsent = isset($input['newsletter_consent']) && (bool) $input['newsletter_consent'];
Expand Down
14 changes: 2 additions & 12 deletions app/Actions/Jetstream/AddOrganizationMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
use App\Enums\Role;
use App\Models\Organization;
use App\Models\User;
use App\Service\MemberService;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
use Laravel\Jetstream\Contracts\AddsTeamMembers;
use Laravel\Jetstream\Events\AddingTeamMember;
use Laravel\Jetstream\Events\TeamMemberAdded;

class AddOrganizationMember implements AddsTeamMembers
{
Expand All @@ -36,15 +34,7 @@ public function add(User $owner, Organization $organization, string $email, ?str
->where('is_placeholder', '=', false)
->firstOrFail();

AddingTeamMember::dispatch($organization, $newOrganizationMember);

DB::transaction(function () use ($organization, $newOrganizationMember, $role): void {
$organization->users()->attach(
$newOrganizationMember, ['role' => $role]
);
});

TeamMemberAdded::dispatch($organization, $newOrganizationMember);
app(MemberService::class)->addMember($newOrganizationMember, $organization, Role::from($role));
}

/**
Expand Down
95 changes: 95 additions & 0 deletions app/Console/Commands/Admin/UserCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\Admin;

use App\Enums\Weekday;
use App\Models\Organization;
use App\Models\User;
use App\Service\UserService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use LogicException;

class UserCreateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:user:create
{ name : The name of the user }
{ email : The email of the user }
{ --ask-for-password : Ask for the password, otherwise the command will generate a random one }
{ --verify-email : Verify the email address of the user }';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new user';

/**
* Execute the console command.
*/
public function handle(): int
{
$name = $this->argument('name');
$email = $this->argument('email');
$askForPassword = (bool) $this->option('ask-for-password');
$verifyEmail = (bool) $this->option('verify-email');

if (User::query()->where('email', $email)->where('is_placeholder', '=', false)->exists()) {
$this->error('User with email "'.$email.'" already exists.');

return self::FAILURE;
}

if ($askForPassword) {
$outputPassword = false;
$password = $this->secret('Enter the password');
} else {
$outputPassword = true;
$password = bin2hex(random_bytes(16));
}

$user = null;
DB::transaction(function () use (&$user, $name, $email, $password): void {
$user = app(UserService::class)->createUser(
$name,
$email,
$password,
'UTC',
Weekday::Monday,
'EUR',
);
});
/** @var Organization|null $organization */
$organization = $user->ownedTeams->first();
if ($organization === null) {
throw new LogicException('User does not have an organization');

Check warning on line 73 in app/Console/Commands/Admin/UserCreateCommand.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Admin/UserCreateCommand.php#L73

Added line #L73 was not covered by tests
}

if ($verifyEmail) {
$user->markEmailAsVerified();
}

$this->info('Created user "'.$name.'" ("'.$email.'")');
$this->line('ID: '.$user->getKey());
$this->line('Name: '.$name);
$this->line('Email: '.$email);
if ($outputPassword) {
$this->line('Password: '.$password);
}
$this->line('Timezone: '.$user->timezone);
$this->line('Week start: '.$user->week_start->value);

// Organization
$this->line('Currency: '.$organization->currency);

return self::SUCCESS;
}
}
4 changes: 3 additions & 1 deletion app/Console/Commands/Admin/UserVerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function handle(): int
$this->info('Start verifying user with email "'.$email.'"');

/** @var User|null $user */
$user = User::where('email', $email)->first();
$user = User::query()->where('email', $email)
->where('is_placeholder', '=', false)
->first();

if ($user === null) {
$this->error('User with email "'.$email.'" not found.');
Expand Down
5 changes: 5 additions & 0 deletions app/Filament/Resources/ClientResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ public static function table(Table $table): Table
->defaultSort('created_at', 'desc')
->filters([
SelectFilter::make('organization')
->label('Organization')
->relationship('organization', 'name')
->searchable(),
SelectFilter::make('organization_id')
->label('Organization ID')
->relationship('organization', 'id')
->searchable(),
])
->actions([
Tables\Actions\EditAction::make(),
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/ClientResource/Pages/EditClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class EditClient extends EditRecord
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->icon('heroicon-m-trash'),
];
}
}
3 changes: 2 additions & 1 deletion app/Filament/Resources/ClientResource/Pages/ListClients.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ListClients extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\CreateAction::make()
->icon('heroicon-s-plus'),
];
}
}
Loading