Skip to content

Commit

Permalink
use RuleSets instead of directly rule property (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Feb 5, 2023
1 parent 8e4f053 commit 7c94325
Show file tree
Hide file tree
Showing 78 changed files with 1,067 additions and 284 deletions.
28 changes: 28 additions & 0 deletions app/Contracts/Http/Requests/RequestAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class RequestAttribute
public const USER_ID_ATTRIBUTE = 'userID';
public const USER_IDS_ATTRIBUTE = 'userIDs';

public const EMAIL_ATTRIBUTE = 'email';

public const PARENT_ID_ATTRIBUTE = 'parent_id';

public const ALBUM_ID_ATTRIBUTE = 'albumID';
Expand All @@ -32,6 +34,17 @@ class RequestAttribute
public const SORTING_COLUMN_ATTRIBUTE = 'sorting_column';
public const SORTING_ORDER_ATTRIBUTE = 'sorting_order';

public const IS_NSFW_ATTRIBUTE = 'is_nsfw';
public const IS_PUBLIC_ATTRIBUTE = 'is_public';
public const IS_LINK_REQUIRED_ATTRIBUTE = 'is_link_required';
public const GRANTS_DOWNLOAD_ATTRIBUTE = 'grants_download';
public const GRANTS_FULL_PHOTO_ACCESS_ATTRIBUTE = 'grants_full_photo_access';

public const FILE_ATTRIBUTE = 'file';
public const SHALL_OVERRIDE_ATTRIBUTE = 'shall_override';
public const IS_STARRED_ATTRIBUTE = 'is_starred';
public const DIRECTION_ATTRIBUTE = 'direction';

/**
* Due to historic reasons the attribute which stores the type of
* size variant is called `kind`.
Expand All @@ -44,8 +57,23 @@ class RequestAttribute
* TODO: Maybe rename the attribute in the back- and front-end to avoid overloading the same term.
*/
public const SIZE_VARIANT_ATTRIBUTE = 'kind';

public const TAGS_ATTRIBUTE = 'tags';
/**
* For historical reasons the parameter of the API is called `show_tags`
* and not only `tags`; otherwise `RequestAttribute::TAGS_ATTRIBUTE` could be used.
*/
public const SHOW_TAGS_ATTRIBUTE = 'show_tags';

public const MAY_UPLOAD_ATTRIBUTE = 'may_upload';
public const MAY_EDIT_OWN_SETTINGS_ATTRIBUTE = 'may_edit_own_settings';

/**
* Import from server attributes.
*/
public const PATH_ATTRIBUTE = 'paths';
public const DELETE_IMPORTED_ATTRIBUTE = 'delete_imported';
public const SKIP_DUPLICATES_ATTRIBUTE = 'skip_duplicates';
public const IMPORT_VIA_SYMLINK_ATTRIBUTE = 'import_via_symlink';
public const RESYNC_METADATA_ATTRIBUTE = 'resync_metadata';
}
20 changes: 20 additions & 0 deletions app/Contracts/Http/RuleSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Contracts\Http;

