diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php index 96d5fab0ff..1ffe47fabd 100644 --- a/app/Handlers/Events/UserEventHandler.php +++ b/app/Handlers/Events/UserEventHandler.php @@ -33,15 +33,16 @@ use FireflyIII\Events\RequestedNewPassword; use FireflyIII\Events\UserChangedEmail; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Mail\ConfirmEmailChangeMail; -use FireflyIII\Mail\RegisteredUser as RegisteredUserMail; -use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail; use FireflyIII\Mail\UndoEmailChangeMail; use FireflyIII\Models\GroupMembership; use FireflyIII\Models\UserGroup; use FireflyIII\Models\UserRole; +use FireflyIII\Notifications\Admin\UserRegistration as AdminRegistrationNotification; use FireflyIII\Notifications\User\UserLogin; use FireflyIII\Notifications\User\UserNewPassword; +use FireflyIII\Notifications\User\UserRegistration as UserRegistrationNotification; use FireflyIII\Repositories\User\UserRepositoryInterface; +use FireflyIII\Support\Facades\FireflyConfig; use FireflyIII\User; use Illuminate\Auth\Events\Login; use Illuminate\Support\Facades\Notification; @@ -281,26 +282,28 @@ class UserEventHandler */ public function sendRegistrationMail(RegisteredUser $event): void { - $sendMail = config('firefly.send_registration_mail'); + $sendMail = FireflyConfig::get('notification_user_new_reg', true)->data; if ($sendMail) { - // get the email address - $email = $event->user->email; - $url = route('index'); + Notification::send($event->user, new UserRegistrationNotification); + } + } - // see if user has alternative email address: - $pref = app('preferences')->getForUser($event->user, 'remote_guard_alt_email'); - if (null !== $pref) { - $email = $pref->data; + /** + * @param RegisteredUser $event + * @return void + */ + public function sendAdminRegistrationNotification(RegisteredUser $event): void + { + $sendMail = FireflyConfig::get('notification_admin_new_reg', true)->data; + if ($sendMail) { + /** @var UserRepositoryInterface $repository */ + $repository = app(UserRepositoryInterface::class); + $all = $repository->all(); + foreach ($all as $user) { + if ($repository->hasRole($user, 'owner')) { + Notification::send($user, new AdminRegistrationNotification($event->user)); + } } - - // send email. - try { - Mail::to($email)->send(new RegisteredUserMail($url)); - - } catch (Exception $e) { // @phpstan-ignore-line - Log::error($e->getMessage()); - } - } } diff --git a/app/Http/Controllers/Admin/HomeController.php b/app/Http/Controllers/Admin/HomeController.php index c6527c210b..722dc2b575 100644 --- a/app/Http/Controllers/Admin/HomeController.php +++ b/app/Http/Controllers/Admin/HomeController.php @@ -26,6 +26,7 @@ use FireflyIII\Events\AdminRequestedTestMessage; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Middleware\IsDemoUser; +use FireflyIII\Support\Facades\FireflyConfig; use FireflyIII\User; use Illuminate\Contracts\View\Factory; use Illuminate\Http\RedirectResponse; @@ -68,9 +69,28 @@ class HomeController extends Controller if (null !== $pref && is_string($pref->data)) { $email = $pref->data; } - Log::debug('Email is ', [$email]); - return view('admin.index', compact('title', 'mainTitleIcon', 'email')); + // admin notification settings: + $notifications = []; + foreach (config('firefly.admin_notifications') as $item) { + $notifications[$item] = FireflyConfig::get(sprintf('notification_%s', $item), true)->data; + } + + return view('admin.index', compact('title', 'mainTitleIcon', 'email', 'notifications')); + } + + public function notifications(Request $request): RedirectResponse + { + foreach (config('firefly.admin_notifications') as $item) { + $value = false; + if ($request->has(sprintf('notification_%s', $item))) { + $value = true; + } + FireflyConfig::set(sprintf('notification_%s',$item), $value); + } + + session()->flash('success', (string) trans('firefly.notification_settings_saved')); + return redirect(route('admin.index')); } /** diff --git a/app/Notifications/Admin/UserRegistration.php b/app/Notifications/Admin/UserRegistration.php index 61605dca26..edfe77a3a1 100644 --- a/app/Notifications/Admin/UserRegistration.php +++ b/app/Notifications/Admin/UserRegistration.php @@ -22,7 +22,61 @@ declare(strict_types=1); namespace FireflyIII\Notifications\Admin; -class UserRegistration -{ +use FireflyIII\User; +use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Notification; +class UserRegistration extends Notification +{ + use Queueable; + + private User $user; + + /** + * Create a new notification instance. + * + * @return void + */ + public function __construct(User $user) + { + $this->user = $user; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage) + ->markdown('emails.registered-admin', ['email' => $this->user->email, 'id' => $this->user->id]) + ->subject((string) trans('email.registered_subject_admin')); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } } diff --git a/app/Notifications/User/UserRegistration.php b/app/Notifications/User/UserRegistration.php new file mode 100644 index 0000000000..521361e5eb --- /dev/null +++ b/app/Notifications/User/UserRegistration.php @@ -0,0 +1,79 @@ +. + */ + +namespace FireflyIII\Notifications\User; + +use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Notification; + +class UserRegistration extends Notification +{ + use Queueable; + + /** + * Create a new notification instance. + * + * @return void + */ + public function __construct() + { + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage) + ->markdown('emails.registered', ['address' => route('index')]) + ->subject((string) trans('email.registered_subject')); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} + diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 1b9e3c2f33..8fa3c3635d 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -73,6 +73,7 @@ class EventServiceProvider extends ServiceProvider // is a User related event. RegisteredUser::class => [ 'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail', + 'FireflyIII\Handlers\Events\UserEventHandler@sendAdminRegistrationNotification', 'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole', 'FireflyIII\Handlers\Events\UserEventHandler@createGroupMembership', 'FireflyIII\Handlers\Events\UserEventHandler@createExchangeRates', diff --git a/config/firefly.php b/config/firefly.php index 736a469bec..7e4e886d68 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -145,7 +145,8 @@ return [ 'update_minimum_age' => 7, // notifications - 'available_notifications' => ['bill_reminder', 'new_access_token', 'transaction_creation', 'user_login'], + 'available_notifications' => ['bill_reminder', 'new_access_token', 'transaction_creation', 'user_login'], + 'admin_notifications' => ['admin_new_reg', 'user_new_reg'], // enabled languages 'languages' => [ diff --git a/resources/lang/en_US/email.php b/resources/lang/en_US/email.php index cf1c77981d..b33fb9c907 100644 --- a/resources/lang/en_US/email.php +++ b/resources/lang/en_US/email.php @@ -49,6 +49,8 @@ return [ // registered 'registered_subject' => 'Welcome to Firefly III!', + 'registered_subject_admin' => 'A new user has registered', + 'admin_new_user_registered' => 'A new user has registered. User **:email** was given user ID #:id.', 'registered_welcome' => 'Welcome to [Firefly III](:address). Your registration has made it, and this email is here to confirm it. Yay!', 'registered_pw' => 'If you have forgotten your password already, please reset it using [the password reset tool](:address/password/reset).', 'registered_help' => 'There is a help-icon in the top right corner of each page. If you need help, click it!', diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 2f4a2f6c87..e18b4bb09f 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -1936,55 +1936,62 @@ return [ 'updated_tag' => 'Updated tag ":tag"', 'created_tag' => 'Tag ":tag" has been created!', - 'transaction_journal_information' => 'Transaction information', - 'transaction_journal_meta' => 'Meta information', - 'transaction_journal_more' => 'More information', - 'basic_journal_information' => 'Basic transaction information', - 'transaction_journal_extra' => 'Extra information', - 'att_part_of_journal' => 'Stored under ":journal"', - 'total_amount' => 'Total amount', - 'number_of_decimals' => 'Number of decimals', + 'transaction_journal_information' => 'Transaction information', + 'transaction_journal_meta' => 'Meta information', + 'transaction_journal_more' => 'More information', + 'basic_journal_information' => 'Basic transaction information', + 'transaction_journal_extra' => 'Extra information', + 'att_part_of_journal' => 'Stored under ":journal"', + 'total_amount' => 'Total amount', + 'number_of_decimals' => 'Number of decimals', // administration - 'administration' => 'Administration', - 'user_administration' => 'User administration', - 'list_all_users' => 'All users', - 'all_users' => 'All users', - 'instance_configuration' => 'Configuration', - 'firefly_instance_configuration' => 'Configuration options for Firefly III', - 'setting_single_user_mode' => 'Single user mode', - 'setting_single_user_mode_explain' => 'By default, Firefly III only accepts one (1) registration: you. This is a security measure, preventing others from using your instance unless you allow them to. Future registrations are blocked. When you uncheck this box, others can use your instance as well, assuming they can reach it (when it is connected to the internet).', - 'store_configuration' => 'Store configuration', - 'single_user_administration' => 'User administration for :email', - 'edit_user' => 'Edit user :email', - 'hidden_fields_preferences' => 'You can enable more transaction options in your preferences.', - 'user_data_information' => 'User data', - 'user_information' => 'User information', - 'total_size' => 'total size', - 'budget_or_budgets' => ':count budget|:count budgets', - 'budgets_with_limits' => ':count budget with configured amount|:count budgets with configured amount', - 'nr_of_rules_in_total_groups' => ':count_rules rule(s) in :count_groups rule group(s)', - 'tag_or_tags' => ':count tag|:count tags', - 'configuration_updated' => 'The configuration has been updated', - 'setting_is_demo_site' => 'Demo site', - 'setting_is_demo_site_explain' => 'If you check this box, this installation will behave as if it is the demo site, which can have weird side effects.', - 'block_code_bounced' => 'Email message(s) bounced', - 'block_code_expired' => 'Demo account expired', - 'no_block_code' => 'No reason for block or user not blocked', - 'block_code_email_changed' => 'User has not yet confirmed new email address', - 'admin_update_email' => 'Contrary to the profile page, the user will NOT be notified their email address has changed!', - 'update_user' => 'Update user', - 'updated_user' => 'User data has been changed.', - 'delete_user' => 'Delete user :email', - 'user_deleted' => 'The user has been deleted', - 'send_test_email' => 'Send test email message', - 'send_test_email_text' => 'To see if your installation is capable of sending email, please press this button. You will not see an error here (if any), the log files will reflect any errors. You can press this button as many times as you like. There is no spam control. The message will be sent to :email and should arrive shortly.', - 'send_message' => 'Send message', - 'send_test_triggered' => 'Test was triggered. Check your inbox and the log files.', - 'give_admin_careful' => 'Users who are given admin rights can take away yours. Be careful.', - 'admin_maintanance_title' => 'Maintenance', - 'admin_maintanance_expl' => 'Some nifty buttons for Firefly III maintenance', - 'admin_maintenance_clear_cache' => 'Clear cache', + 'administration' => 'Administration', + 'user_administration' => 'User administration', + 'list_all_users' => 'All users', + 'all_users' => 'All users', + 'instance_configuration' => 'Configuration', + 'firefly_instance_configuration' => 'Configuration options for Firefly III', + 'setting_single_user_mode' => 'Single user mode', + 'setting_single_user_mode_explain' => 'By default, Firefly III only accepts one (1) registration: you. This is a security measure, preventing others from using your instance unless you allow them to. Future registrations are blocked. When you uncheck this box, others can use your instance as well, assuming they can reach it (when it is connected to the internet).', + 'store_configuration' => 'Store configuration', + 'single_user_administration' => 'User administration for :email', + 'edit_user' => 'Edit user :email', + 'hidden_fields_preferences' => 'You can enable more transaction options in your preferences.', + 'user_data_information' => 'User data', + 'user_information' => 'User information', + 'total_size' => 'total size', + 'budget_or_budgets' => ':count budget|:count budgets', + 'budgets_with_limits' => ':count budget with configured amount|:count budgets with configured amount', + 'nr_of_rules_in_total_groups' => ':count_rules rule(s) in :count_groups rule group(s)', + 'tag_or_tags' => ':count tag|:count tags', + 'configuration_updated' => 'The configuration has been updated', + 'setting_is_demo_site' => 'Demo site', + 'setting_is_demo_site_explain' => 'If you check this box, this installation will behave as if it is the demo site, which can have weird side effects.', + 'block_code_bounced' => 'Email message(s) bounced', + 'block_code_expired' => 'Demo account expired', + 'no_block_code' => 'No reason for block or user not blocked', + 'block_code_email_changed' => 'User has not yet confirmed new email address', + 'admin_update_email' => 'Contrary to the profile page, the user will NOT be notified their email address has changed!', + 'update_user' => 'Update user', + 'updated_user' => 'User data has been changed.', + 'delete_user' => 'Delete user :email', + 'user_deleted' => 'The user has been deleted', + 'send_test_email' => 'Send test email message', + 'send_test_email_text' => 'To see if your installation is capable of sending email, please press this button. You will not see an error here (if any), the log files will reflect any errors. You can press this button as many times as you like. There is no spam control. The message will be sent to :email and should arrive shortly.', + 'send_message' => 'Send message', + 'send_test_triggered' => 'Test was triggered. Check your inbox and the log files.', + 'give_admin_careful' => 'Users who are given admin rights can take away yours. Be careful.', + 'admin_maintanance_title' => 'Maintenance', + 'admin_maintanance_expl' => 'Some nifty buttons for Firefly III maintenance', + 'admin_maintenance_clear_cache' => 'Clear cache', + 'admin_notifications' => 'Admin notifications', + 'admin_notifications_expl' => 'The following notifications can be enabled or disabled by the administrator.', + 'admin_notification_check_user_new_reg' => 'User gets post-registration welcome message', + 'admin_notification_check_admin_new_reg' => 'Administrator(s) get new user registration notification', + 'save_notification_settings' => 'Save settings', + 'notification_settings_saved' => 'The notification settings have been saved', + 'split_transaction_title' => 'Description of the split transaction', 'split_transaction_title_help' => 'If you create a split transaction, there must be a global description for all splits of the transaction.', diff --git a/resources/views/admin/index.twig b/resources/views/admin/index.twig index 3b4171337b..f045d1bf33 100644 --- a/resources/views/admin/index.twig +++ b/resources/views/admin/index.twig @@ -12,7 +12,9 @@
@@ -28,6 +30,31 @@
+
+ +
+
+

{{ 'admin_notifications'|_ }}

+
+
+

+ {{ 'admin_notifications_expl'|_ }} +

+ {% for notification, value in notifications %} +
+ +
+ {% endfor %} +
+ +
+
@@ -35,11 +62,11 @@

{{ 'send_test_email'|_ }}

+

{{ trans('firefly.send_test_email_text', {email:email})|raw }}

-
diff --git a/resources/views/emails/registered-admin.blade.php b/resources/views/emails/registered-admin.blade.php new file mode 100644 index 0000000000..ea3649e612 --- /dev/null +++ b/resources/views/emails/registered-admin.blade.php @@ -0,0 +1,3 @@ +@component('mail::message') +{{ trans('email.admin_new_user_registered', ['id' => $id,'email' => $email]) }} +@endcomponent diff --git a/routes/web.php b/routes/web.php index c90fb57ee9..af9ac1a9f2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1085,6 +1085,7 @@ Route::group( // admin home Route::get('', ['uses' => 'HomeController@index', 'as' => 'index']); Route::post('test-message', ['uses' => 'HomeController@testMessage', 'as' => 'test-message']); + Route::post('notifications', ['uses' => 'HomeController@notifications', 'as' => 'notifications']); // check for updates? Route::get('update-check', ['uses' => 'UpdateController@index', 'as' => 'update-check']);