$adminId, 'ch' => $channels, 'iat' => $now, 'exp' => $now + $ttl, 'sxp' => $sessionExpiresAt, 'nonce' => bin2hex(random_bytes(8)), ])); return [ 'ticket' => $body . '.' . self::base64Url(hash_hmac('sha256', $body, $secret, true)), 'expires_in' => $ttl, 'url' => (string) config('admin_realtime.public_url'), 'channels' => $channels, ]; } /** @return list */ public static function allowedChannels(int $adminId): array { $channels = array_keys(AdminRealtimeRows::CHANNELS); if ($adminId === 1) { return $channels; } $roleIds = collect(RoleUserService::findAll(['user_id' => $adminId]))->pluck('role_id')->toArray() ?: [-1]; $granted = []; foreach (RoleService::model()::with(['menus' => function ($query) { $query->where('type', MenuService::model()::TYPE_BUTTON); }])->whereIn('id', $roleIds)->get() as $role) { $granted = array_merge($granted, $role['menus_uris'] ?: []); } return array_values(array_filter($channels, static function (string $channel) use ($granted): bool { $uri = AdminRealtimeRows::CHANNELS[$channel][1]; $menu = MenuService::findOne(['uri' => $uri, 'type' => MenuService::model()::TYPE_BUTTON, 'status' => MenuService::model()::STATUS_SHOW]); return empty($menu) || in_array($uri, $granted, true); })); } private static function base64Url(string $value): string { return rtrim(strtr(base64_encode($value), '+/', '-_'), '='); } }