a lot hehe

This commit is contained in:
Andras Bacsai
2023-06-01 12:15:33 +02:00
parent c8f70a4e3b
commit 0aa816b4f2
42 changed files with 570 additions and 249 deletions

View File

@@ -4,7 +4,7 @@ namespace App\Http\Livewire\Notifications;
use App\Models\Server;
use App\Models\Team;
use App\Notifications\DemoNotification;
use App\Notifications\TestNotification;
use Illuminate\Support\Facades\Notification;
use Livewire\Component;
@@ -13,31 +13,40 @@ class DiscordSettings extends Component
public Team|Server $model;
protected $rules = [
'model.extra_attributes.discord_webhook' => 'nullable|url',
'model.extra_attributes.discord_active' => 'nullable|boolean',
'model.smtp_attributes.discord_active' => 'nullable|boolean',
'model.smtp_attributes.discord_webhook' => 'required|url',
];
protected $validationAttributes = [
'model.extra_attributes.discord_webhook' => 'Discord Webhook',
'model.smtp_attributes.discord_webhook' => 'Discord Webhook',
];
public function mount($model)
{
//
}
public function instantSave()
{
try {
$this->submit();
} catch (\Exception $e) {
$this->model->smtp_attributes->discord_active = false;
$this->addError('model.smtp_attributes.discord_webhook', $e->getMessage());
}
}
private function saveModel()
{
$this->model->save();
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
}
}
public function submit()
{
$this->resetErrorBag();
$this->validate();
$this->model->save();
if ( is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
}
$this->saveModel();
}
public function sendTestNotification()
{
Notification::send($this->model, new DemoNotification);
}
public function render()
{
return view('livewire.notifications.discord-settings');
Notification::send($this->model, new TestNotification);
}
}

View File

