90 lines
2.4 KiB
PHP
90 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class HorizonReportAccess
|
|
{
|
|
public static function canView(): bool
|
|
{
|
|
$user = Auth::user();
|
|
if (!$user || ($user->domain ?? '') !== 'horizon') {
|
|
return false;
|
|
}
|
|
|
|
$designation = strtoupper(trim(self::tokenDesignation($user->access_token ?? '')));
|
|
if ($designation === 'RBM') {
|
|
return false;
|
|
}
|
|
|
|
if (self::canViewPob()) {
|
|
return true;
|
|
}
|
|
|
|
// Match web: show Custom Reports for designations above RBM/ABM/TM.
|
|
return $designation !== '' && $designation !== 'ABM' && $designation !== 'TM';
|
|
}
|
|
|
|
public static function canViewPob(): bool
|
|
{
|
|
$user = Auth::user();
|
|
if (!$user || ($user->domain ?? '') !== 'horizon') {
|
|
return false;
|
|
}
|
|
|
|
$permissions = self::permissions($user->permissions ?? []);
|
|
if (in_array('all_data_access', $permissions, true)) {
|
|
return true;
|
|
}
|
|
|
|
$designation = strtoupper(trim(self::tokenDesignation($user->access_token ?? '')));
|
|
|
|
return $designation === 'HO';
|
|
}
|
|
|
|
private static function permissions($permissions): array
|
|
{
|
|
if (is_string($permissions)) {
|
|
$decoded = json_decode($permissions, true);
|
|
$permissions = is_array($decoded) ? $decoded : [];
|
|
}
|
|
if (!is_array($permissions)) {
|
|
return [];
|
|
}
|
|
|
|
$names = [];
|
|
foreach ($permissions as $permission) {
|
|
if (is_scalar($permission) && (string) $permission !== '') {
|
|
$names[] = (string) $permission;
|
|
}
|
|
}
|
|
|
|
return $names;
|
|
}
|
|
|
|
private static function tokenDesignation(string $token): string
|
|
{
|
|
$token = trim($token);
|
|
if (stripos($token, 'Bearer ') === 0) {
|
|
$token = trim(substr($token, 7));
|
|
}
|
|
$parts = explode('.', $token);
|
|
if (count($parts) < 2) {
|
|
return '';
|
|
}
|
|
$segment = strtr($parts[1], '-_', '+/');
|
|
$pad = strlen($segment) % 4;
|
|
if ($pad > 0) {
|
|
$segment .= str_repeat('=', 4 - $pad);
|
|
}
|
|
$payload = json_decode(base64_decode($segment));
|
|
$user = is_object($payload) ? ($payload->user ?? null) : null;
|
|
if (!is_object($user)) {
|
|
return '';
|
|
}
|
|
|
|
return (string) ($user->designation ?? '');
|
|
}
|
|
}
|