/**
* In order to avoid code duplication, we centralize the rule sets
* used during the validation of requests as they are used both
* in Livewire and in the Requests class.
*/
interface RuleSet
{
/**
* Return an array containing the rules to be applied to the request attributes.
*
* @return array
*/
public static function rules(): array;

// TODO: Associate error message to above rules.
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use App\Http\Requests\Photo\SetPhotosStarredRequest;
use App\Http\Requests\Photo\SetPhotosTagsRequest;
use App\Http\Requests\Photo\SetPhotosTitleRequest;
use App\Http\Requests\Photo\SetPhotoUploadDate;
use App\Http\Requests\Photo\SetPhotoUploadDateRequest;
use App\Image\Files\TemporaryLocalFile;
use App\Image\Files\UploadedFile;
use App\ModelFunctions\SymLinkFunctions;
Expand Down Expand Up @@ -276,13 +276,13 @@ public function setLicense(SetPhotoLicenseRequest $request): void
/**
* Sets the license of the photo.
*
* @param SetPhotoUploadDate $request
* @param SetPhotoUploadDateRequest $request
*
* @return void
*
* @throws LycheeException
*/
public function setUploadDate(SetPhotoUploadDate $request): void
public function setUploadDate(SetPhotoUploadDateRequest $request): void
{
$request->photo()->created_at = $request->requestDate();
$request->photo()->save();
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/AddAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasParentAlbumTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Http\RuleSets\Album\AddAlbumRuleSet;
use App\Models\Album;
use App\Policies\AlbumPolicy;
use App\Rules\RandomIDRule;
use App\Rules\TitleRule;
use Illuminate\Support\Facades\Gate;

class AddAlbumRequest extends BaseApiRequest implements HasTitle, HasParentAlbum
Expand All @@ -33,10 +32,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::PARENT_ID_ATTRIBUTE => ['present', new RandomIDRule(true)],
RequestAttribute::TITLE_ATTRIBUTE => ['required', new TitleRule()],
];
return AddAlbumRuleSet::rules();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/AddTagAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasTagsTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Http\RuleSets\Album\AddTagAlbumRuleSet;
use App\Policies\AlbumPolicy;
use App\Rules\TitleRule;
use Illuminate\Support\Facades\Gate;

class AddTagAlbumRequest extends BaseApiRequest implements HasTitle, HasTags
Expand All @@ -34,11 +34,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::TITLE_ATTRIBUTE => ['required', new TitleRule()],
RequestAttribute::TAGS_ATTRIBUTE => 'required|array|min:1',
RequestAttribute::TAGS_ATTRIBUTE . '.*' => 'required|string|min:1',
];
return AddTagAlbumRuleSet::rules();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Requests/Album/ArchiveAlbumsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use App\Contracts\Models\AbstractAlbum;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAlbumsTrait;
use App\Http\RuleSets\Album\ArchiveAlbumRuleSet;
use App\Policies\AlbumPolicy;
use App\Rules\AlbumIDListRule;
use Illuminate\Support\Facades\Gate;

