freescout-restricted-customers/Providers/MMFRestrictedCustomersServiceProvider.php

140 lines
3.6 KiB
PHP

<?php
namespace Modules\MMFRestrictedCustomers\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;
use Eventy;
use App\User;
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() {
// 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 ( 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
);
}
/**
* 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->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 [];
}
}