ultimatepos/app/Services/Api/BusinessContextService.php

86 lines
3.1 KiB
PHP

<?php
namespace App\Services\Api;
use App\Services\Holding\ContextAssignmentService;
use App\Utils\Util;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BusinessContextService
{
public function __construct(
protected Util $util,
protected ContextAssignmentService $contextAssignmentService
)
{
}
public function getMePayload(): array
{
$user = Auth::user();
$request = request();
$business = $request->attributes->get('api_business');
$currency = $request->attributes->get('api_currency');
$financial_year = $request->attributes->get('api_financial_year');
$is_admin = $this->util->is_admin($user);
$permissions = $is_admin
? ['*']
: $user->getAllPermissions()->pluck('name')->values()->all();
$assignment = config('constants.enable_holding_context_rbac')
? $this->contextAssignmentService->resolveFromRequest($request, $user)
: null;
return [
'user' => [
'id' => $user->id,
'user_type' => $user->user_type,
'surname' => $user->surname,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'username' => $user->username,
'email' => $user->email,
'language' => $user->language,
'business_id' => $user->business_id,
'is_admin' => $is_admin,
],
'business' => [
'id' => $business->id,
'name' => $business->name,
'currency_id' => $business->currency_id,
'time_zone' => $business->time_zone,
'fy_start_month' => $business->fy_start_month,
'accounting_method' => $business->accounting_method,
'default_sales_tax' => $business->default_sales_tax,
'default_profit_percent' => $business->default_profit_percent,
'logo' => $business->logo,
'sku_prefix' => $business->sku_prefix,
],
'currency' => $currency,
'financial_year' => $financial_year,
'permissions' => $permissions,
'assignment_context' => $assignment ? [
'id' => $assignment->id,
'entity_id' => $assignment->entity_id,
'entity_name' => optional($assignment->entity)->name,
'org_unit_id' => $assignment->org_unit_id,
'org_unit_name' => optional($assignment->orgUnit)->name,
'role_id' => $assignment->role_id,
'role_name' => optional($assignment->role)->name,
'scopes' => $assignment->scopes->map(function ($scope) {
return [
'key' => $scope->scope_key,
'value' => $scope->scope_value,
];
})->values()->all(),
] : null,
];
}
public function getBusinessId(Request $request): int
{
return (int) $request->attributes->get('api_user')['business_id'];
}
}