Improve self-service access management and polish public student lookup
This commit is contained in:
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\DocumentType;
|
||||
use App\Models\Student;
|
||||
use App\Models\StudentDocument;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PublicStudentLookupController extends Controller
|
||||
@@ -75,23 +77,63 @@ class PublicStudentLookupController extends Controller
|
||||
protected function resolvePhotoUrl(Student $student): ?string
|
||||
{
|
||||
$photoDocument = $student->documents
|
||||
->filter(function ($document) {
|
||||
$code = strtoupper((string) ($document->documentType?->code ?? ''));
|
||||
$name = strtolower((string) ($document->documentType?->name ?? ''));
|
||||
|
||||
return $code === 'PAS_FOTO'
|
||||
|| $code === 'PASFOTO'
|
||||
|| str_contains($name, 'pas foto')
|
||||
|| str_contains($name, 'pasfoto');
|
||||
->filter(fn ($document) => $this->isPhotoDocument($document))
|
||||
->sortByDesc(function ($document) {
|
||||
return sprintf(
|
||||
'%d-%s',
|
||||
$document->verification_status === 'valid' ? 1 : 0,
|
||||
optional($document->created_at)?->format('YmdHis') ?? '00000000000000'
|
||||
);
|
||||
})
|
||||
->sortByDesc('created_at')
|
||||
->first();
|
||||
|
||||
if (! $photoDocument && $student->relationLoaded('documents') === false) {
|
||||
$photoDocument = StudentDocument::query()
|
||||
->with('documentType')
|
||||
->where('student_id', $student->id)
|
||||
->get()
|
||||
->filter(fn ($document) => $this->isPhotoDocument($document))
|
||||
->sortByDesc(function ($document) {
|
||||
return sprintf(
|
||||
'%d-%s',
|
||||
$document->verification_status === 'valid' ? 1 : 0,
|
||||
optional($document->created_at)?->format('YmdHis') ?? '00000000000000'
|
||||
);
|
||||
})
|
||||
->first();
|
||||
}
|
||||
|
||||
if (! $photoDocument?->file_path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return asset('storage/' . $photoDocument->file_path);
|
||||
if (! Storage::disk('public')->exists($photoDocument->file_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return asset('storage/' . ltrim($photoDocument->file_path, '/'));
|
||||
}
|
||||
|
||||
protected function isPhotoDocument(StudentDocument $document): bool
|
||||
{
|
||||
$code = strtoupper((string) ($document->documentType?->code ?? ''));
|
||||
$name = strtolower((string) ($document->documentType?->name ?? ''));
|
||||
$fileName = strtolower((string) ($document->file_name ?? ''));
|
||||
$mimeType = strtolower((string) ($document->mime_type ?? ''));
|
||||
|
||||
$looksLikePhoto = $code === 'PAS_FOTO'
|
||||
|| $code === 'PASFOTO'
|
||||
|| str_contains($name, 'pas foto')
|
||||
|| str_contains($name, 'pasfoto')
|
||||
|| str_contains($fileName, 'pas foto')
|
||||
|| str_contains($fileName, 'pasfoto');
|
||||
|
||||
if ($looksLikePhoto) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return str_starts_with($mimeType, 'image/')
|
||||
&& (str_contains($name, 'foto') || str_contains($fileName, 'foto'));
|
||||
}
|
||||
|
||||
protected function buildRequiredDocumentSummary(Student $student): array
|
||||
|
||||
Reference in New Issue
Block a user