Skip to content

Commit

Permalink
Merge pull request #209 from Sebbo94BY/Implement-sending-of-further-m…
Browse files Browse the repository at this point in the history
…ails

Implement sending of further mails
  • Loading branch information
Sebbo94BY authored Oct 28, 2023
2 parents e20ae99 + 1a0bb73 commit 05839bd
Show file tree
Hide file tree
Showing 43 changed files with 991 additions and 6 deletions.
2 changes: 2 additions & 0 deletions laravel/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ MAIL_ENCRYPTION=null
MAIL_VERIFY_PEER=true
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
MAIL_REPLY_TO_ADDRESS="${MAIL_FROM_ADDRESS}"
MAIL_REPLY_TO_NAME="${MAIL_FROM_NAME}"

MATOMO_ENABLED=false
MATOMO_BASE_URL=
Expand Down
10 changes: 9 additions & 1 deletion laravel/app/Http/Controllers/Administration/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use App\Http\Requests\UserAddRequest;
use App\Http\Requests\UserDeleteRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Mail\UserCreated;
use App\Models\Localization;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
use Illuminate\View\View;
Expand All @@ -24,6 +28,7 @@ public function users(): View
return view('administration.users', [
'users' => User::all(),
'roles' => Role::all(),
'localizations' => Localization::all(),
]);
}

Expand All @@ -38,6 +43,7 @@ public function create_user(UserAddRequest $request): RedirectResponse
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($initial_password);
$user->localization_id = $request->localization_id;

if (! $user->save()) {
return Redirect::route('administration.user.add')->withInput($request->all())->with([
Expand All @@ -50,9 +56,11 @@ public function create_user(UserAddRequest $request): RedirectResponse
$user->assignRole($role_id);
}

Mail::to($user)->send(new UserCreated(Auth::user(), $user, $initial_password));

return Redirect::route('administration.users')->with([
'success' => 'user-add-successful',
'message' => "Successfully added the new user. Initial password: $initial_password",
'message' => 'Successfully sent the new user an invitation email.',
]);
}

Expand Down
10 changes: 9 additions & 1 deletion laravel/app/Http/Controllers/Setup/Installer/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\InstallerAddUserRequest;
use App\Mail\SetupInstallerCompleted;
use App\Models\Localization;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

Expand All @@ -22,7 +25,9 @@ public function show_view(): RedirectResponse|View
return Redirect::route('dashboard');
}

return view('setup.installer.user');
return view('setup.installer.user', [
'localizations' => Localization::all(),
]);
}

