v2 first commit
This commit is contained in:
@ -2,12 +2,8 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Module\Module;
|
||||
use App\Events\AdminMenuCreated;
|
||||
use Auth;
|
||||
use App\Models\Common\Dashboard;
|
||||
use Closure;
|
||||
use Menu;
|
||||
use Module as LaravelModule;
|
||||
|
||||
class AdminMenu
|
||||
{
|
||||
@ -21,68 +17,81 @@ class AdminMenu
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Check if logged in
|
||||
if (!Auth::check()) {
|
||||
if (!auth()->check()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Setup the admin menu
|
||||
Menu::create('AdminMenu', function ($menu) {
|
||||
$menu->style('adminlte');
|
||||
menu()->create('admin', function ($menu) {
|
||||
event(new \App\Events\Menu\AdminCreating($menu));
|
||||
|
||||
$user = Auth::user();
|
||||
$attr = ['icon' => 'fa fa-angle-double-right'];
|
||||
$menu->style('argon');
|
||||
|
||||
$user = user();
|
||||
$attr = ['icon' => ''];
|
||||
|
||||
// Dashboard
|
||||
$menu->add([
|
||||
'url' => '/',
|
||||
'title' => trans('general.dashboard'),
|
||||
'icon' => 'fa fa-dashboard',
|
||||
'order' => 1,
|
||||
]);
|
||||
$dashboards = Dashboard::getByUser($user->id);
|
||||
|
||||
if ($dashboards->count() > 1) {
|
||||
$menu->dropdown(trans_choice('general.dashboards', 2), function ($sub) use ($user, $attr, $dashboards) {
|
||||
foreach ($dashboards as $key => $dashboard) {
|
||||
$path = (session('dashboard_id') == $dashboard->id) ? '/' : '/?dashboard_id=' . $dashboard->id;
|
||||
|
||||
$sub->url($path, $dashboard->name, $key, $attr);
|
||||
}
|
||||
}, 1, [
|
||||
'url' => '/',
|
||||
'title' => trans_choice('general.incomes', 2),
|
||||
'icon' => 'fa fa-tachometer-alt',
|
||||
]);
|
||||
} else {
|
||||
$menu->add([
|
||||
'url' => '/',
|
||||
'title' => trans_choice('general.dashboards', 1),
|
||||
'icon' => 'fa fa-tachometer-alt',
|
||||
'order' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
// Items
|
||||
if ($user->can('read-common-items')) {
|
||||
$menu->add([
|
||||
'url' => 'common/items',
|
||||
'title' => trans_choice('general.items', 2),
|
||||
'icon' => 'fa fa-cubes',
|
||||
'order' => 2,
|
||||
]);
|
||||
$menu->route('items.index', trans_choice('general.items', 2), [], 2, ['icon' => 'fa fa-cube']);
|
||||
}
|
||||
|
||||
// Incomes
|
||||
if ($user->can(['read-incomes-invoices', 'read-incomes-revenues', 'read-incomes-customers'])) {
|
||||
$menu->dropdown(trans_choice('general.incomes', 2), function ($sub) use($user, $attr) {
|
||||
$menu->dropdown(trans_choice('general.incomes', 2), function ($sub) use ($user, $attr) {
|
||||
if ($user->can('read-incomes-invoices')) {
|
||||
$sub->url('incomes/invoices', trans_choice('general.invoices', 2), 1, $attr);
|
||||
$sub->route('invoices.index', trans_choice('general.invoices', 2), [], 1, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-incomes-revenues')) {
|
||||
$sub->url('incomes/revenues', trans_choice('general.revenues', 2), 2, $attr);
|
||||
$sub->route('revenues.index', trans_choice('general.revenues', 2), [], 2, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-incomes-customers')) {
|
||||
$sub->url('incomes/customers', trans_choice('general.customers', 2), 3, $attr);
|
||||
$sub->route('customers.index', trans_choice('general.customers', 2), [], 3, $attr);
|
||||
}
|
||||
}, 3, [
|
||||
'title' => trans_choice('general.incomes', 2),
|
||||
'icon' => 'fa fa-money',
|
||||
'icon' => 'fa fa-money-bill',
|
||||
]);
|
||||
}
|
||||
|
||||
// Expenses
|
||||
if ($user->can(['read-expenses-bills', 'read-expenses-payments', 'read-expenses-vendors'])) {
|
||||
$menu->dropdown(trans_choice('general.expenses', 2), function ($sub) use($user, $attr) {
|
||||
$menu->dropdown(trans_choice('general.expenses', 2), function ($sub) use ($user, $attr) {
|
||||
if ($user->can('read-expenses-bills')) {
|
||||
$sub->url('expenses/bills', trans_choice('general.bills', 2), 1, $attr);
|
||||
$sub->route('bills.index', trans_choice('general.bills', 2), [], 1, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-expenses-payments')) {
|
||||
$sub->url('expenses/payments', trans_choice('general.payments', 2), 2, $attr);
|
||||
$sub->route('payments.index', trans_choice('general.payments', 2), [], 2, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-expenses-vendors')) {
|
||||
$sub->url('expenses/vendors', trans_choice('general.vendors', 2), 3, $attr);
|
||||
$sub->route('vendors.index', trans_choice('general.vendors', 2), [], 3, $attr);
|
||||
}
|
||||
}, 4, [
|
||||
'title' => trans_choice('general.expenses', 2),
|
||||
@ -92,118 +101,44 @@ class AdminMenu
|
||||
|
||||
// Banking
|
||||
if ($user->can(['read-banking-accounts', 'read-banking-transfers', 'read-banking-transactions', 'read-banking-reconciliations'])) {
|
||||
$menu->dropdown(trans('general.banking'), function ($sub) use($user, $attr) {
|
||||
$menu->dropdown(trans('general.banking'), function ($sub) use ($user, $attr) {
|
||||
if ($user->can('read-banking-accounts')) {
|
||||
$sub->url('banking/accounts', trans_choice('general.accounts', 2), 1, $attr);
|
||||
$sub->route('accounts.index', trans_choice('general.accounts', 2), [], 1, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-banking-transfers')) {
|
||||
$sub->url('banking/transfers', trans_choice('general.transfers', 2), 2, $attr);
|
||||
$sub->route('transfers.index', trans_choice('general.transfers', 2), [], 2, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-banking-transactions')) {
|
||||
$sub->url('banking/transactions', trans_choice('general.transactions', 2), 3, $attr);
|
||||
$sub->route('transactions.index', trans_choice('general.transactions', 2), [], 3, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-banking-reconciliations')) {
|
||||
$sub->url('banking/reconciliations', trans_choice('general.reconciliations', 2), 4, $attr);
|
||||
$sub->route('reconciliations.index', trans_choice('general.reconciliations', 2), [], 4, $attr);
|
||||
}
|
||||
}, 5, [
|
||||
'title' => trans('general.banking'),
|
||||
'icon' => 'fa fa-university',
|
||||
'icon' => 'fa fa-briefcase',
|
||||
]);
|
||||
}
|
||||
|
||||
// Reports
|
||||
if ($user->can([
|
||||
'read-reports-income-summary',
|
||||
'read-reports-expense-summary',
|
||||
'read-reports-income-expense-summary',
|
||||
'read-reports-tax-summary',
|
||||
'read-reports-profit-loss',
|
||||
])) {
|
||||
$menu->dropdown(trans_choice('general.reports', 2), function ($sub) use($user, $attr) {
|
||||
if ($user->can('read-reports-income-summary')) {
|
||||
$sub->url('reports/income-summary', trans('reports.summary.income'), 1, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-reports-expense-summary')) {
|
||||
$sub->url('reports/expense-summary', trans('reports.summary.expense'), 2, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-reports-income-expense-summary')) {
|
||||
$sub->url('reports/income-expense-summary', trans('reports.summary.income_expense'), 3, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-reports-tax-summary')) {
|
||||
$sub->url('reports/tax-summary', trans('reports.summary.tax'), 4, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-reports-profit-loss')) {
|
||||
$sub->url('reports/profit-loss', trans('reports.profit_loss'), 5, $attr);
|
||||
}
|
||||
}, 6, [
|
||||
'title' => trans_choice('general.reports', 2),
|
||||
'icon' => 'fa fa-bar-chart',
|
||||
]);
|
||||
if ($user->can('read-common-reports')) {
|
||||
$menu->route('reports.index', trans_choice('general.reports', 2), [], 6, ['icon' => 'fa fa-chart-pie']);
|
||||
}
|
||||
|
||||
// Settings
|
||||
if ($user->can(['read-settings-settings', 'read-settings-categories', 'read-settings-currencies', 'read-settings-taxes'])) {
|
||||
$menu->dropdown(trans_choice('general.settings', 2), function ($sub) use($user, $attr) {
|
||||
if ($user->can('read-settings-settings')) {
|
||||
$sub->url('settings/settings', trans('general.general'), 1, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-settings-categories')) {
|
||||
$sub->url('settings/categories', trans_choice('general.categories', 2), 2, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-settings-currencies')) {
|
||||
$sub->url('settings/currencies', trans_choice('general.currencies', 2), 3, $attr);
|
||||
}
|
||||
|
||||
if ($user->can('read-settings-taxes')) {
|
||||
$sub->url('settings/taxes', trans_choice('general.tax_rates', 2), 4, $attr);
|
||||
}
|
||||
|
||||
// Modules
|
||||
$modules = Module::all();
|
||||
$position = 5;
|
||||
foreach ($modules as $module) {
|
||||
if (!$module->status) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$m = LaravelModule::findByAlias($module->alias);
|
||||
|
||||
// Check if the module exists and has settings
|
||||
if (!$m || empty($m->get('settings'))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sub->url('settings/apps/' . $module->alias, title_case(str_replace('_', ' ', snake_case($m->getName()))), $position, $attr);
|
||||
|
||||
$position++;
|
||||
}
|
||||
}, 7, [
|
||||
'title' => trans_choice('general.settings', 2),
|
||||
'icon' => 'fa fa-gears',
|
||||
]);
|
||||
if ($user->can('read-settings-settings')) {
|
||||
$menu->route('settings.index', trans_choice('general.settings', 2), [], 7, ['icon' => 'fa fa-cog']);
|
||||
}
|
||||
|
||||
// Apps
|
||||
if ($user->can('read-modules-home')) {
|
||||
$menu->add([
|
||||
'url' => 'apps/home',
|
||||
'title' => trans_choice('general.modules', 2),
|
||||
'icon' => 'fa fa-rocket',
|
||||
'order' => 8,
|
||||
]);
|
||||
$menu->route('apps.home.index', trans_choice('general.modules', 2), [], 8, ['icon' => 'fa fa-rocket']);
|
||||
}
|
||||
|
||||
// Fire the event to extend the menu
|
||||
event(new AdminMenuCreated($menu));
|
||||
event(new \App\Events\Menu\AdminCreated($menu));
|
||||
});
|
||||
|
||||
return $next($request);
|
||||
|
@ -3,9 +3,12 @@
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use App\Traits\Users;
|
||||
|
||||
class ApiCompany
|
||||
{
|
||||
use Users;
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
@ -22,8 +25,7 @@ class ApiCompany
|
||||
}
|
||||
|
||||
// Check if user can access company
|
||||
$companies = app('Dingo\Api\Auth\Auth')->user()->companies()->pluck('id')->toArray();
|
||||
if (!in_array($company_id, $companies)) {
|
||||
if (!$this->isUserCompany($company_id)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
21
app/Http/Middleware/Authenticate.php
Normal file
21
app/Http/Middleware/Authenticate.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Auth\Middleware\Authenticate as Middleware;
|
||||
|
||||
class Authenticate extends Middleware
|
||||
{
|
||||
/**
|
||||
* Get the path the user should be redirected to when they are not authenticated.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return string
|
||||
*/
|
||||
protected function redirectTo($request)
|
||||
{
|
||||
if (!$request->expectsJson()) {
|
||||
return route('login');
|
||||
}
|
||||
}
|
||||
}
|
28
app/Http/Middleware/CanApiKey.php
Normal file
28
app/Http/Middleware/CanApiKey.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class CanApiKey
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request"
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if ($request['alias'] != 'core') {
|
||||
if (setting('apps.api_key')) {
|
||||
return $next($request);
|
||||
} else {
|
||||
redirect('apps/api-key/create')->send();
|
||||
}
|
||||
} else {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Events\CustomerMenuCreated;
|
||||
use Auth;
|
||||
use Closure;
|
||||
use Menu;
|
||||
|
||||
class CustomerMenu
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Check if logged in
|
||||
if (!Auth::check()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
Menu::create('CustomerMenu', function ($menu) {
|
||||
$menu->style('adminlte');
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
// Dashboard
|
||||
$menu->add([
|
||||
'url' => 'customers/',
|
||||
'title' => trans('general.dashboard'),
|
||||
'icon' => 'fa fa-dashboard',
|
||||
'order' => 1,
|
||||
]);
|
||||
|
||||
// Invoices
|
||||
$menu->add([
|
||||
'url' => 'customers/invoices',
|
||||
'title' => trans_choice('general.invoices', 2),
|
||||
'icon' => 'fa fa-wpforms',
|
||||
'order' => 2,
|
||||
]);
|
||||
|
||||
// Payments
|
||||
$menu->add([
|
||||
'url' => 'customers/payments',
|
||||
'title' => trans_choice('general.payments', 2),
|
||||
'icon' => 'fa fa-money',
|
||||
'order' => 3,
|
||||
]);
|
||||
|
||||
// Transactions
|
||||
$menu->add([
|
||||
'url' => 'customers/transactions',
|
||||
'title' => trans_choice('general.transactions', 2),
|
||||
'icon' => 'fa fa-list',
|
||||
'order' => 4,
|
||||
]);
|
||||
|
||||
// Fire the event to extend the menu
|
||||
event(new CustomerMenuCreated($menu));
|
||||
});
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
|
||||
|
||||
class EncryptCookies extends BaseEncrypter
|
||||
class EncryptCookies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the cookies that should not be encrypted.
|
||||
|
@ -26,5 +26,4 @@ class LoadSettings
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
28
app/Http/Middleware/LogoutIfUserDisabled.php
Normal file
28
app/Http/Middleware/LogoutIfUserDisabled.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class LogoutIfUserDisabled
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$user = user();
|
||||
|
||||
if (!$user || $user->enabled) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
auth()->logout();
|
||||
|
||||
return redirect()->route('login');
|
||||
}
|
||||
}
|
@ -23,10 +23,10 @@ class Money
|
||||
$purchase_price = $request->get('purchase_price');
|
||||
$opening_balance = $request->get('opening_balance');
|
||||
$currency_code = $request->get('currency_code');
|
||||
$items = $request->get('item');
|
||||
$items = $request->get('items');
|
||||
|
||||
if (empty($currency_code)) {
|
||||
$currency_code = setting('general.default_currency');
|
||||
$currency_code = setting('default.currency');
|
||||
}
|
||||
|
||||
if (!empty($amount)) {
|
||||
@ -49,7 +49,7 @@ class Money
|
||||
}
|
||||
}
|
||||
|
||||
$request->request->set('item', $items);
|
||||
$request->request->set('items', $items);
|
||||
}
|
||||
}
|
||||
|
||||
|
47
app/Http/Middleware/PortalMenu.php
Normal file
47
app/Http/Middleware/PortalMenu.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class PortalMenu
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Check if logged in
|
||||
if (!auth()->check()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
menu()->create('portal', function ($menu) {
|
||||
event(new \App\Events\Menu\PortalCreating($menu));
|
||||
|
||||
$menu->style('argon');
|
||||
|
||||
$user = user();
|
||||
|
||||
// Dashboard
|
||||
$menu->route('portal.dashboard', trans_choice('general.dashboards', 1), [], 1, ['icon' => 'fa fa-tachometer-alt']);
|
||||
|
||||
// Invoices
|
||||
$menu->route('portal.invoices.index', trans_choice('general.invoices', 2), [], 2, ['icon' => 'fa fa-money-bill']);
|
||||
|
||||
// Payments
|
||||
$menu->route('portal.payments.index', trans_choice('general.payments', 2), [], 3, ['icon' => 'fa fa-shopping-cart']);
|
||||
|
||||
// Transactions
|
||||
$menu->route('portal.transactions.index', trans_choice('general.transactions', 2), [], 4, ['icon' => 'fa fa-briefcase']);
|
||||
|
||||
event(new \App\Events\Menu\PortalCreated($menu));
|
||||
});
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Auth;
|
||||
use Closure;
|
||||
|
||||
class RedirectIfAuthenticated
|
||||
@ -17,12 +16,12 @@ class RedirectIfAuthenticated
|
||||
*/
|
||||
public function handle($request, Closure $next, $guard = null)
|
||||
{
|
||||
if (Auth::guard($guard)->check()) {
|
||||
if (Auth::user()->customer) {
|
||||
return redirect('/customers');
|
||||
if (auth()->guard($guard)->check()) {
|
||||
if (user()->contact) {
|
||||
return redirect()->route('portal.dashboard');
|
||||
}
|
||||
|
||||
return redirect('/');
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
@ -4,6 +4,7 @@ namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RedirectIfNotInstalled
|
||||
{
|
||||
@ -22,11 +23,11 @@ class RedirectIfNotInstalled
|
||||
}
|
||||
|
||||
// Already in the wizard
|
||||
if (starts_with($request->getPathInfo(), '/install')) {
|
||||
if (Str::startsWith($request->getPathInfo(), '/install')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Not installed, redirect to installation wizard
|
||||
redirect('install/requirements')->send();
|
||||
redirect()->route('install.requirements')->send();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RedirectIfWizardCompleted
|
||||
{
|
||||
@ -16,12 +17,12 @@ class RedirectIfWizardCompleted
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Not in wizard
|
||||
if (!starts_with($request->getPathInfo(), '/wizard')) {
|
||||
if (!Str::startsWith($request->getPathInfo(), '/wizard')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Wizard not completed
|
||||
if (!setting('general.wizard', 0)) {
|
||||
if (!setting('wizard.completed', 0)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class SignedUrlCompany
|
||||
class SignedCompany
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
23
app/Http/Middleware/TrustProxies.php
Normal file
23
app/Http/Middleware/TrustProxies.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Fideloper\Proxy\TrustProxies as Middleware;
|
||||
|
||||
class TrustProxies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The trusted proxies for this application.
|
||||
*
|
||||
* @var array|string
|
||||
*/
|
||||
protected $proxies;
|
||||
|
||||
/**
|
||||
* The headers that should be used to detect proxies.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $headers = Request::HEADER_X_FORWARDED_ALL;
|
||||
}
|
78
app/Http/Middleware/ValidateSignature.php
Normal file
78
app/Http/Middleware/ValidateSignature.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Exceptions\InvalidSignatureException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ValidateSignature
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Illuminate\Routing\Exceptions\InvalidSignatureException
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if ($this->hasValidSignature($request)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
throw new InvalidSignatureException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given request has a valid signature.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param bool $absolute
|
||||
* @return bool
|
||||
*/
|
||||
public function hasValidSignature(Request $request, $absolute = true)
|
||||
{
|
||||
return $this->hasCorrectSignature($request, $absolute)
|
||||
&& $this->signatureHasNotExpired($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the signature from the given request matches the URL.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param bool $absolute
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCorrectSignature(Request $request, $absolute = true)
|
||||
{
|
||||
$url = $absolute ? $request->url() : '/'.$request->path();
|
||||
|
||||
$original = rtrim($url . '?' . Arr::query(
|
||||
Arr::only($request->query(), ['company_id'])
|
||||
), '?');
|
||||
|
||||
$signature = hash_hmac('sha256', $original, call_user_func(function () {
|
||||
return config('app.key');
|
||||
}));
|
||||
|
||||
return hash_equals($signature, (string) $request->query('signature', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the expires timestamp from the given request is not from the past.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function signatureHasNotExpired(Request $request)
|
||||
{
|
||||
$expires = $request->query('expires');
|
||||
|
||||
return ! ($expires && Carbon::now()->getTimestamp() > $expires);
|
||||
}
|
||||
}
|
@ -2,10 +2,17 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
|
||||
|
||||
class VerifyCsrfToken extends BaseVerifier
|
||||
class VerifyCsrfToken extends Middleware
|
||||
{
|
||||
/**
|
||||
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $addHttpCookie = true;
|
||||
|
||||
/**
|
||||
* The URIs that should be excluded from CSRF verification.
|
||||
*
|
||||
@ -14,4 +21,4 @@ class VerifyCsrfToken extends BaseVerifier
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user