source: app/Http/Requests/Dashboard/FolderRequest.php

Last change on this file was 190db9f, checked in by beratkjufliju <kufliju@…>, 3 years ago

edited user permissions

  • Property mode set to 100644
File size: 2.2 KB
Line 
1<?php
2
3namespace App\Http\Requests\Dashboard;
4
5use App\Models\Department;
6use App\Models\FileType;
7use App\Models\Folder;
8use Illuminate\Foundation\Http\FormRequest;
9
10class FolderRequest extends FormRequest
11{
12 /**
13 * Determine if the user is authorized to make this request.
14 *
15 * @return bool
16 */
17 public function authorize()
18 {
19 if ($this->isMethod("patch")) {
20 $folder = Folder::find($this->route("id"));
21 return auth()->user()->hasPermission("manage_all_folders") || ($folder->user->id == auth()->user()->id);
22 }
23
24 return true;
25 }
26
27 /**
28 * Get the validation rules that apply to the request.
29 *
30 * @return array
31 */
32 public function rules()
33 {
34 $rules = [
35 "arch_id" => [
36 "required",
37 function ($attribute, $value, $fail) {
38
39 try {
40 $arch_id = $this->request->get('arch_id');
41
42 $deptCode = explode('/', $arch_id)[0];
43 $archNum = explode('/', $arch_id)[1];
44
45 if (empty($archNum)) {
46 $fail("Please enter folders Archive ID");
47 }
48
49 if ($deptCode != Department::find($this->request->get('department'))->code) {
50 $fail("Folder Archive ID field format is invalid");
51 }
52 }
53 catch (\Exception $e) {
54 $fail("Please enter folders Archive ID");
55 }
56 }
57 ],
58 "name" => "required|min:2|max:30",
59 "department" => "required|integer|exists:departments,id",
60 ];
61
62 $mimes = FileType::find("1")->mimes;
63 $maxSize = FileType::find("1")->max_size;
64
65 if ($this->isMethod("patch")) {
66 $fileRules = [
67 "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
68 ];
69 }
70
71 else {
72 $fileRules = [
73 "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
74 ];
75 }
76
77 $rules = array_merge(
78 $rules,
79 $fileRules,
80 );
81
82 return $rules;
83 }
84}
Note: See TracBrowser for help on using the repository browser.