Changeset View
Changeset View
Standalone View
Standalone View
src/app/Rules/SharedFolderName.php
- This file was copied from src/app/Rules/ResourceName.php.
<?php | <?php | ||||
namespace App\Rules; | namespace App\Rules; | ||||
use Illuminate\Contracts\Validation\Rule; | use Illuminate\Contracts\Validation\Rule; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
class ResourceName implements Rule | class SharedFolderName implements Rule | ||||
{ | { | ||||
private $message; | private $message; | ||||
private $owner; | private $owner; | ||||
private $domain; | private $domain; | ||||
private const FORBIDDEN_CHARS = '+/^%*!`@(){}|\\?<;"'; | |||||
/** | /** | ||||
* Class constructor. | * Class constructor. | ||||
* | * | ||||
* @param \App\User $owner The account owner | * @param \App\User $owner The account owner | ||||
* @param string $domain The domain name of the group | * @param string $domain The domain name of the group | ||||
*/ | */ | ||||
public function __construct($owner, $domain) | public function __construct($owner, $domain) | ||||
{ | { | ||||
$this->owner = $owner; | $this->owner = $owner; | ||||
$this->domain = Str::lower($domain); | $this->domain = Str::lower($domain); | ||||
} | } | ||||
/** | /** | ||||
* Determine if the validation rule passes. | * Determine if the validation rule passes. | ||||
* | * | ||||
* @param string $attribute Attribute name | * @param string $attribute Attribute name | ||||
* @param mixed $name Resource name input | * @param mixed $name Shared folder name input | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function passes($attribute, $name): bool | public function passes($attribute, $name): bool | ||||
{ | { | ||||
if (empty($name) || !is_string($name)) { | if (empty($name) || !is_string($name) || $name == 'Resources') { | ||||
$this->message = \trans('validation.nameinvalid'); | |||||
return false; | |||||
} | |||||
if (strcspn($name, self::FORBIDDEN_CHARS) < strlen($name)) { | |||||
$this->message = \trans('validation.nameinvalid'); | $this->message = \trans('validation.nameinvalid'); | ||||
return false; | return false; | ||||
} | } | ||||
// Check the max length, according to the database column length | // Check the max length, according to the database column length | ||||
if (strlen($name) > 191) { | if (strlen($name) > 191) { | ||||
$this->message = \trans('validation.max.string', ['max' => 191]); | $this->message = \trans('validation.max.string', ['max' => 191]); | ||||
return false; | return false; | ||||
} | } | ||||
// Check if specified domain is belongs to the user | // Check if specified domain belongs to the user | ||||
$domains = \collect($this->owner->domains(true, false))->pluck('namespace')->all(); | $domains = \collect($this->owner->domains(true, false))->pluck('namespace')->all(); | ||||
if (!in_array($this->domain, $domains)) { | if (!in_array($this->domain, $domains)) { | ||||
$this->message = \trans('validation.domaininvalid'); | $this->message = \trans('validation.domaininvalid'); | ||||
return false; | return false; | ||||
} | } | ||||
// Check if the name is unique in the domain | // Check if the name is unique in the domain | ||||
// FIXME: Maybe just using the whole resources table would be faster than resources()? | // FIXME: Maybe just using the whole shared_folders table would be faster than sharedFolders()? | ||||
$exists = $this->owner->resources() | $exists = $this->owner->sharedFolders() | ||||
->where('resources.name', $name) | ->where('shared_folders.name', $name) | ||||
->where('resources.email', 'like', '%@' . $this->domain) | ->where('shared_folders.email', 'like', '%@' . $this->domain) | ||||
->exists(); | ->exists(); | ||||
if ($exists) { | if ($exists) { | ||||
$this->message = \trans('validation.nameexists'); | $this->message = \trans('validation.nameexists'); | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
Show All 12 Lines |