Passed
Push — master ( 67253e...1a1f73 )
by Arthur
04:53 queued 11s
created

OwnershipPolicy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 81
ccs 2
cts 18
cp 0.1111
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A access() 0 3 1
A userIsModelOwner() 0 14 3
A create() 0 3 1
A before() 0 4 1
A update() 0 3 1
A delete() 0 3 1
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
12
use Foundation\Abstracts\Policies\Policy;
13
use Foundation\Contracts\ModelPolicyContract;
14
use Foundation\Contracts\Ownable;
15
use Foundation\Exceptions\Exception;
16
use Illuminate\Auth\Access\HandlesAuthorization;
17
use Illuminate\Contracts\Auth\Authenticatable;
18
use Modules\User\Entities\User;
19
20
class OwnershipPolicy extends Policy implements ModelPolicyContract
21
{
22
23
    use HandlesAuthorization;
24
25
    /**
26
     * Determine if the given user can access the model.
27
     *
28
     * @param  User $user
29
     * @return bool
30
     * @throws Exception
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
     * @return bool
41
     * @throws Exception
42
     */
43
    private function userIsModelOwner(User $user, Ownable $model): bool
44
    {
45
        if (classImplementsInterface($model->ownedBy(), Authenticatable::class)) {
46
            return $user->id === $model->ownerId();
47
        }
48
49
        $ownerModel = $model->ownedBy();
50
        $owner = $ownerModel::find($model->ownerId());
51
52
        if (classImplementsInterface($owner, Ownable::class)) {
53
            return $this->userIsModelOwner($user, $owner);
54
        }
55
56
        throw new Exception("recursive ownershippolicy lookup failed. Not all models implemented ownable so couldn't identify if user owned model");
57
    }
58
59
    /**
60
     * Determine if the given user can access the model.
61
     *
62
     * @param  User $user
63
     * @return bool
64
     */
65
    public function create(User $user): bool
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * Determine if the given user can update the model.
72
     *
73
     * @param  User $user
74
     * @return bool
75
     * @throws Exception
76
     */
77
    public function update(User $user, $model): bool
78
    {
79
        return $this->userIsModelOwner($user, $model);
80
    }
81
82
    /**
83
     * @param User $user
84
     * @param $model
85
     * @return bool
86
     */
87
    public function delete(User $user, $model): bool
88
    {
89
        return true;
90
    }
91
92
    /**
93
     * @param $user
94
     * @param $ability
95
     * @return null
96
     */
97 1
    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

97
    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...
Unused Code introduced by
The parameter $user 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

97
    public function before(/** @scrutinizer ignore-unused */ $user, $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...
98
    {
99
        //TODO IMPLEMENT CHECK USER IS ADMIN
100 1
        return null;
101
    }
102
103
}
104