first commit
This commit is contained in:
26
app/Http/Middleware/Authenticate.php
Normal file
26
app/Http/Middleware/Authenticate.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Exceptions\Entities\AuthorizationException;
|
||||
use Closure;
|
||||
use Illuminate\Auth\Middleware\Authenticate as BaseAuthenticate;
|
||||
use Lang;
|
||||
|
||||
class Authenticate extends BaseAuthenticate
|
||||
{
|
||||
public const DEFAULT_USER_LANGUAGE = 'en';
|
||||
|
||||
public function handle($request, Closure $next, ...$guards): mixed
|
||||
{
|
||||
$this->authenticate($request, $guards);
|
||||
|
||||
if (!$request->user()->active) {
|
||||
$request->user()->tokens()->whereId($request->user()->currentAccessToken()->id)->delete();
|
||||
throw new AuthorizationException(AuthorizationException::ERROR_TYPE_USER_DISABLED);
|
||||
}
|
||||
|
||||
Lang::setLocale($request->user()->user_language ?: self::DEFAULT_USER_LANGUAGE);
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
17
app/Http/Middleware/EncryptCookies.php
Normal file
17
app/Http/Middleware/EncryptCookies.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
|
||||
|
||||
class EncryptCookies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the cookies that should not be encrypted.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
99
app/Http/Middleware/RegisterModulesEvents.php
Normal file
99
app/Http/Middleware/RegisterModulesEvents.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App;
|
||||
use App\Events\ChangeEvent;
|
||||
use App\Filters\AttachmentFilter;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectGroup;
|
||||
use App\Models\Task;
|
||||
use App\Models\TaskHistory;
|
||||
use App\Models\TimeInterval;
|
||||
use App\Observers\AttachmentObserver;
|
||||
use CatEvent;
|
||||
use Filter;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RegisterModulesEvents
|
||||
{
|
||||
/**
|
||||
* @param Task|Project|TimeInterval|TaskHistory $model
|
||||
*/
|
||||
public static function broadcastEvent(string $entityType, string $action, $model): void
|
||||
{
|
||||
foreach (ChangeEvent::getRelatedUserIds($model) as $userId) {
|
||||
broadcast(new ChangeEvent($entityType, $action, $model, $userId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add subscribers from modules for Event and Filter
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
// TODO:
|
||||
// [ ] move to Observers folder
|
||||
// [ ] rewrite with laravel Event so updates that come from modules will trigger update
|
||||
CatEvent::listen('event.after.action.*', static function (string $eventName, array $data) {
|
||||
$eventNameParts = explode('.', $eventName);
|
||||
[$entityType, $action] = array_slice($eventNameParts, 3, 2); // Strip "event.after.action" and get the next two parts
|
||||
if (!in_array($entityType, ['tasks', 'projects', 'projects_members', 'intervals', 'task_comments', 'project_groups'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!in_array($action, ['create', 'edit', 'destroy'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entityType === 'projects_members') {
|
||||
$entityType = 'projects';
|
||||
$projectId = $data[0];
|
||||
$model = Project::query()->find($projectId);
|
||||
} elseif ($entityType === 'task_comments') {
|
||||
$entityType = 'tasks_activities';
|
||||
$model = $data[0];
|
||||
} elseif ($entityType === 'project_groups') {
|
||||
$entityType = 'projects';
|
||||
$action = 'edit';
|
||||
/** @var ProjectGroup $group */
|
||||
$group = $data[0];
|
||||
$model = $group->projects()->get();
|
||||
} else {
|
||||
$model = $data[0];
|
||||
}
|
||||
|
||||
App::terminating(static function () use ($entityType, $action, $model) {
|
||||
$items = is_array($model) || $model instanceof Collection ? $model : [$model];
|
||||
foreach ($items as $item) {
|
||||
static::broadcastEvent($entityType, $action, $item);
|
||||
|
||||
if (in_array($entityType, ['tasks', 'projects'])) {
|
||||
$project = match (true) {
|
||||
$item instanceof Task => $item->project,
|
||||
$item instanceof Project => $item,
|
||||
};
|
||||
|
||||
static::broadcastEvent('gantt', 'updateAll', $project);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// CatEvent and Filter are scoped to request, subscribe on every request for it to work with laravel octane
|
||||
Filter::subscribe(AttachmentFilter::class);
|
||||
CatEvent::subscribe(AttachmentObserver::class);
|
||||
|
||||
collect(Module::allEnabled())->each(static function (\Nwidart\Modules\Module $module) {
|
||||
App::call([preg_grep("/ModuleServiceProvider$/i", $module->get('providers'))[0], 'registerEvents']);
|
||||
});
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
52
app/Http/Middleware/SentryContext.php
Normal file
52
app/Http/Middleware/SentryContext.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Enums\Role;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Route;
|
||||
use Sentry\State\Scope;
|
||||
use Throwable;
|
||||
use function Sentry\configureScope;
|
||||
|
||||
class SentryContext
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): mixed
|
||||
{
|
||||
if (env('IMAGE_VERSION')) {
|
||||
configureScope(static function (Scope $scope): void {
|
||||
$scope->setTag('docker', env('IMAGE_VERSION'));
|
||||
});
|
||||
}
|
||||
|
||||
if ($user = $request->user()) {
|
||||
configureScope(static function (Scope $scope) use ($user): void {
|
||||
$scope->setUser([
|
||||
'id' => $user->id,
|
||||
'email' => config('sentry.send_default_pii') ? $user->email : sha1($user->email),
|
||||
'role' => is_int($user->role_id) ? Role::tryFrom($user->role_id)?->name :$user->role_id->name,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
configureScope(static function (Scope $scope) use ($request): void {
|
||||
$scope->setTag('request.host', $request->host());
|
||||
$scope->setTag('request.method', $request->method());
|
||||
try {
|
||||
$scope->setTag('request.route', Route::getRoutes()->match($request)->getName());
|
||||
} catch (Throwable) {
|
||||
}
|
||||
});
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
18
app/Http/Middleware/TrimStrings.php
Normal file
18
app/Http/Middleware/TrimStrings.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\TrimStrings as BaseTrimmer;
|
||||
|
||||
class TrimStrings extends BaseTrimmer
|
||||
{
|
||||
/**
|
||||
* The names of the attributes that should not be trimmed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $except = [
|
||||
'password',
|
||||
'password_confirmation',
|
||||
];
|
||||
}
|
||||
17
app/Http/Middleware/VerifyCsrfToken.php
Normal file
17
app/Http/Middleware/VerifyCsrfToken.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
|
||||
|
||||
class VerifyCsrfToken extends BaseVerifier
|
||||
{
|
||||
/**
|
||||
* The URIs that should be excluded from CSRF verification.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $except = [
|
||||
'/api/*'
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user