Refresh
This commit is contained in:
48
app/Http/Requests/Folders/SetPermissionsRequest.php
Executable file
48
app/Http/Requests/Folders/SetPermissionsRequest.php
Executable file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Folders;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SetPermissionsRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return $this->user()->can('setPermission', $this->folder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'ability' => [
|
||||
'nullable',
|
||||
Rule::in([
|
||||
'can_create_folder',
|
||||
'can_update_folder',
|
||||
'can_delete_folder',
|
||||
'can_create_document',
|
||||
'can_delete_document',
|
||||
]),
|
||||
],
|
||||
'granted' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'user_id' => [
|
||||
'nullable',
|
||||
Rule::exists('users', 'id'),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
51
app/Http/Requests/Folders/StoreRequest.php
Executable file
51
app/Http/Requests/Folders/StoreRequest.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Folders;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Group;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
$parentFolder = Folder::find($this->parent_id);
|
||||
|
||||
return $this->user()->can('createIn', $parentFolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$groupId = $this->group_id;
|
||||
|
||||
return [
|
||||
'title' => [
|
||||
'required',
|
||||
'max:255',
|
||||
],
|
||||
// Parent folder ID must exist and in the same group as requested
|
||||
'parent_id' => [
|
||||
'required',
|
||||
Rule::exists(Folder::class, 'id')->where(function ($query) use ($groupId) {
|
||||
$query->where('group_id', '=', $groupId);
|
||||
}),
|
||||
],
|
||||
'group_id' => [
|
||||
'required',
|
||||
Rule::exists(Group::class, 'id'),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
107
app/Http/Requests/Folders/UpdateRequest.php
Executable file
107
app/Http/Requests/Folders/UpdateRequest.php
Executable file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Folders;
|
||||
|
||||
use App\Models\Folder;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
if (empty($this->parent_id)) {
|
||||
return 'root' === $this->folder->type;
|
||||
}
|
||||
|
||||
$parent = Folder::find($this->parent_id);
|
||||
|
||||
return $this->user()->can('createIn', $parent) && $this->user()->can('update', $this->folder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$groupId = $this->group_id;
|
||||
|
||||
return [
|
||||
'title' => [
|
||||
'required',
|
||||
'max:255',
|
||||
],
|
||||
'parent_id' => [
|
||||
'nullable',
|
||||
Rule::exists('App\Models\Folder', 'id')->where(function ($query) use ($groupId) {
|
||||
$query->where('group_id', '=', $groupId);
|
||||
}),
|
||||
],
|
||||
'group_id' => [
|
||||
'required',
|
||||
Rule::exists('App\Models\Group', 'id'),
|
||||
],
|
||||
'is_expanded' => [
|
||||
'sometimes',
|
||||
'boolean',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the validator instance.
|
||||
*
|
||||
* @param \Illuminate\Validation\Validator $validator
|
||||
*/
|
||||
public function withValidator($validator)
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
if ($this->isMoving()) {
|
||||
if ('folder' !== $this->folder->type) {
|
||||
// Trying to move a "special" folder like root
|
||||
$validator->errors()->add('parent_id', __('You cannot move this folder'));
|
||||
} elseif ($this->targetParentIsDescendant()) {
|
||||
$validator->errors()->add('parent_id', __('You cannot move this folder to a descendant'));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a boolean value indicating if we're moving a folder.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isMoving()
|
||||
{
|
||||
return $this->parent_id !== $this->folder->parent_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a boolean value indicating if we're trying to move a folder into
|
||||
* one of its descendants.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function targetParentIsDescendant()
|
||||
{
|
||||
$parent = Folder::find($this->parent_id);
|
||||
|
||||
while ($parent) {
|
||||
if ($parent->id === $this->folder->id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$parent = $parent->parent;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user