freescout-restricted-customers/Providers/MMFRestrictedCustomersServiceProvider.php

182 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2024-07-12 10:36:00 +00:00
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
namespace Modules\MMFRestrictedCustomers\Providers;
use Eventy;
use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use App\User;
use Modules\MMFRestrictedCustomers\Console\Commands\FetchEmails;
use Modules\MMFRestrictedCustomers\Entities\Customer;
class MMFRestrictedCustomersServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot() {
$this->registerConfig();
$this->registerViews();
$this->registerTranslations();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$this->commands([
FetchEmails::class,
]);
$this->hooks();
}
/**
* Module hooks.
*/
public function hooks() {
// When searching for customers, restrict the list to the ones the current user is allowed to see.
Eventy::addFilter('search.customers.apply_filters', function($query_customers) {
$user = auth()->user();
// Do not restrict the query if the current user is an admin.
if ( $user->role != User::ROLE_ADMIN ) {
// Get IDs of mailboxes the current user is allowed to access.
$mailboxes = $user->mailboxesIdsCanView();
// Restrict the query to the Customers the current user is allowed to access.
$query_customers->whereIn('customers.mailbox_id', $mailboxes);
}
return $query_customers;
});
// When creating or updating a customer, ensure its mailbox id is set.
Eventy::addAction(
'customer.set_data',
function($customer, $data, $replace_data) {
if ( array_key_exists('mailbox', $data) ) {
// TODO: Check that the current user is allowed to access this Mailbox.
$data['mailbox_id'] = $data['mailbox'];
unset($data['mailbox']);
}
if ( array_key_exists('mailbox_id', $data) ) {
$customer->mailbox_id = $data['mailbox_id'];
} else {
$customer_id = $customer->id;
if ( $customer_id == null ) {
Log::error('The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.');
// TODO: Error out more gracefully.
abort(500, 'The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.');
}
$customer = Customer::find($customer_id);
if ( $customer == null ) {
Log::error('Trying to use a Customer in another Mailbox than the one it is linked to is not allowed, and has been aborted.');
// TODO: Error out more gracefully.
abort(500, 'Trying to use a Customer in another Mailbox than the one it is linked to is not allowed, and has been aborted.');
}
if ( $customer->mailbox_id == null ) {
Log::error('The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.');
// TODO: Error out more gracefully.
abort(500, 'The creation of a Customer with no link to a Mailbox is not allowed, and has been aborted.');
}
}
},
$priority = 20,
$arguments = 3
);
// Filter the list of Customers returned by MMFCustomersGroups when creating/editing a Customers Group.
Eventy::addFilter('mmfcustomergroups.groups.create.customers-query', function($customers_query) {
$user = auth()->user();
// Do not restrict the query if the current user is an admin.
if ( $user->role != User::ROLE_ADMIN ) {
// Get IDs of mailboxes the current user is allowed to access.
$mailboxes = $user->mailboxesIdsCanView();
// Restrict the query to the Customers the current user is allowed to access.
$customers_query->whereIn('customers.mailbox_id', $mailboxes);
}
return $customers_query;
});
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->registerTranslations();
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig() {
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('mmfrestrictedcustomers.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../Config/config.php', 'mmfrestrictedcustomers'
);
}
/**
* Register views.
*
* @return void
*/
public function registerViews() {
$viewPath = resource_path('views/modules/mmfrestrictedcustomers');
$sourcePath = __DIR__.'/../Resources/views';
$this->publishes([
$sourcePath => $viewPath,
// Override the application view for the Conversations search results.
$sourcePath . '/conversations/search.blade.php' => resource_path('views') . '/conversations/search.blade.php',
],'views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/mmfrestrictedcustomers';
}, \Config::get('view.paths')), [$sourcePath]), 'mmfrestrictedcustomers');
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations() {
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'mmfrestrictedcustomers');
}
/**
* Register an additional directory of factories.
* @source https://github.com/sebastiaanluca/laravel-resource-flow/blob/develop/src/Modules/ModuleServiceProvider.php#L66
*/
public function registerFactories() {
if (! app()->environment('production')) {
app(Factory::class)->load(__DIR__ . '/../Database/factories');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return [];
}
}