2024-07-10 15:16:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\MMFRestrictedCustomers\Providers;
|
|
|
|
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
use Illuminate\Database\Eloquent\Factory;
|
2024-07-10 16:26:08 +00:00
|
|
|
use Eventy;
|
|
|
|
use App\User;
|
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();
|
|
|
|
$this->registerFactories();
|
|
|
|
$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.
|
|
|
|
$query_customers->whereIn('customers.mailbox_id', $mailbox_ids);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if ( isset($data['mailbox']) ) {
|
|
|
|
// TODO: Check that the current user is allowed to access this Mailbox.
|
|
|
|
$data['mailbox_id'] = $data['mailbox'];
|
|
|
|
unset($data['mailbox']);
|
|
|
|
}
|
|
|
|
// TODO: Throw an error if the Mailbox is not set.
|
|
|
|
$customer->mailbox_id = $data['mailbox_id'];
|
|
|
|
},
|
|
|
|
$priority = 20,
|
|
|
|
$arguments = 3
|
|
|
|
);
|
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() {
|
|
|
|
$this->loadJsonTranslationsFrom(__DIR__ .'/../Resources/lang');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 [];
|
|
|
|
}
|
|
|
|
}
|