/**
Expand Down Expand Up @@ -38,9 +38,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_IDS_ATTRIBUTE => ['required', new AlbumIDListRule()],
];
return ArchiveAlbumRuleSet::rules();
}

/**
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Requests/Album/DeleteAlbumsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use App\Contracts\Http\Requests\RequestAttribute;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAlbumIDsTrait;
use App\Http\RuleSets\Album\DeleteAlbumsRuleSet;
use App\Models\Album;
use App\Policies\AlbumPolicy;
use App\Rules\AlbumIDRule;
use Illuminate\Support\Facades\Gate;

class DeleteAlbumsRequest extends BaseApiRequest implements HasAlbumIDs
Expand All @@ -28,10 +28,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_IDS_ATTRIBUTE => 'required|array|min:1',
RequestAttribute::ALBUM_IDS_ATTRIBUTE . '.*' => ['required', new AlbumIDRule(false)],
];
return DeleteAlbumsRuleSet::rules();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Requests/Album/DeleteTrackRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
use App\Http\RuleSets\Album\BasicAlbumIdRuleSet;
use App\Models\Album;
use App\Rules\AlbumIDRule;

class DeleteTrackRequest extends BaseApiRequest implements HasAlbum
{
Expand All @@ -20,9 +20,7 @@ class DeleteTrackRequest extends BaseApiRequest implements HasAlbum
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new AlbumIDRule(false)],
];
return BasicAlbumIdRuleSet::rules();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Requests/Album/GetAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use App\Exceptions\PasswordRequiredException;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAbstractAlbumTrait;
use App\Http\RuleSets\Album\BasicAlbumIdRuleSet;
use App\Models\Extensions\BaseAlbum;
use App\Policies\AlbumPolicy;
use App\Rules\AlbumIDRule;
use Illuminate\Support\Facades\Gate;

class GetAlbumRequest extends BaseApiRequest implements HasAbstractAlbum
Expand Down Expand Up @@ -45,9 +45,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new AlbumIDRule(false)],
];
return BasicAlbumIdRuleSet::rules();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/MergeAlbumsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumAlbumsTrait;
use App\Http\Requests\Traits\HasAlbumsTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
use App\Http\RuleSets\Album\MergeAlbumsRuleSet;
use App\Models\Album;
use App\Rules\RandomIDRule;

/**
* @implements HasAlbums<Album>
Expand All @@ -27,11 +27,7 @@ class MergeAlbumsRequest extends BaseApiRequest implements HasAlbum, HasAlbums
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::ALBUM_IDS_ATTRIBUTE => 'required|array|min:1',
RequestAttribute::ALBUM_IDS_ATTRIBUTE . '.*' => ['required', new RandomIDRule(false)],
];
return MergeAlbumsRuleSet::rules();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/MoveAlbumsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumAlbumsTrait;
use App\Http\Requests\Traits\HasAlbumsTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
use App\Http\RuleSets\Album\MoveAlbumsRuleSet;
use App\Models\Album;
use App\Rules\RandomIDRule;

/**
* @implements HasAlbums<Album>
Expand All @@ -27,11 +27,7 @@ class MoveAlbumsRequest extends BaseApiRequest implements HasAlbum, HasAlbums
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['present', new RandomIDRule(true)],
RequestAttribute::ALBUM_IDS_ATTRIBUTE => 'required|array|min:1',
RequestAttribute::ALBUM_IDS_ATTRIBUTE . '.*' => ['required', new RandomIDRule(false)],
];
return MoveAlbumsRuleSet::rules();
}

/**
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Requests/Album/SetAlbumCoverRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAlbumTrait;
use App\Http\Requests\Traits\HasPhotoTrait;
use App\Http\RuleSets\Album\SetAlbumCoverRuleSet;
use App\Models\Album;
use App\Models\Photo;
use App\Policies\AlbumPolicy;
use App\Policies\PhotoPolicy;
use App\Rules\RandomIDRule;
use Illuminate\Support\Facades\Gate;

class SetAlbumCoverRequest extends BaseApiRequest implements HasAlbum, HasPhoto
Expand All @@ -35,10 +35,7 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::PHOTO_ID_ATTRIBUTE => ['present', new RandomIDRule(true)],
];
return SetAlbumCoverRuleSet::rules();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/SetAlbumDescriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasBaseAlbumTrait;
use App\Http\Requests\Traits\HasDescriptionTrait;
use App\Rules\DescriptionRule;
use App\Rules\RandomIDRule;
use App\Http\RuleSets\Album\SetAlbumDescriptionRuleSet;

class SetAlbumDescriptionRequest extends BaseApiRequest implements HasBaseAlbum, HasDescription
{
Expand All @@ -23,10 +22,7 @@ class SetAlbumDescriptionRequest extends BaseApiRequest implements HasBaseAlbum,
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::DESCRIPTION_ATTRIBUTE => ['present', new DescriptionRule()],
];
return SetAlbumDescriptionRuleSet::rules();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Requests/Album/SetAlbumLicenseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
use App\Http\Requests\Traits\HasLicenseTrait;
use App\Http\RuleSets\Album\SetAlbumLicenseRuleSet;
use App\Models\Album;
use App\Rules\LicenseRule;
use App\Rules\RandomIDRule;

class SetAlbumLicenseRequest extends BaseApiRequest implements HasAlbum, HasLicense
{
Expand All @@ -24,10 +23,7 @@ class SetAlbumLicenseRequest extends BaseApiRequest implements HasAlbum, HasLice
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new LicenseRule()],
];
return SetAlbumLicenseRuleSet::rules();
}

/**
Expand Down
11 changes: 3 additions & 8 deletions app/Http/Requests/Album/SetAlbumNSFWRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasBaseAlbumTrait;
use App\Rules\RandomIDRule;
use App\Http\RuleSets\Album\SetAlbumNSFWRuleSet;

/**
* Class SetAlbumNSFWRequest.
Expand All @@ -17,19 +17,14 @@ class SetAlbumNSFWRequest extends BaseApiRequest implements HasBaseAlbum
use HasBaseAlbumTrait;
use AuthorizeCanEditAlbumTrait;

public const IS_NSFW_ATTRIBUTE = 'is_nsfw';

protected bool $isNSFW = false;

/**
* {@inheritDoc}
*/
public function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
self::IS_NSFW_ATTRIBUTE => 'required|boolean',
];
return SetAlbumNSFWRuleSet::rules();
}

/**
Expand All @@ -40,7 +35,7 @@ protected function processValidatedValues(array $values, array $files): void
$this->album = $this->albumFactory->findBaseAlbumOrFail(
$values[RequestAttribute::ALBUM_ID_ATTRIBUTE]
);
$this->isNSFW = static::toBoolean($values[self::IS_NSFW_ATTRIBUTE]);
$this->isNSFW = static::toBoolean($values[RequestAttribute::IS_NSFW_ATTRIBUTE]);
}

public function isNSFW(): bool
Expand Down
Loading

0 comments on commit 7c94325

Please sign in to comment.