CheckPermission   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 18
ccs 0
cts 8
cp 0
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPermission() 0 4 2
A checkPermissionAllowOwner() 0 6 3
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