updates
This commit is contained in:
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Livewire\Team\Invitations;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\TeamInvitation;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
@@ -53,14 +56,56 @@ class Controller extends BaseController
|
||||
}
|
||||
public function team()
|
||||
{
|
||||
ray(auth()->user()->isAdmin());
|
||||
$invitations = [];
|
||||
if (auth()->user()->isAdmin()) {
|
||||
$invitations = auth()->user()->currentTeam()->invitations;
|
||||
$invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
|
||||
}
|
||||
return view('team.show', [
|
||||
'transactional_emails_active' => data_get(InstanceSettings::get(), 'extra_attributes.smtp_host') ? true : false,
|
||||
'transactional_emails_active' => is_transactional_emails_active(),
|
||||
'invitations' => $invitations,
|
||||
]);
|
||||
}
|
||||
public function accept_invitation()
|
||||
{
|
||||
try {
|
||||
$invitation = TeamInvitation::whereUuid(request()->route('uuid'))->firstOrFail();
|
||||
$user = User::whereEmail($invitation->email)->firstOrFail();
|
||||
if (is_null(auth()->user())) {
|
||||
return redirect()->route('login');
|
||||
}
|
||||
if (auth()->user()->id !== $user->id) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
$created_at = $invitation->created_at;
|
||||
$diff = $created_at->diffInMinutes(now());
|
||||
if ($diff <= config('constants.invitation.link.expiration')) {
|
||||
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
|
||||
$invitation->delete();
|
||||
return redirect()->route('team.show');
|
||||
} else {
|
||||
$invitation->delete();
|
||||
abort(401);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
public function revoke_invitation()
|
||||
{
|
||||
try {
|
||||
$invitation = TeamInvitation::whereUuid(request()->route('uuid'))->firstOrFail();
|
||||
$user = User::whereEmail($invitation->email)->firstOrFail();
|
||||
if (is_null(auth()->user())) {
|
||||
return redirect()->route('login');
|
||||
}
|
||||
if (auth()->user()->id !== $user->id) {
|
||||
abort(401);
|
||||
}
|
||||
$invitation->delete();
|
||||
return redirect()->route('team.show');
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
namespace App\Http\Livewire\Settings;
|
||||
|
||||
use App\Mail\TestTransactionalEmail;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Notifications\TestTransactionEmail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Notifications\TransactionalEmails\TestEmail;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Livewire\Component;
|
||||
|
||||
@@ -20,46 +18,17 @@ class Email extends Component
|
||||
'settings.extra_attributes.smtp_username' => 'nullable',
|
||||
'settings.extra_attributes.smtp_password' => 'nullable',
|
||||
'settings.extra_attributes.smtp_timeout' => 'nullable',
|
||||
'settings.extra_attributes.smtp_recipients' => 'required',
|
||||
'settings.extra_attributes.smtp_test_recipients' => 'nullable',
|
||||
'settings.extra_attributes.smtp_from_address' => 'required|email',
|
||||
'settings.extra_attributes.smtp_from_name' => 'required',
|
||||
];
|
||||
public function test_email()
|
||||
{
|
||||
Notification::send($this->settings, new TestTransactionEmail);
|
||||
Notification::send($this->settings, new TestEmail);
|
||||
}
|
||||
// public function test_email()
|
||||
// {
|
||||
// config()->set('mail.default', 'smtp');
|
||||
// config()->set('mail.mailers.smtp', [
|
||||
// "transport" => "smtp",
|
||||
// "host" => $this->settings->smtp_host,
|
||||
// "port" => $this->settings->smtp_port,
|
||||
// "encryption" => $this->settings->smtp_encryption,
|
||||
// "username" => $this->settings->smtp_username,
|
||||
// "password" => $this->settings->smtp_password,
|
||||
// ]);
|
||||
|
||||
// $this->send_email();
|
||||
// }
|
||||
// public function test_email_local()
|
||||
// {
|
||||
// config()->set('mail.default', 'smtp');
|
||||
// config()->set('mail.mailers.smtp', [
|
||||
// "transport" => "smtp",
|
||||
// "host" => 'coolify-mail',
|
||||
// "port" => 1025,
|
||||
// ]);
|
||||
// $this->send_email();
|
||||
// }
|
||||
// private function send_email()
|
||||
// {
|
||||
// }
|
||||
public function submit()
|
||||
{
|
||||
$this->validate();
|
||||
$this->settings->extra_attributes->smtp_recipients = str_replace(' ', '', $this->settings->extra_attributes->smtp_recipients);
|
||||
$this->settings->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->settings->extra_attributes->smtp_test_recipients);
|
||||
$this->settings->save();
|
||||
}
|
||||
|
||||
21
app/Http/Livewire/Team/Invitations.php
Normal file
21
app/Http/Livewire/Team/Invitations.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Team;
|
||||
|
||||
use App\Models\TeamInvitation;
|
||||
use Livewire\Component;
|
||||
|
||||
class Invitations extends Component
|
||||
{
|
||||
public $invitations;
|
||||
protected $listeners = ['refreshInvitations'];
|
||||
public function refreshInvitations()
|
||||
{
|
||||
$this->invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
|
||||
}
|
||||
public function deleteInvitation(int $invitation_id)
|
||||
{
|
||||
TeamInvitation::find($invitation_id)->delete();
|
||||
$this->refreshInvitations();
|
||||
}
|
||||
}
|
||||
@@ -4,42 +4,64 @@ namespace App\Http\Livewire\Team;
|
||||
|
||||
use App\Models\TeamInvitation;
|
||||
use App\Models\User;
|
||||
use App\Notifications\TransactionalEmails\InvitationLinkEmail;
|
||||
use Livewire\Component;
|
||||
use Visus\Cuid2\Cuid2;
|
||||
|
||||
class InviteLink extends Component
|
||||
{
|
||||
public string $email;
|
||||
public string $role = 'member';
|
||||
public function mount()
|
||||
{
|
||||
$this->email = config('app.env') === 'local' ? 'test@example.com' : '';
|
||||
$this->email = config('app.env') === 'local' ? 'test3@example.com' : '';
|
||||
}
|
||||
public function inviteByLink()
|
||||
public function viaEmail()
|
||||
{
|
||||
$this->generate_invite_link(isEmail: true);
|
||||
}
|
||||
private function generate_invite_link(bool $isEmail = false)
|
||||
{
|
||||
$uuid = new Cuid2(32);
|
||||
$link = url('/') . '/api/invitation/' . $uuid;
|
||||
try {
|
||||
$user_exists = User::whereEmail($this->email)->exists();
|
||||
if (!$user_exists) {
|
||||
$uuid = new Cuid2(32);
|
||||
$link = url('/') . config('constants.invitation.link.base_url') . $uuid;
|
||||
|
||||
$user = User::whereEmail($this->email);
|
||||
|
||||
if (!$user->exists()) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "$this->email must be registered first (or activate transactional emails to invite via email).");
|
||||
}
|
||||
$invitation = TeamInvitation::where('email', $this->email);
|
||||
|
||||
$member_emails = session('currentTeam')->members()->get()->pluck('email');
|
||||
if ($member_emails->contains($this->email)) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "$this->email is already a member of " . session('currentTeam')->name . ".");
|
||||
}
|
||||
|
||||
$invitation = TeamInvitation::whereEmail($this->email);
|
||||
|
||||
if ($invitation->exists()) {
|
||||
$created_at = $invitation->first()->created_at;
|
||||
$diff = $created_at->diffInMinutes(now());
|
||||
if ($diff < 11) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "Invitation already sent and active for $this->email.");
|
||||
if ($diff <= config('constants.invitation.link.expiration')) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "Invitation already sent to $this->email and waiting for action.");
|
||||
} else {
|
||||
$invitation->delete();
|
||||
}
|
||||
}
|
||||
$invitation = TeamInvitation::firstOrCreate([
|
||||
|
||||
TeamInvitation::firstOrCreate([
|
||||
'team_id' => session('currentTeam')->id,
|
||||
'uuid' => $uuid,
|
||||
'email' => $this->email,
|
||||
'role' => 'readonly',
|
||||
'role' => $this->role,
|
||||
'link' => $link,
|
||||
'via' => $isEmail ? 'email' : 'link',
|
||||
]);
|
||||
$this->emit('reloadWindow');
|
||||
if ($isEmail) {
|
||||
$user->first()->notify(new InvitationLinkEmail());
|
||||
}
|
||||
$this->emit('refreshInvitations');
|
||||
$this->emit('message', 'Invitation sent successfully.');
|
||||
} catch (\Throwable $e) {
|
||||
$error_message = $e->getMessage();
|
||||
if ($e->getCode() === '23505') {
|
||||
@@ -48,4 +70,8 @@ class InviteLink extends Component
|
||||
return general_error_handler(err: $e, that: $this, customErrorMessage: $error_message);
|
||||
}
|
||||
}
|
||||
public function inviteByLink()
|
||||
{
|
||||
$this->generate_invite_link();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class Member extends Component
|
||||
}
|
||||
public function makeReadonly()
|
||||
{
|
||||
$this->member->teams()->updateExistingPivot(session('currentTeam')->id, ['role' => 'readonly']);
|
||||
$this->member->teams()->updateExistingPivot(session('currentTeam')->id, ['role' => 'member']);
|
||||
$this->emit('reloadWindow');
|
||||
}
|
||||
public function remove()
|
||||
|
||||
@@ -18,7 +18,7 @@ class InstanceSettings extends Model implements SendsEmail
|
||||
{
|
||||
return $this->extra_attributes->modelScope();
|
||||
}
|
||||
public function routeNotificationForEmail(string $attribute = 'smtp_recipients')
|
||||
public function routeNotificationForEmail(string $attribute = 'smtp_test_recipients')
|
||||
{
|
||||
$recipients = $this->extra_attributes->get($attribute, '');
|
||||
if (is_null($recipients) || $recipients === '') {
|
||||
|
||||
@@ -8,9 +8,11 @@ class TeamInvitation extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'team_id',
|
||||
'uuid',
|
||||
'email',
|
||||
'role',
|
||||
'link',
|
||||
'via',
|
||||
];
|
||||
public function team()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Notifications\Channels\SendsEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@@ -10,7 +11,7 @@ use Laravel\Sanctum\HasApiTokens;
|
||||
use Visus\Cuid2\Cuid2;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
|
||||
class User extends Authenticatable
|
||||
class User extends Authenticatable implements SendsEmail
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
|
||||
protected $fillable = [
|
||||
@@ -46,10 +47,13 @@ class User extends Authenticatable
|
||||
$user->teams()->attach($new_team, ['role' => 'owner']);
|
||||
});
|
||||
}
|
||||
public function routeNotificationForEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
public function isAdmin()
|
||||
{
|
||||
if (auth()->user()->id === 0) {
|
||||
ray('is root user');
|
||||
return true;
|
||||
}
|
||||
$teams = $this->teams()->get();
|
||||
@@ -59,7 +63,6 @@ class User extends Authenticatable
|
||||
($is_part_of_root_team->pivot->role === 'admin' || $is_part_of_root_team->pivot->role === 'owner');
|
||||
|
||||
if ($is_part_of_root_team && $is_admin_of_root_team) {
|
||||
ray('is admin of root team');
|
||||
return true;
|
||||
}
|
||||
$role = $teams->where('id', session('currentTeam')->id)->first()->pivot->role;
|
||||
|
||||
@@ -12,11 +12,10 @@ class EmailChannel
|
||||
{
|
||||
$this->bootConfigs($notifiable);
|
||||
|
||||
if ($notification instanceof \App\Notifications\TestNotification) {
|
||||
$is_test_notification = $notification instanceof \App\Notifications\TestNotification;
|
||||
|
||||
if ($is_test_notification) {
|
||||
$bcc = $notifiable->routeNotificationForEmail('smtp_test_recipients');
|
||||
if (count($bcc) === 0) {
|
||||
$bcc = $notifiable->routeNotificationForEmail();
|
||||
}
|
||||
} else {
|
||||
$bcc = $notifiable->routeNotificationForEmail();
|
||||
}
|
||||
|
||||
51
app/Notifications/Channels/TransactionalEmailChannel.php
Normal file
51
app/Notifications/Channels/TransactionalEmailChannel.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\User;
|
||||
use Illuminate\Mail\Message;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class TransactionalEmailChannel
|
||||
{
|
||||
public function send(User $notifiable, Notification $notification): void
|
||||
{
|
||||
$email = $notifiable->email;
|
||||
if (!$email) {
|
||||
return;
|
||||
}
|
||||
$settings = InstanceSettings::get();
|
||||
$this->bootConfigs($settings);
|
||||
$mailMessage = $notification->toMail($notifiable);
|
||||
|
||||
Mail::send(
|
||||
[],
|
||||
[],
|
||||
fn (Message $message) => $message
|
||||
->from(
|
||||
$settings->extra_attributes?->get('smtp_from_address'),
|
||||
$settings->extra_attributes?->get('smtp_from_name')
|
||||
)
|
||||
->to($email)
|
||||
->subject($mailMessage->subject)
|
||||
->html((string)$mailMessage->render())
|
||||
);
|
||||
}
|
||||
|
||||
private function bootConfigs(InstanceSettings $settings): void
|
||||
{
|
||||
config()->set('mail.default', 'smtp');
|
||||
config()->set('mail.mailers.smtp', [
|
||||
"transport" => "smtp",
|
||||
"host" => $settings->extra_attributes?->get('smtp_host'),
|
||||
"port" => $settings->extra_attributes?->get('smtp_port'),
|
||||
"encryption" => $settings->extra_attributes?->get('smtp_encryption'),
|
||||
"username" => $settings->extra_attributes?->get('smtp_username'),
|
||||
"password" => $settings->extra_attributes?->get('smtp_password'),
|
||||
"timeout" => $settings->extra_attributes?->get('smtp_timeout'),
|
||||
"local_domain" => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -12,20 +12,6 @@ use Illuminate\Notifications\Notification;
|
||||
class TestNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
$channels = [];
|
||||
@@ -33,11 +19,7 @@ class TestNotification extends Notification implements ShouldQueue
|
||||
$notifiable->extra_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
|
||||
return $channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*/
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
public function toMail(): MailMessage
|
||||
{
|
||||
return (new MailMessage)
|
||||
->subject('Coolify Test Notification')
|
||||
@@ -45,20 +27,8 @@ class TestNotification extends Notification implements ShouldQueue
|
||||
->line('You have successfully received a test Email notification from Coolify. 🥳');
|
||||
}
|
||||
|
||||
public function toDiscord(object $notifiable): string
|
||||
public function toDiscord(): string
|
||||
{
|
||||
return 'You have successfully received a test Discord notification from Coolify. 🥳 [Go to your dashboard](' . url('/') . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Notifications\Channels\EmailChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class TestTransactionEmail extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
$channels = [];
|
||||
$notifiable->extra_attributes?->get('smtp_host') && $channels[] = EmailChannel::class;
|
||||
return $channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*/
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$mail = new MailMessage();
|
||||
$mail->subject('Coolify Test Notification');
|
||||
$mail->view('emails.test-email');
|
||||
return $mail;
|
||||
}
|
||||
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\TransactionalEmails;
|
||||
|
||||
use App\Models\Team;
|
||||
use App\Models\TeamInvitation;
|
||||
use App\Models\User;
|
||||
use App\Notifications\Channels\TransactionalEmailChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class InvitationLinkEmail extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
public function via()
|
||||
{
|
||||
return [TransactionalEmailChannel::class];
|
||||
}
|
||||
|
||||
public function toMail(User $user): MailMessage
|
||||
{
|
||||
$invitation = TeamInvitation::whereEmail($user->email)->first();
|
||||
$invitation_team = Team::find($invitation->team->id);
|
||||
|
||||
$mail = new MailMessage();
|
||||
$mail->subject('Invitation for ' . $invitation_team->name);
|
||||
$mail->view('emails.invitation-link', [
|
||||
'team' => $invitation_team->name,
|
||||
'email' => $user->email,
|
||||
'invitation_link' => $invitation->link,
|
||||
]);
|
||||
return $mail;
|
||||
}
|
||||
}
|
||||
26
app/Notifications/TransactionalEmails/TestEmail.php
Normal file
26
app/Notifications/TransactionalEmails/TestEmail.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\TransactionalEmails;
|
||||
|
||||
use App\Notifications\Channels\EmailChannel;
|
||||
use App\Notifications\Channels\TransactionalEmailChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class TestEmail extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
public function via(): array
|
||||
{
|
||||
return [EmailChannel::class];
|
||||
}
|
||||
public function toMail(): MailMessage
|
||||
{
|
||||
$mail = new MailMessage();
|
||||
$mail->subject('Coolify Test Notification');
|
||||
$mail->view('emails.test');
|
||||
return $mail;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user