Passed
Push — master ( 85c419...35703b )
by Arthur
07:22
created

OwnershipPolicy::userIsModelOwner()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 12
rs 10
c 0
b 0
f 0
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
    public function access($user, $model): bool
33
    {
34
        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
    private function userIsModelOwner(User $user, Ownable $model): bool
46
    {
47
        if (class_implements_interface($model->ownedBy(), Authenticatable::class)) {
48
            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
    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
        if ($user->isAdmin())
106
            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
109
}
110