Here is the implementation that I did. Definitively not the most elegant way, but it's working as intended by me ... Maybe it's useful for someone else who is looking for a kind of closed forum automatically approving known company mail domain users and notifying user and admin if manual approval is required:
`
<?php
/*
This file is part of Flarum.
For detailed copyright and license information, please view the
LICENSE file that was distributed with this source code.
*/
use Flarum\Extend;
use Flarum\User\Event\Registered;
use Flarum\User\Event\Activated;
use Flarum\Mail\Mailer;
use Flarum\Mail\DriverInterface;
use Flarum\User\User;
use Flarum\User\UserRepository;
use Flarum\Group\Group;
use Illuminate\Contracts\Bus\Dispatcher;
use Flarum\User\Command\RequestConfirmationEmail;
use Flarum\Notification\NotificationMailer;
use Illuminate\Mail\Message;
use Flarum\Settings\SettingsRepositoryInterface;
use Psr\Log\LoggerInterface;
use Illuminate\Support\HtmlString;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\MailableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserActivatedUserBlueprint implements BlueprintInterface, MailableInterface
{
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function getSubject()
{
return $this->user;
}
public function getFromUser()
{
return null;
}
public function getData()
{
return null;
}
public static function getType()
{
return 'userActivatedUser';
}
public static function getSubjectModel()
{
return User::class;
}
public function getEmailView()
{
return ['html' => new HtmlString("<h1>Hi {$this->user->display_name},</h1>"
. "<p>Your mail address has been verified successfully, but your registration could not be verified automatically.</p>"
. "<p>An admin will check your registration and activate it soon.</p>"
. "<p>Thank you for your patience!<br />Yours, bla bla Community Forum-Team" )];
}
public function getEmailSubject(TranslatorInterface $translator)
{
return 'Your registration for bla bla Community Forum is being checked! Please be patient for a moment.';
}
}
class UserActivatedAdminBlueprint implements BlueprintInterface, MailableInterface
{
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function getSubject()
{
return $this->user;
}
public function getFromUser()
{
return null;
}
public function getData()
{
return null;
}
public static function getType()
{
return 'userActivatedAdmin';
}
public static function getSubjectModel()
{
return User::class;
}
public function getEmailView()
{
return ['html' => new HtmlString("<h1>Manual verification of a user registration in the bla bla Community Forum required</h1>"
. "<p>Hi Admin,<br>A new user ({$this->user->display_name}) has registered and confirmed his e-mail address "
. "{$this->user->email} successfully.</p>"
. "<p>He/she could not be activated automatically, because the mail address is not one of our known customer "
. "mail domains.</p>"
. "<p>Please check the registration and approve it in the admin panel if applicable or contact the user, "
. "that he/she/they should register with a company email address. The link to the admin panel:</p>"
. "<p>...</p>"
. "<p>If it is a company address of one of our customers, please check and add it to the file autoapproved_domains.txt "
. "to include this previously unknown domain.</p>"
)];
}
public function getEmailSubject(TranslatorInterface $translator)
{
return "(!!) Manual verification of a user registration in the bla bla Community Forum required!";
}
}
use Flarum\Notification\NotificationSyncer;
class SendNotificationWhenUserIsActivated
{
/**
* @var NotificationSyncer
*/
protected $notifications;
/**
* @param NotificationSyncer $notifications
*/
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function handle(Activated $event)
{
$this->notifications->sync(
new UserActivatedBlueprint($event->user),
[$event->user]
);
}
}
return [
(new Extend\Event)
->listen(Registered::class, function (Registered $event) {
$user = $event->user;
$emailDomain = substr(strrchr($user->email, "@"), 1); // extract Domain
$autoApprovedDomainsFile = __DIR__ . '/autoapproved_domains.txt'; // file with allowed domains
// Read allowed domains for auto approval from file
$autoApprovedDomains = file_exists($autoApprovedDomainsFile)
? array_map('trim', file($autoApprovedDomainsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES))
: [];
// definine group names
$autoApprovedGroupName = 'Approved';
$defaultGroupName = 'Unapproved';
// Use group model to find IDs
$autoApprovedGroup = Group::where('name_singular', $autoApprovedGroupName)->orWhere('name_plural', $autoApprovedGroupName)->first();
$defaultGroup = Group::where('name_singular', $defaultGroupName)->orWhere('name_plural', $defaultGroupName)->first();
// Get group ID
$groupId = in_array($emailDomain, $autoApprovedDomains)
? ($autoApprovedGroup ? $autoApprovedGroup->id : null)
: ($defaultGroup ? $defaultGroup->id : null);
// Add group to user if group exists
if ($groupId) {
$user->groups()->attach($groupId);
$user->save();
}
}),
// *** NEW EVENT: Send after successful email address confirmation ***
(new Extend\Event)
->listen(Activated::class, function (Activated $event) {
$user = $event->user;
// Check, if user is in "Unapproved" group
$defaultGroup = Group::where('name_singular', 'Unapproved')->orWhere('name_plural', 'Unapproved')->first();
if ($defaultGroup && $user->groups()->where('id', $defaultGroup->id)->exists()) {
// Debugging: Log event
$logger = resolve(LoggerInterface::class); // Holt den Flarum-Logger
$logger->info("A new user has comfirmed his/her/their email address, BUT it's an unknown email domain: " . $user->email);
$user_repo = resolve(UserRepository::class);
$admin = $user_repo->findByIdentification('admin');
$mailer = resolve(NotificationMailer::class);
// email to Admin
$mailer->send(new UserActivatedAdminBlueprint($event->user), $admin);
// email to user
$mailer->send(new UserActivatedUserBlueprint($event->user), $user);
} else {
$logger = resolve(LoggerInterface::class);
$logger->info("A new user has comfirmed his/her/their email address and was approved automatically by his/her/their email domain: " . $user->email);
}
}),
];
`