@@ -4,7 +4,7 @@ namespace App\Http\Livewire\Notifications;
use App\Models\Server;
use App\Models\Team;
use App\Notifications\DemoNotification;
use App\Notifications\TestNotification;
use Illuminate\Support\Facades\Notification;
use Livewire\Component;
@@ -13,27 +13,27 @@ class EmailSettings extends Component
public Team|Server $model;
protected $rules = [
'model.extra_attributes.smtp_active' => 'nullable|boolean',
'model.extra_attributes.from_address' => 'nullable',
'model.extra_attributes.from_name' => 'nullable',
'model.extra_attributes.recipients' => 'nullable',
'model.extra_attributes.smtp_host' => 'nullable',
'model.extra_attributes.smtp_port' => 'nullable',
'model.extra_attributes.smtp_encryption' => 'nullable',
'model.extra_attributes.smtp_username' => 'nullable',
'model.extra_attributes.smtp_password' => 'nullable',
'model.extra_attributes.smtp_timeout' => 'nullable',
'model.smtp_attributes.smtp_active' => 'nullable|boolean',
'model.smtp_attributes.from_address' => 'required',
'model.smtp_attributes.from_name' => 'required',
'model.smtp_attributes.recipients' => 'required',
'model.smtp_attributes.smtp_host' => 'required',
'model.smtp_attributes.smtp_port' => 'required',
'model.smtp_attributes.smtp_encryption' => 'nullable',
'model.smtp_attributes.smtp_username' => 'nullable',
'model.smtp_attributes.smtp_password' => 'nullable',
'model.smtp_attributes.smtp_timeout' => 'nullable',
'model.smtp_attributes.test_address' => 'nullable',
];
protected $validationAttributes = [
'model.extra_attributes.from_address' => 'From Address',
'model.extra_attributes.from_name' => 'From Name',
'model.extra_attributes.recipients' => 'Recipients',
'model.extra_attributes.smtp_host' => 'Host',
'model.extra_attributes.smtp_port' => 'Port',
'model.extra_attributes.smtp_encryption' => 'Encryption',
'model.extra_attributes.smtp_username' => 'Username',
'model.extra_attributes.smtp_password' => 'Password',
'model.extra_attributes.smtp_timeout' => 'Timeout',
'model.smtp_attributes.from_address' => 'From Address',
'model.smtp_attributes.from_name' => 'From Name',
'model.smtp_attributes.recipients' => 'Recipients',
'model.smtp_attributes.smtp_host' => 'Host',
'model.smtp_attributes.smtp_port' => 'Port',
'model.smtp_attributes.smtp_encryption' => 'Encryption',
'model.smtp_attributes.smtp_username' => 'Username',
'model.smtp_attributes.smtp_password' => 'Password',
];
public function mount($model)
{
@@ -43,17 +43,17 @@ class EmailSettings extends Component
{
$this->resetErrorBag();
$this->validate();
$this->saveModel();
}
private function saveModel()
{
$this->model->save();
if ( is_a($this->model, Team::class)) {
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
}
}
public function sendTestNotification()
public function instantSave()
{
Notification::send($this->model, new DemoNotification);
}
public function render()
{
return view('livewire.notifications.email-settings');
$this->saveModel();
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Http\Livewire\Notifications;
use App\Models\Server;
use App\Models\Team;
use App\Notifications\TestNotification;
use Livewire\Component;
use Notification;
class Test extends Component
{
public Team|Server $model;
public function sendTestNotification()
{
Notification::send($this->model, new TestNotification);
$this->emit('saved', 'Test notification sent.');
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Livewire\Settings;
use App\Models\InstanceSettings;
use Livewire\Component;
class Email extends Component
{
public InstanceSettings $model;
protected $rules = [
'model.extra_attributes.from_address' => 'nullable',
'model.extra_attributes.from_name' => 'nullable',
'model.extra_attributes.recipients' => 'nullable',
'model.extra_attributes.smtp_host' => 'nullable',
'model.extra_attributes.smtp_port' => 'nullable',
'model.extra_attributes.smtp_encryption' => 'nullable',
'model.extra_attributes.smtp_username' => 'nullable',
'model.extra_attributes.smtp_password' => 'nullable',
'model.extra_attributes.smtp_timeout' => 'nullable',
];
protected $validationAttributes = [
'model.extra_attributes.from_address' => 'From Address',
'model.extra_attributes.from_name' => 'From Name',
'model.extra_attributes.recipients' => 'Recipients',
'model.extra_attributes.smtp_host' => 'Host',
'model.extra_attributes.smtp_port' => 'Port',
'model.extra_attributes.smtp_encryption' => 'Encryption',
'model.extra_attributes.smtp_username' => 'Username',
'model.extra_attributes.smtp_password' => 'Password',
];
public function mount($model)
{
//
}
public function render()
{
return view('livewire.settings.email');
}
}

View File

@@ -22,16 +22,23 @@ class Server extends BaseModel
'port',
'team_id',
'private_key_id',
'extra_attributes',
'smtp_attributes',
];
public $casts = [
'extra_attributes' => SchemalessAttributes::class,
'smtp_attributes' => SchemalessAttributes::class,
];
public function scopeWithExtraAttributes(): Builder
{
return $this->extra_attributes->modelScope();
}
public function scopeWithSmtpAttributes(): Builder
{
return $this->smtp_attributes->modelScope();
}
public function standaloneDockers()
{
@@ -43,8 +50,6 @@ class Server extends BaseModel
return $this->hasMany(SwarmDocker::class);
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);

View File

@@ -2,42 +2,41 @@
namespace App\Models;
use App\Notifications\Channels\SendsCoolifyEmail;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsDiscord;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Notifications\Notifiable;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
class Team extends BaseModel implements SendsDiscord, SendsCoolifyEmail
class Team extends BaseModel implements SendsDiscord, SendsEmail
{
use Notifiable;
protected $casts = [
'extra_attributes' => SchemalessAttributes::class,
'smtp_attributes' => SchemalessAttributes::class,
'personal_team' => 'boolean',
];
protected $fillable = [
'id',
'name',
'personal_team',
'extra_attributes',
'smtp_attributes',
];
public function routeNotificationForDiscord()
{
return $this->extra_attributes->get('discord_webhook');
return $this->smtp_attributes->get('discord_webhook');
}
public function routeNotificationForCoolifyEmail()
public function routeNotificationForEmail(string $attribute = 'recipients')
{
$recipients = $this->extra_attributes->get('recipients', '');
$recipients = $this->smtp_attributes->get($attribute, '');
return explode(PHP_EOL, $recipients);
}
public function scopeWithExtraAttributes(): Builder
{
return $this->extra_attributes->modelScope();
return $this->smtp_attributes->modelScope();
}
public function projects()

View File

@@ -8,10 +8,11 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Visus\Cuid2\Cuid2;
use Laravel\Fortify\TwoFactorAuthenticatable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
protected $fillable = [
'id',
'name',

View File

@@ -1,44 +0,0 @@
<?php
namespace App\Notifications\Channels;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
class CoolifyEmailChannel
{
/**
* Send the given notification.
*/
public function send(SendsCoolifyEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
$bcc = $notifiable->routeNotificationForCoolifyEmail();
$mailMessage = $notification->toMail($notifiable);
Mail::send([], [], fn(Message $message) => $message
->from(
$notifiable->extra_attributes?->get('from_address'),
$notifiable->extra_attributes?->get('from_name')
)
->bcc($bcc)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
private function bootConfigs($notifiable): void
{
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => $notifiable->extra_attributes?->get('smtp_host'),
"port" => $notifiable->extra_attributes?->get('smtp_port'),
"encryption" => $notifiable->extra_attributes?->get('smtp_encryption'),
"username" => $notifiable->extra_attributes?->get('smtp_username'),
"password" => $notifiable->extra_attributes?->get('smtp_password'),
"timeout" => $notifiable->extra_attributes?->get('smtp_timeout'),
"local_domain" => null,
]);
}
}

View File

@@ -13,9 +13,7 @@ class DiscordChannel
public function send(SendsDiscord $notifiable, Notification $notification): void
{
$message = $notification->toDiscord($notifiable);
$webhookUrl = $notifiable->routeNotificationForDiscord();
dispatch(new SendMessageToDiscordJob($message, $webhookUrl));
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Notifications\Channels;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
class EmailChannel
{
/**
* Send the given notification.
*/
public function send(SendsEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
if ($notification instanceof \App\Notifications\TestNotification) {
$bcc = $notifiable->routeNotificationForEmail('test_address');
if (count($bcc) === 1) {
$bcc = $notifiable->routeNotificationForEmail();
}
} else {
$bcc = $notifiable->routeNotificationForEmail();
}
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
$notifiable->smtp_attributes?->get('from_address'),
$notifiable->smtp_attributes?->get('from_name')
)
->cc($bcc)
->bcc($bcc)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
private function bootConfigs($notifiable): void
{
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => $notifiable->smtp_attributes?->get('smtp_host'),
"port" => $notifiable->smtp_attributes?->get('smtp_port'),
"encryption" => $notifiable->smtp_attributes?->get('smtp_encryption'),
"username" => $notifiable->smtp_attributes?->get('smtp_username'),
"password" => $notifiable->smtp_attributes?->get('smtp_password'),
"timeout" => $notifiable->smtp_attributes?->get('smtp_timeout'),
"local_domain" => null,
]);
}
}

View File

@@ -1,8 +0,0 @@
<?php
namespace App\Notifications\Channels;
interface SendsCoolifyEmail
{
public function routeNotificationForCoolifyEmail();
}

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Notifications\Channels;
interface SendsEmail
{
public function routeNotificationForEmail();
}

View File

@@ -2,14 +2,14 @@
namespace App\Notifications;
use App\Notifications\Channels\CoolifyEmailChannel;
use App\Notifications\Channels\EmailChannel;
use App\Notifications\Channels\DiscordChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class DemoNotification extends Notification implements ShouldQueue
class TestNotification extends Notification implements ShouldQueue
{
use Queueable;
@@ -29,8 +29,8 @@ class DemoNotification extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$notifiable->extra_attributes?->get('smtp_active') && $channels[] = CoolifyEmailChannel::class;
$notifiable->extra_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
$notifiable->smtp_attributes?->get('smtp_active') && $channels[] = EmailChannel::class;
$notifiable->smtp_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
return $channels;
}
@@ -40,15 +40,14 @@ class DemoNotification extends Notification implements ShouldQueue
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Coolify demo notification')
->line('Welcome to Coolify!')
->action('Go to dashboard', url('/'))
->line('We need your attention for disk usage.');
->subject('Coolify Test Notification')
->line('Congratulations!')
->line('You have successfully received a test Email notification from Coolify. 🥳');
}
public function toDiscord(object $notifiable): string
{
return 'Welcome to Coolify! We need your attention for disk usage. [Go to dashboard]('.url('/').')';
return 'You have successfully received a test Discord notification from Coolify. 🥳 [Go to your dashboard](' . url('/') . ')';
}
/**

View File

@@ -13,6 +13,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Contracts\RegisterResponse;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
@@ -22,7 +23,17 @@ class FortifyServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
$this->app->instance(RegisterResponse::class, new class implements RegisterResponse
{
public function toResponse($request)
{
// First user (root) will be redirected to /settings instead of / on registration.
if ($request->user()->currentTeam->id === 0) {
return redirect('/settings');
}
return redirect('/');
}
});
}
/**
@@ -30,6 +41,7 @@ class FortifyServiceProvider extends ServiceProvider
*/
public function boot(): void
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::registerView(function () {
$settings = InstanceSettings::get();
if (!$settings->is_registration_enabled) {
@@ -58,10 +70,21 @@ class FortifyServiceProvider extends ServiceProvider
Fortify::requestPasswordResetLinkView(function () {
return view('auth.forgot-password');
});
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::resetPasswordView(function ($request) {
return view('auth.reset-password', ['request' => $request]);
});
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::confirmPasswordView(function () {
return view('auth.confirm-password');
});
Fortify::twoFactorChallengeView(function () {
return view('auth.two-factor-challenge');
});
RateLimiter::for('login', function (Request $request) {
$email = (string) $request->email;