CheckPermission::checkPermission()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace App\Traits;
3
4
use Symfony\Component\Finder\Exception\AccessDeniedException;
5
use Illuminate\Support\Facades\Auth;
6
7
trait CheckPermission
8
{
9
    public function checkPermission(string $permissionName)
10
    {
11
        if (!Auth::user()->hasPermissionTo($permissionName)) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

11
        if (!Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo($permissionName)) {
Loading history...
12
            throw new AccessDeniedException("You have not the permission to view this page", 403);
13
        }
14
    }
15
16
    /**
17
    * Check permmission and also allows the owner of the model.
18
    **/
19
    public function checkPermissionAllowOwner(string $permissionName, $entity)
20
    {
21
        $userId = $entity->user_id ?? 'none';
22
        
23
        if (!( Auth::user()->hasPermissionTo($permissionName) || Auth::id() === $userId)) {
24
            throw new AccessDeniedException("You have not the permission to view this page", 403);
25
        }
26
    }
27
28
29
30
}
31