| Total Complexity | 18 |
| Total Lines | 144 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | trait UsesPolicies |
||
| 11 | { |
||
| 12 | use AuthorizesRequests; |
||
| 13 | |||
| 14 | /** bulk authorize based on policies |
||
| 15 | */ |
||
| 16 | public function authorizeAll(): void |
||
| 17 | { |
||
| 18 | if (! auth()->check()) { |
||
| 19 | return; |
||
| 20 | } |
||
| 21 | |||
| 22 | $model = $this->crud->getModel(); |
||
|
1 ignored issue
–
show
|
|||
| 23 | $authArray = [ |
||
| 24 | 'list' => 'view', |
||
| 25 | 'create' => 'create', |
||
| 26 | 'edit' => 'update', |
||
| 27 | 'update' => 'update', |
||
| 28 | 'destroy' => 'delete', |
||
| 29 | 'delete' => 'delete', |
||
| 30 | 'show' => 'view', |
||
| 31 | 'details_row' => 'view', |
||
| 32 | ]; |
||
| 33 | $done = []; |
||
| 34 | foreach ($authArray as $key => $ability) { |
||
| 35 | $this->crud->denyAccess([$ability]); |
||
| 36 | if (\in_array($ability, $done, true)) { |
||
| 37 | $this->crud->allowAccess([$key]); |
||
| 38 | continue; |
||
| 39 | } |
||
| 40 | |||
| 41 | switch ($ability) { |
||
| 42 | case 'view': |
||
| 43 | if (auth()->user()->can($ability, $model)) { |
||
| 44 | $this->crud->allowAccess([$key]); |
||
| 45 | } |
||
| 46 | break; |
||
| 47 | case 'create': |
||
| 48 | if (auth()->user()->can($ability, \get_class($model))) { |
||
| 49 | $this->crud->allowAccess([$key]); |
||
| 50 | } |
||
| 51 | break; |
||
| 52 | default: |
||
| 53 | if (auth()->user()->can($ability, $model)) { |
||
| 54 | $this->crud->allowAccess([$key]); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | $done[] = $ability; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 63 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 64 | */ |
||
| 65 | public function create() |
||
| 66 | { |
||
| 67 | $this->policyAuthorize('create', \get_class($this->crud->getModel())); |
||
|
1 ignored issue
–
show
|
|||
| 68 | |||
| 69 | return parent::create(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $id |
||
| 74 | * |
||
| 75 | * @return |
||
| 76 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 77 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 78 | */ |
||
| 79 | public function destroy($id) |
||
| 80 | { |
||
| 81 | $this->policyAuthorize('delete', $this->crud->getModel(), $id); |
||
|
1 ignored issue
–
show
|
|||
| 82 | |||
| 83 | return parent::destroy($id); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $id |
||
| 88 | * |
||
| 89 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 90 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 91 | */ |
||
| 92 | public function edit($id) |
||
| 93 | { |
||
| 94 | $this->policyAuthorize('update', $this->crud->getModel(), $id); |
||
|
1 ignored issue
–
show
|
|||
| 95 | |||
| 96 | return parent::edit($id); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 101 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 102 | */ |
||
| 103 | public function index() |
||
| 104 | { |
||
| 105 | $this->policyAuthorize('list', \get_class($this->crud->getModel())); |
||
|
1 ignored issue
–
show
|
|||
| 106 | |||
| 107 | return parent::index(); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function list() |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $id |
||
| 117 | * |
||
| 118 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 119 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 120 | */ |
||
| 121 | public function show($id) |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Determines the access to allow based on policy. |
||
| 130 | * |
||
| 131 | * @param string $ability The ability to validate |
||
| 132 | * @param string|Model $class The instance of a Model class to check against |
||
| 133 | * @param int|null $id The id of the individual to check against |
||
| 134 | * |
||
| 135 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 136 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 137 | */ |
||
| 138 | protected function policyAuthorize($ability, $class, $id = null): void |
||
| 154 | } |
||
| 155 | } |
||
| 156 |