Passed
Push — master ( 7bc374...cf39f3 )
by Arthur
05:05
created

OwnershipPolicy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 31.25%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 77
ccs 5
cts 16
cp 0.3125
rs 10
c 0
b 0
f 0
wmc 7

5 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
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 3
    public function access($user, $model): bool
33
    {
34 3
        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 3
    private function userIsModelOwner(User $user, Ownable $model): bool
46
    {
47 3
        if (class_implements_interface($model->ownedBy(), Authenticatable::class)) {
48 3
            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)
104
    * {
105
        * //TODO IMPLEMENT CHECK USER IS ADMIN
106
        * return null;
107
     * }
108
     */
109
}
110