2024-07-10 15:16:13 +00:00
|
|
|
<?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>
|
|
|
|
*/
|
2024-07-10 15:16:13 +00:00
|
|
|
|
|
|
|
namespace Modules\MMFRestrictedCustomers\Providers;
|
|
|
|
|
2024-07-10 16:26:08 +00:00
|
|
|
use Eventy;
|
2024-07-22 16:05:54 +00:00
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factory;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
2024-07-10 16:26:08 +00:00
|
|
|
use App\User;
|
2024-07-22 16:05:54 +00:00
|
|
|
|
2024-07-10 15:16:13 +00:00
|
|
|
use Modules\MMFRestrictedCustomers\Console\Commands\FetchEmails;
|
|
|
|
|
|
|
|
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();
|
2024-07-12 10:31:57 +00:00
|
|
|
$this->registerTranslations();
|
2024-07-10 15:16:13 +00:00
|
|
|
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
|
|
|
|
$this->commands([
|
|
|
|
FetchEmails::class,
|
|
|
|
]);
|
|
|
|
$this->hooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module hooks.
|
|
|
|
*/
|
|
|
|
public function hooks() {
|
2024-07-11 09:55:29 +00:00
|
|
|
// When searching for customers, restrict the list to the ones the current user is allowed to see.
|
2024-07-10 16:26:08 +00:00
|
|
|
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.
|
2024-07-11 14:48:54 +00:00
|
|
|
$query_customers->whereIn('customers.mailbox_id', $mailboxes);
|
2024-07-10 16:26:08 +00:00
|
|
|
}
|
|
|
|
return $query_customers;
|
|
|
|
});
|
2024-07-11 09:55:29 +00:00
|
|
|
|
|
|
|
// When creating or updating a customer, ensure its mailbox id is set.
|
|
|
|
Eventy::addAction(
|
|
|
|
'customer.set_data',
|
|
|
|
function($customer, $data, $replace_data) {
|
2024-07-22 16:05:54 +00:00
|
|
|
if ( array_key_exists('mailbox', $data) ) {
|
2024-07-11 09:55:29 +00:00
|
|
|
// TODO: Check that the current user is allowed to access this Mailbox.
|
|
|
|
$data['mailbox_id'] = $data['mailbox'];
|
|
|
|
unset($data['mailbox']);
|
|
|
|
}
|
2024-07-22 16:05:54 +00:00
|
|
|
if ( array_key_exists('mailbox_id', $data) ) {
|
|
|
|
$customer->mailbox_id = $data['mailbox_id'];
|
|
|
|
} else {
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 09:55:29 +00:00
|
|
|
},
|
|
|
|
$priority = 20,
|
|
|
|
$arguments = 3
|
|
|
|
);
|
2024-07-20 16:48:58 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
});
|
2024-07-10 15:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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([
|
2024-07-10 16:26:08 +00:00
|
|
|
$sourcePath => $viewPath,
|
|
|
|
// Override the application view for the Conversations search results.
|
|
|
|
$sourcePath . '/conversations/search.blade.php' => resource_path('views') . '/conversations/search.blade.php',
|
2024-07-10 15:16:13 +00:00
|
|
|
],'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() {
|
2024-07-12 10:31:57 +00:00
|
|
|
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'mmfrestrictedcustomers');
|
2024-07-10 15:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 [];
|
|
|
|
}
|
|
|
|
}
|