Passed
Push — master ( a9574a...4c3e72 )
by Arthur
07:41
created

OwnershipPolicy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 42.11%

Importance

Changes 0
Metric Value
wmc 9
eloc 15
dl 0
loc 88
ccs 8
cts 19
cp 0.4211
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A update() 0 3 1
A delete() 0 3 1
A access() 0 3 1
A userIsModelOwner() 0 14 3
A before() 0 4 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 11.10.18
6
 * Time: 15:35.
7
 */
8
9
namespace Foundation\Policies;
10
11
use Foundation\Abstracts\Policies\Policy;
12
use Foundation\Contracts\ModelPolicyContract;
13
use Foundation\Contracts\Ownable;
14
use Foundation\Exceptions\Exception;
15
use Illuminate\Auth\Access\HandlesAuthorization;
16
use Illuminate\Contracts\Auth\Authenticatable;
17
use Modules\User\Entities\User;
18
19
class OwnershipPolicy extends Policy implements ModelPolicyContract
20
{
21
    use HandlesAuthorization;
22
23
    /**
24
     * Determine if the given user can access the model.
25
     *
26
     * @param User $user
27
     *
28
     * @throws Exception
29
     *
30
     * @return bool
31
     */
32 6
    public function access($user, $model): bool
33
    {
34 6
        return $this->userIsModelOwner($user, $model);
35
    }
36
37
    /**
38
     * @param User    $user
39
     * @param Ownable $model
40
     *
41
     * @throws Exception
42
     *
43
     * @return bool
44
     */
45 6
    private function userIsModelOwner(User $user, Ownable $model): bool
46
    {
47 6
        if (class_implements_interface($model->ownedBy(), Authenticatable::class)) {
48 6
            return $user->id === $model->ownerId();
49
        }
50
51
        $ownerModel = $model->ownedBy();
52
        $owner = $ownerModel::find($model->ownerId());
53
54
        if (class_implements_interface($owner, Ownable::class)) {
55
            return $this->userIsModelOwner($user, $owner);
56
        }
57
58
        throw new Exception("recursive ownershippolicy lookup failed. Not all models implemented ownable so couldn't identify if user owned model");
59
    }
60
61
    /**
62
     * Determine if the given user can access the model.
63
     *
64
     * @param User $user
65
     *
66
     * @return bool
67
     */
68
    public function create(User $user): bool
69
    {
70
        return true;
71
    }
72
73
    /**
74
     * Determine if the given user can update the model.
75
     *
76
     * @param User $user
77
     *
78
     * @throws Exception
79
     *
80
     * @return bool
81
     */
82
    public function update(User $user, $model): bool
83
    {
84
        return $this->userIsModelOwner($user, $model);
85
    }
86
87
    /**
88
     * @param User $user
89
     * @param $model
90
     *
91
     * @return bool
92
     */
93
    public function delete(User $user, $model): bool
94
    {
95
        return true;
96
    }
97
98
    /*
99
     * @param $user
100
     * @param $ability
101
     * @return null
102
    */
103 7
    public function before($user, $ability)
0 ignored issues
show
Unused Code introduced by
The parameter $ability is not used and could be removed. ( Ignorable by Annotation )

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

103
    public function before($user, /** @scrutinizer ignore-unused */ $ability)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105 7
        if ($user->isAdmin()) {
106 1
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type null.
Loading history...
107
        }
108 6
    }
109
}
110