Completed
Pull Request — master (#12)
by ARCANEDEV
14:50 queued 10:14
created

ImpersonationPolicy::canBeImpersonatedPolicy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelImpersonator\Policies;
6
7
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
8
use Arcanedev\LaravelPolicies\Policy;
9
use Illuminate\Foundation\Auth\User;
10
11
/**
12
 * Class     ImpersonationPolicy
13
 *
14
 * @package  Arcanedev\LaravelImpersonator\Policies
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class ImpersonationPolicy extends Policy
18
{
19
    /* -----------------------------------------------------------------
20
     |  Main Methods
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Get the ability's prefix.
26
     *
27
     * @return string
28
     */
29 168
    protected static function prefix(): string
30
    {
31 168
        return 'auth.impersonator.';
32
    }
33
34 168
    public function abilities(): iterable
35
    {
36
        return [
37
38
            // auth.impersonator.can-impersonate
39 168
            static::makeAbility('can-impersonate', 'canImpersonate')->setMetas([
40 168
                'name'        => 'Ability to impersonate',
41
                'description' => 'Ability to impersonate other users',
42
            ]),
43
44
            // auth.impersonator.can-be-impersonated
45 168
            static::makeAbility('can-be-impersonated', 'canBeImpersonated')->setMetas([
46 168
                'name'        => 'Ability to be impersonated',
47
                'description' => 'Ability to be impersonated by other users',
48
            ]),
49
50
        ];
51
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Policies
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Check if the current user can impersonate.
60
     *
61
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
62
     *
63
     * @return bool
64
     */
65 36
    public function canImpersonate(User $user): bool
66
    {
67 36
        if ( ! $this->isEnabled()) {
68 6
            return false;
69
        }
70
71 30
        return $user->canImpersonate();
72
    }
73
74
    /**
75
     * Check if the given user can be impersonated.
76
     *
77
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
78
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable                                   $impersonated
79
     *
80
     * @return bool
81
     */
82 36
    public function canBeImpersonated(User $user, Impersonatable $impersonated): bool
83
    {
84 36
        if ( ! $this->canImpersonate($user)) {
85 12
            return false;
86
        }
87
88 24
        return $impersonated->canBeImpersonated();
89
    }
90
91
    /* -----------------------------------------------------------------
92
     |  Other Methods
93
     | -----------------------------------------------------------------
94
     */
95
96
    /**
97
     * Check if the impersonation is enabled.
98
     *
99
     * @return bool
100
     */
101 36
    private function isEnabled()
102
    {
103 36
        return config('impersonator.enabled', false);
104
    }
105
}
106