CheckPermission::checkPermissionAllowOwner()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 12
rs 10
c 1
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