Passed
Push — master ( 5f3af1...18e61f )
by Arthur
04:52
created

OwnershipPolicy::before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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
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
     * public function before($user, $ability)
98
    * {
99
        * //TODO IMPLEMENT CHECK USER IS ADMIN
100
        * return null;
101
     * }
102
     */
103
104
}
105