fixed common api endpoints

This commit is contained in:
Denis Duliçi
2020-12-26 16:13:34 +03:00
parent 8dbe178a70
commit 2e09989cf5
13 changed files with 267 additions and 113 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Traits;
use App\Events\Auth\ApiPermissionsAssigning;
use App\Models\Auth\Permission;
use App\Models\Auth\Role;
use App\Utilities\Reports;
@@ -401,34 +402,46 @@ trait Permissions
return;
}
$route = app(Route::class);
$table = request()->is('api/*') ? request()->segment(2) : '';
// Get the controller array
$arr = array_reverse(explode('\\', explode('@', $route->getAction()['uses'])[0]));
// Fire event to find the proper controller for common API endpoints
if (in_array($table, ['contacts', 'documents', 'transactions'])) {
$p = new \stdClass();
$p->controller = '';
$controller = '';
event(new ApiPermissionsAssigning($p, $table, request()->get('type')));
// Add module
if (isset($arr[3]) && isset($arr[4])) {
if (strtolower($arr[4]) == 'modules') {
$controller .= Str::kebab($arr[3]) . '-';
} elseif (isset($arr[5]) && (strtolower($arr[5]) == 'modules')) {
$controller .= Str::kebab($arr[4]) . '-';
$controller = $p->controller;
} else {
$route = app(Route::class);
// Get the controller array
$arr = array_reverse(explode('\\', explode('@', $route->getAction()['uses'])[0]));
$controller = '';
// Add module
if (isset($arr[3]) && isset($arr[4])) {
if (strtolower($arr[4]) == 'modules') {
$controller .= Str::kebab($arr[3]) . '-';
} elseif (isset($arr[5]) && (strtolower($arr[5]) == 'modules')) {
$controller .= Str::kebab($arr[4]) . '-';
}
}
}
// Add folder
if (strtolower($arr[1]) != 'controllers') {
$controller .= Str::kebab($arr[1]) . '-';
}
// Add folder
if (strtolower($arr[1]) != 'controllers') {
$controller .= Str::kebab($arr[1]) . '-';
}
// Add file
$controller .= Str::kebab($arr[0]);
// Add file
$controller .= Str::kebab($arr[0]);
// Skip ACL
$skip = ['portal-dashboard'];
if (in_array($controller, $skip)) {
return;
// Skip ACL
$skip = ['portal-dashboard'];
if (in_array($controller, $skip)) {
return;
}
}
// App\Http\Controllers\FooBar -->> foo-bar

62
app/Traits/Scopes.php Normal file
View File

@@ -0,0 +1,62 @@
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait Scopes
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function applyTypeScope(Builder $builder, Model $model)
{
// Skip if already exists
if ($this->scopeExists($builder, 'type')) {
return;
}
$type = request()->get('type') ?: Str::singular(request()->segment(2, ''));
// Apply type scope
$builder->where($model->getTable() . '.type', '=', $type);
}
/**
* Check if scope exists.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param $column
* @return boolean
*/
public function scopeExists($builder, $column)
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['column'] = $whr[1];
}
if ($where['column'] != $column) {
continue;
}
return true;
}
return false;
}
}