mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 12:12:15 +01:00
Send permission to client via inertia data
This commit is contained in:
committed by
Constantin Graf
parent
a3196c4964
commit
af447ce2c0
@@ -6,6 +6,7 @@ namespace App\Http\Middleware;
|
|||||||
|
|
||||||
use App\Models\Organization;
|
use App\Models\Organization;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Service\PermissionStore;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
@@ -23,7 +24,9 @@ class ShareInertiaData
|
|||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next): Response
|
public function handle(Request $request, Closure $next): Response
|
||||||
{
|
{
|
||||||
|
$permissions = app(PermissionStore::class);
|
||||||
Inertia::share(array_filter([
|
Inertia::share(array_filter([
|
||||||
|
'permissions' => $request->user() !== null ? $permissions->permissions($request->user()->currentTeam) : [],
|
||||||
'jetstream' => function () use ($request) {
|
'jetstream' => function () use ($request) {
|
||||||
/** @var User|null $user */
|
/** @var User|null $user */
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ namespace App\Service;
|
|||||||
use App\Models\Organization;
|
use App\Models\Organization;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Laravel\Jetstream\Jetstream;
|
||||||
|
use Laravel\Jetstream\Role;
|
||||||
|
|
||||||
class PermissionStore
|
class PermissionStore
|
||||||
{
|
{
|
||||||
@@ -40,4 +42,31 @@ class PermissionStore
|
|||||||
|
|
||||||
return in_array($permission, $permissions, true);
|
return in_array($permission, $permissions, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string>
|
||||||
|
*/
|
||||||
|
public function getPermissions(Organization $organization): array
|
||||||
|
{
|
||||||
|
/** @var User|null $user */
|
||||||
|
$user = Auth::user();
|
||||||
|
if ($user === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $user->belongsToTeam($organization)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$role = $organization->users
|
||||||
|
->where('id', $user->getKey())
|
||||||
|
->first()
|
||||||
|
?->membership
|
||||||
|
?->role;
|
||||||
|
|
||||||
|
/** @var Role|null $roleObj */
|
||||||
|
$roleObj = Jetstream::findRole($role);
|
||||||
|
|
||||||
|
return $role !== null ? ($roleObj?->permissions ?? []) : [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user