add nova
This commit is contained in:
34
nova/src/Rules/Filename.php
Normal file
34
nova/src/Rules/Filename.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Filename implements Rule
|
||||
{
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* https://stackoverflow.com/a/1032128
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return preg_match('/^[^\\/?*:;{}\\\\]+\\.[^\\/?*:;{}\\\\]{3}$/', $value) !== false
|
||||
&& ! Str::contains($value, DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return 'The :attribute field format is invalid.';
|
||||
}
|
||||
}
|
||||
62
nova/src/Rules/NotAttached.php
Normal file
62
nova/src/Rules/NotAttached.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
|
||||
class NotAttached implements Rule
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Laravel\Nova\Http\Requests\NovaRequest
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* The model instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public $model;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(NovaRequest $request, $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return ! in_array(
|
||||
$this->request->input($this->request->relatedResource),
|
||||
$this->model->{$this->request->viaRelationship}()
|
||||
->withoutGlobalScopes()->get()->modelKeys()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return trans('nova::validation.attached');
|
||||
}
|
||||
}
|
||||
94
nova/src/Rules/NotExactlyAttached.php
Normal file
94
nova/src/Rules/NotExactlyAttached.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Laravel\Nova\Nova;
|
||||
use Laravel\Nova\ResourceToolElement;
|
||||
|
||||
class NotExactlyAttached implements Rule
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Laravel\Nova\Http\Requests\NovaRequest
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* The model instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public $model;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
|
||||
* @param \Illuminate\Database\Eloquent\Model|null $model
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(NovaRequest $request, $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
/** @var \Illuminate\Database\Eloquent\Relations\MorphToMany|\Illuminate\Database\Eloquent\Relations\BelongsToMany $relation */
|
||||
$relation = $this->model->{$this->request->viaRelationship}();
|
||||
|
||||
$pivot = $relation->newPivot();
|
||||
$pivotAccessor = $relation->getPivotAccessor();
|
||||
$query = $relation->withoutGlobalScopes()
|
||||
->where($relation->getQualifiedRelatedPivotKeyName(), '=', $this->request->input($this->request->relatedResource));
|
||||
|
||||
$resource = Nova::newResourceFromModel($this->model);
|
||||
|
||||
$fields = $resource->resolvePivotFields($this->request, $this->request->relatedResource)
|
||||
->reject(function ($field) {
|
||||
return $field instanceof ResourceToolElement || $field->computed();
|
||||
});
|
||||
|
||||
if ($fields->isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$fields->each(function ($field) use ($pivot) {
|
||||
$field->fill($this->request, $pivot);
|
||||
});
|
||||
|
||||
$attributes = $pivot->toArray();
|
||||
|
||||
foreach ($query->cursor() as $result) {
|
||||
$pivots = Arr::only($result->{$pivotAccessor}->toArray(), array_keys($attributes));
|
||||
|
||||
if (array_diff_assoc(Arr::flatten($pivots), Arr::flatten($attributes)) === []) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return trans('nova::validation.attached');
|
||||
}
|
||||
}
|
||||
129
nova/src/Rules/Relatable.php
Normal file
129
nova/src/Rules/Relatable.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Laravel\Nova\Fields\HasOne;
|
||||
use Laravel\Nova\Fields\MorphOne;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Laravel\Nova\Nova;
|
||||
|
||||
class Relatable implements Rule
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Laravel\Nova\Http\Requests\NovaRequest
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* The query builder instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public $query;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(NovaRequest $request, $query)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$model = $this->query->tap(function ($query) {
|
||||
tap($query->getQuery(), function ($builder) {
|
||||
/** @var \Illuminate\Database\Query\Builder $builder */
|
||||
$builder->orders = [];
|
||||
|
||||
$builder->select(
|
||||
! empty($builder->joins) ? $builder->from.'.*' : '*'
|
||||
);
|
||||
});
|
||||
})->whereKey($value)->first();
|
||||
|
||||
if (! $model) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->relationshipIsFull($model, $attribute, $value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($resource = Nova::resourceForModel($model)) {
|
||||
return $this->authorize($resource, $model);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the relationship is "full".
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function relationshipIsFull($model, $attribute, $value)
|
||||
{
|
||||
$inverseRelation = $this->request->newResource()
|
||||
->resolveInverseFieldsForAttribute($this->request, $attribute)->first(function ($field) {
|
||||
return ($field instanceof MorphOne || $field instanceof HasOne) && ! $field->ofManyRelationship();
|
||||
});
|
||||
|
||||
if ($inverseRelation && $this->request->resourceId) {
|
||||
$modelBeingUpdated = $this->request->findModelOrFail();
|
||||
|
||||
if (is_null($modelBeingUpdated->{$attribute})) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($modelBeingUpdated->{$attribute}->getKey() == $value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $inverseRelation &&
|
||||
$model->{$inverseRelation->attribute}()->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorize that the user is allowed to relate this resource.
|
||||
*
|
||||
* @param class-string<\Laravel\Nova\Resource> $resourceClass
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($resourceClass, $model)
|
||||
{
|
||||
return (new $resourceClass($model))->authorizedToAdd(
|
||||
$this->request, $this->request->model()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return trans('nova::validation.relatable');
|
||||
}
|
||||
}
|
||||
39
nova/src/Rules/RelatableAttachment.php
Normal file
39
nova/src/Rules/RelatableAttachment.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Rules;
|
||||
|
||||
use Laravel\Nova\Nova;
|
||||
|
||||
class RelatableAttachment extends Relatable
|
||||
{
|
||||
/**
|
||||
* Authorize that the user is allowed to relate this resource.
|
||||
*
|
||||
* @param string $resource
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($resource, $model)
|
||||
{
|
||||
$parentResource = Nova::newResourceFromModel($this->request->findModelOrFail());
|
||||
|
||||
return $parentResource->authorizedToAttachAny(
|
||||
$this->request, $model
|
||||
) || $parentResource->authorizedToAttach(
|
||||
$this->request, $model
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the relationship is "full".
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function relationshipIsFull($model, $attribute, $value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user