/**
Expand All @@ -34,6 +39,7 @@ public function create(InstallerAddUserRequest $request): RedirectResponse
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->localization_id = $request->localization_id;

if (! $user->save()) {
return Redirect::route('setup.installer.user', ['locale' => $request->route()->parameter('locale')])->withInput($request->all())->with([
Expand All @@ -52,6 +58,8 @@ public function create(InstallerAddUserRequest $request): RedirectResponse
]);
$request->session()->regenerate();

Mail::to($user)->send(new SetupInstallerCompleted($user));

return Redirect::route('dashboard')->with([
'success' => 'installer-successful',
'message' => 'Successfully finished the installation. Enjoy the application!',
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Requests/InstallerAddUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function rules(): array
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'localization_id' => ['required', 'integer', 'exists:App\Models\Localization,id'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
];
}
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Requests/UserAddRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function rules(): array
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'localization_id' => ['required', 'integer', 'exists:App\Models\Localization,id'],
'roles' => ['required', 'array', 'min:1'],
];
}
Expand Down
60 changes: 60 additions & 0 deletions laravel/app/Mail/SetupInstallerCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class SetupInstallerCompleted extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

protected User $user;

/**
* Create a new message instance.
*/
public function __construct(User $user)
{
$this->user = $user;
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
to: $this->user->email,
subject: __('views/emails/setup/installer/completed.subject'),
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.setup.installer.completed',
with: [
'user' => $this->user,
],
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
68 changes: 68 additions & 0 deletions laravel/app/Mail/UserCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class UserCreated extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

protected User $auth_user;

protected User $user;

protected string $initial_password;

/**
* Create a new message instance.
*/
public function __construct(User $auth_user, User $user, string $initial_password)
{
$this->auth_user = $auth_user;
$this->user = $user;
$this->initial_password = $initial_password;
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
to: $this->user->email,
subject: __('views/emails/users/created.subject', ['app_name' => config('app.name')]),
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.users.created',
with: [
'auth_user' => $this->auth_user,
'user' => $this->user,
'initial_password' => $this->initial_password,
],
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
12 changes: 11 additions & 1 deletion laravel/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
class User extends Authenticatable implements HasLocalePreference
{
use HasRoles, HasApiTokens, HasFactory, Notifiable;

Expand Down Expand Up @@ -53,4 +55,12 @@ public function localization(): BelongsTo
{
return $this->belongsTo(Localization::class);
}

/**
* Get the user's preferred locale.
*/
public function preferredLocale(): string
{
return $this->localization->locale;
}
}
9 changes: 7 additions & 2 deletions laravel/config/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@
*/

'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'Example'),
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', config('app.name')),
],

'reply_to' => [
'address' => env('MAIL_REPLY_TO_ADDRESS', '[email protected]'),
'name' => env('MAIL_REPLY_TO_NAME', config('app.name')),
],

/*
Expand Down
2 changes: 1 addition & 1 deletion laravel/database/factories/LocalizationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function definition()
{
return [
'language_name' => fake()->text(16),
'locale' => fake()->languageCode(),
'locale' => fake()->randomElement(['en', 'de']), // only list here available locales
];
}
}
31 changes: 31 additions & 0 deletions laravel/lang/de/views/emails/setup/installer/completed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [

/**
* Mailable Subject
*/
'subject' => 'Installation abgeschlossen',

/**
* Mailable Body
*/
'greet_user' => 'Hallo <i>:username</i>',
'installation_completed' => 'Du hast das Projekt <a href="https://github.com/Sebbo94BY/teamspeak-dynamic-banner" target="_blank">TeamSpeak Dynamischer Banner</a> erfolgreich installiert.',
'check_system_status' => 'Zunächst einmal solltest du überprüfen, ob deine Anwendung voll funktionsfähig ist oder ob etwas zu beheben ist',
'open_system_status_page_button' => 'Öffne <b>Administration</b> > <b>System Status</b>',
'next_steps' => 'Nachdem du dich vergewissert hast, dass alles voll funktionsfähig ist, sollten deine nächsten Schritte folgende sein',
'next_steps_download_install_ttf_fonts' => 'Lade eine oder mehrere TrueType-Schriften herunter und installiere sie, wie unter <i>Administration</i> > <i>Schriftarten</i> erklärt',
'next_steps_add_configure_instance' => 'Füge und konfiguriere eine Instanz unter <i>Instanzen</i>',
'next_steps_start_instance' => 'Starte die neu erstellte Instanz, um die entsprechenden Variablen zu sammeln',
'next_steps_upload_templates' => 'Lade unter <i>Vorlagen</i> eine oder mehrere Vorlagen hoch',
'next_steps_add_banner' => 'Füge unter <i>Banner</i> einen neuen Banner hinzu',
'next_steps_add_templates_to_banner' => 'Füge eine oder mehrere kürzlich hochgeladene Vorlagen zum Banner hinzu',
'next_steps_configure_banner_templates' => 'Konfiguriere deine Bannervorlagen mit einigen Texten',
'next_steps_enable_banner_templates' => 'Aktiviere die Bannervorlagen in der Übersicht',
'next_steps_configure_banner_api_url' => 'Konfiguriere die Banner API URL in deinem TeamSpeak Server als Hostbanner Gfx URL',
'github_feature_request_issue_hint' => 'Du kannst <a href="https://github.com/Sebbo94BY/teamspeak-dynamic-banner/issues/new/choose" target="_blank">auf GitHub</a> gerne eine Feature-Anfrage stellen oder ein Problem melden.',
'automated_email_hint' => 'Dies ist eine automatisierte E-Mail. Bitte antworte nicht auf diese E-Mail.',
'closing_words' => 'Viel Spaß mit der Anwendung',

];
20 changes: 20 additions & 0 deletions laravel/lang/de/views/emails/users/created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/**
* Mailable Subject
*/
'subject' => 'Einladung zu :app_name',

/**
* Mailable Body
*/
'greet_user' => 'Hallo <i>:username</i>',
'invited_by' => 'Du wurdest durch <i>:username</i> zu <i>:app_name</i> eingeladen.',
'initial_password' => 'Dein initiales Passwort lautet <code>:initial_password</code>. Bitte ändere dies sofort nach dem Login.',
'open_login_page_button' => 'Login öffnen',
'automated_email_hint' => 'Dies ist eine automatisierte E-Mail. Bitte antworte nicht auf diese E-Mail.',
'closing_words' => 'Danke',

];
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
'roles' => 'Rollen',
'roles_help' => 'Die Rollen (und damit Berechtigungen), die der Benutzer erhalten soll.',

'language_label' => 'Sprache',
'language_help' => 'Die bevorzugte Sprache des neuen Benutzers.',

/**
* Buttons
*/
Expand All @@ -34,5 +37,6 @@
'name_validation_error' => 'Bitte gib einen gültigen Namen an.',
'email_validation_error' => 'Bitte gib eine gültige E-Mail Adresse an.',
'role_validation_error' => 'Bitte wähle mindestens eine verfügbare Rolle aus.',
'language_validation_error' => 'Bitte wähle eine verfügbare Sprache aus!',

];
4 changes: 4 additions & 0 deletions laravel/lang/de/views/setup/installer/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
'password_confirmation_placeholder' => 'z.B. meinSup3r!geh31mesP4ssw0rD',
'password_confirmation_help' => 'Wiederhole dein Passwort, um es zu bestätigen.',

'language_label' => 'Sprache',
'language_help' => 'Deine bevorzugte Sprache.',

/**
* Form Validation
*/
Expand All @@ -39,5 +42,6 @@
'email_validation_error' => 'Bitte gib eine gültige E-Mail Adresse an!',
'password_validation_error' => 'Bitte gib ein gültiges und sicheres Passwort an!',
'password_confirmation_validation_error' => 'Bitte wiederhole dein zuvor definiertes Passwort!',
'language_validation_error' => 'Bitte wähle eine verfügbare Sprache!',

];
Loading

0 comments on commit 05839bd

Please sign in to comment.