Completed
Push — master ( 1b5b46...498a70 )
by ARCANEDEV
12s queued 10s
created

ImpersonationPolicy::canImpersonate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
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 174
    protected static function prefix(): string
30
    {
31 174
        return 'auth::impersonator.';
32
    }
33
34
    /**
35
     * Get the abilities.
36
     *
37
     * @return iterable
38
     */
39 174
    public function abilities(): iterable
40
    {
41
        return [
42
43
            // auth::impersonator.can-impersonate
44 174
            static::makeAbility('can-impersonate')->setMetas([
45 174
                'name'        => 'Ability to impersonate',
46
                'description' => 'Ability to impersonate other users',
47
            ]),
48
49
            // auth::impersonator.can-be-impersonated
50 174
            static::makeAbility('can-be-impersonated')->setMetas([
51 174
                'name'        => 'Ability to be impersonated',
52
                'description' => 'Ability to be impersonated by other users',
53
            ]),
54
55
        ];
56
    }
57
58
    /* -----------------------------------------------------------------
59
     |  Policies
60
     | -----------------------------------------------------------------
61
     */
62
63
    /**
64
     * Check if the current user can impersonate.
65
     *
66
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
67
     *
68
     * @return bool
69
     */
70 36
    public function canImpersonate(User $user): bool
71
    {
72 36
        if ( ! $this->isEnabled()) {
73 6
            return false;
74
        }
75
76 30
        return $user->canImpersonate();
77
    }
78
79
    /**
80
     * Check if the given user can be impersonated.
81
     *
82
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
83
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable                                   $impersonated
84
     *
85
     * @return bool
86
     */
87 36
    public function canBeImpersonated(User $user, Impersonatable $impersonated): bool
88
    {
89 36
        if ( ! $this->canImpersonate($user)) {
90 12
            return false;
91
        }
92
93 24
        return $impersonated->canBeImpersonated();
94
    }
95
96
    /* -----------------------------------------------------------------
97
     |  Other Methods
98
     | -----------------------------------------------------------------
99
     */
100
101
    /**
102
     * Check if the impersonation is enabled.
103
     *
104
     * @return bool
105
     */
106 36
    private function isEnabled()
107
    {
108 36
        return config('impersonator.enabled', false);
109
    }
110
}
111