Skip to content

Commit

Permalink
Refactor HTTP exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Oct 3, 2021
1 parent 7a8937b commit 81b729d
Show file tree
Hide file tree
Showing 106 changed files with 350 additions and 769 deletions.
135 changes: 72 additions & 63 deletions app/Auth.php
Expand Up @@ -20,21 +20,8 @@
namespace Fisharebest\Webtrees;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Exceptions\FamilyAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
use Fisharebest\Webtrees\Exceptions\MediaAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
use Fisharebest\Webtrees\Exceptions\NoteAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
use Fisharebest\Webtrees\Exceptions\RecordAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\RecordNotFoundException;
use Fisharebest\Webtrees\Exceptions\RepositoryAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
use Fisharebest\Webtrees\Exceptions\SourceAccessDeniedException;
use Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Services\UserService;

Expand Down Expand Up @@ -191,7 +178,7 @@ public static function user(): UserInterface
*/
public static function login(UserInterface $user): void
{
Session::regenerate(false);
Session::regenerate();
Session::put('wt_user', $user->id());
}

Expand Down Expand Up @@ -225,13 +212,15 @@ public static function checkComponentAccess(ModuleInterface $module, string $int
* @param bool $edit
*
* @return Family
* @throws FamilyNotFoundException
* @throws FamilyAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkFamilyAccess(?Family $family, bool $edit = false): Family
{
$message = I18N::translate('This family does not exist or you do not have permission to view it.');

if ($family === null) {
throw new FamilyNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $family->canEdit()) {
Expand All @@ -244,21 +233,23 @@ public static function checkFamilyAccess(?Family $family, bool $edit = false): F
return $family;
}

throw new FamilyAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Header|null $header
* @param bool $edit
*
* @return Header
* @throws RecordNotFoundException
* @throws RecordAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkHeaderAccess(?Header $header, bool $edit = false): Header
{
$message = I18N::translate('This record does not exist or you do not have permission to view it.');

if ($header === null) {
throw new RecordNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $header->canEdit()) {
Expand All @@ -271,22 +262,24 @@ public static function checkHeaderAccess(?Header $header, bool $edit = false): H
return $header;
}

throw new RecordAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Individual|null $individual
* @param bool $edit
* @param bool $chart For some charts, we can show private records
* @param bool $chart For some charts, we can show private records
*
* @return Individual
* @throws IndividualNotFoundException
* @throws IndividualAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkIndividualAccess(?Individual $individual, bool $edit = false, $chart = false): Individual
public static function checkIndividualAccess(?Individual $individual, bool $edit = false, bool $chart = false): Individual
{
$message = I18N::translate('This individual does not exist or you do not have permission to view it.');

if ($individual === null) {
throw new IndividualNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $individual->canEdit()) {
Expand All @@ -303,21 +296,23 @@ public static function checkIndividualAccess(?Individual $individual, bool $edit
return $individual;
}

throw new IndividualAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Location|null $location
* @param bool $edit
* @param bool $edit
*
* @return Location
* @throws RecordNotFoundException
* @throws RecordAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkLocationAccess(?Location $location, bool $edit = false): Location
{
$message = I18N::translate('This record does not exist or you do not have permission to view it.');

if ($location === null) {
throw new RecordNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $location->canEdit()) {
Expand All @@ -330,21 +325,23 @@ public static function checkLocationAccess(?Location $location, bool $edit = fal
return $location;
}

throw new RecordAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Media|null $media
* @param bool $edit
*
* @return Media
* @throws MediaNotFoundException
* @throws MediaAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkMediaAccess(?Media $media, bool $edit = false): Media
{
$message = I18N::translate('This media object does not exist or you do not have permission to view it.');

if ($media === null) {
throw new MediaNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $media->canEdit()) {
Expand All @@ -357,21 +354,23 @@ public static function checkMediaAccess(?Media $media, bool $edit = false): Medi
return $media;
}

throw new MediaAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Note|null $note
* @param bool $edit
*
* @return Note
* @throws NoteNotFoundException
* @throws NoteAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkNoteAccess(?Note $note, bool $edit = false): Note
{
$message = I18N::translate('This note does not exist or you do not have permission to view it.');

if ($note === null) {
throw new NoteNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $note->canEdit()) {
Expand All @@ -384,21 +383,23 @@ public static function checkNoteAccess(?Note $note, bool $edit = false): Note
return $note;
}

throw new NoteAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param GedcomRecord|null $record
* @param bool $edit
*
* @return GedcomRecord
* @throws RecordNotFoundException
* @throws RecordAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkRecordAccess(?GedcomRecord $record, bool $edit = false): GedcomRecord
{
$message = I18N::translate('This record does not exist or you do not have permission to view it.');

if ($record === null) {
throw new RecordNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $record->canEdit()) {
Expand All @@ -411,21 +412,23 @@ public static function checkRecordAccess(?GedcomRecord $record, bool $edit = fal
return $record;
}

throw new RecordAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Repository|null $repository
* @param bool $edit
*
* @return Repository
* @throws RepositoryNotFoundException
* @throws RepositoryAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkRepositoryAccess(?Repository $repository, bool $edit = false): Repository
{
$message = I18N::translate('This repository does not exist or you do not have permission to view it.');

if ($repository === null) {
throw new RepositoryNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $repository->canEdit()) {
Expand All @@ -438,21 +441,23 @@ public static function checkRepositoryAccess(?Repository $repository, bool $edit
return $repository;
}

throw new RepositoryAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
* @param Source|null $source
* @param bool $edit
*
* @return Source
* @throws SourceNotFoundException
* @throws SourceAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkSourceAccess(?Source $source, bool $edit = false): Source
{
$message = I18N::translate('This source does not exist or you do not have permission to view it.');

if ($source === null) {
throw new SourceNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $source->canEdit()) {
Expand All @@ -465,21 +470,23 @@ public static function checkSourceAccess(?Source $source, bool $edit = false): S
return $source;
}

throw new SourceAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/*
* @param Submitter|null $submitter
* @param bool $edit
*
* @return Submitter
* @throws RecordNotFoundException
* @throws RecordAccessDeniedException
* @throws HttpFoundException
* @throws HttpDeniedException
*/
public static function checkSubmitterAccess(?Submitter $submitter, bool $edit = false): Submitter
{
$message = I18N::translate('This record does not exist or you do not have permission to view it.');

if ($submitter === null) {
throw new RecordNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $submitter->canEdit()) {
Expand All @@ -492,21 +499,23 @@ public static function checkSubmitterAccess(?Submitter $submitter, bool $edit =
return $submitter;
}

throw new RecordAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/*
* @param Submission|null $submission
* @param bool $edit
*
* @return Submission
* @throws RecordNotFoundException
* @throws RecordAccessDeniedException
* @throws HttpNotFoundException
* @throws HttpAccessDeniedException
*/
public static function checkSubmissionAccess(?Submission $submission, bool $edit = false): Submission
{
$message = I18N::translate('This record does not exist or you do not have permission to view it.');

if ($submission === null) {
throw new RecordNotFoundException();
throw new HttpNotFoundException($message);
}

if ($edit && $submission->canEdit()) {
Expand All @@ -519,7 +528,7 @@ public static function checkSubmissionAccess(?Submission $submission, bool $edit
return $submission;
}

throw new RecordAccessDeniedException();
throw new HttpAccessDeniedException($message);
}

/**
Expand Down
38 changes: 0 additions & 38 deletions app/Exceptions/FamilyAccessDeniedException.php

This file was deleted.

0 comments on commit 81b729d

Please sign in to comment.