ImpersonationPolicy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 94
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prefix() 0 4 1
A abilities() 0 18 1
A canImpersonate() 0 8 2
A canBeImpersonated() 0 8 2
A isEnabled() 0 4 1
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
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class ImpersonationPolicy extends Policy
17
{
18
    /* -----------------------------------------------------------------
19
     |  Main Methods
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Get the ability's prefix.
25
     *
26
     * @return string
27
     */
28 112
    protected static function prefix(): string
29
    {
30 112
        return 'auth::impersonator.';
31
    }
32
33
    /**
34
     * Get the abilities.
35
     *
36
     * @return iterable
37
     */
38 112
    public function abilities(): iterable
39
    {
40
        return [
41
42
            // auth::impersonator.can-impersonate
43 112
            static::makeAbility('can-impersonate')->setMetas([
44 112
                'name'        => 'Ability to impersonate',
45
                'description' => 'Ability to impersonate other users',
46
            ]),
47
48
            // auth::impersonator.can-be-impersonated
49 112
            static::makeAbility('can-be-impersonated')->setMetas([
50 112
                'name'        => 'Ability to be impersonated',
51
                'description' => 'Ability to be impersonated by other users',
52
            ]),
53
54
        ];
55
    }
56
57
    /* -----------------------------------------------------------------
58
     |  Policies
59
     | -----------------------------------------------------------------
60
     */
61
62
    /**
63
     * Check if the current user can impersonate.
64
     *
65
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
66
     *
67
     * @return bool
68
     */
69 24
    public function canImpersonate(User $user): bool
70
    {
71 24
        if ( ! $this->isEnabled()) {
72 4
            return false;
73
        }
74
75 20
        return $user->canImpersonate();
76
    }
77
78
    /**
79
     * Check if the given user can be impersonated.
80
     *
81
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User  $user
82
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable                                   $impersonated
83
     *
84
     * @return bool
85
     */
86 24
    public function canBeImpersonated(User $user, Impersonatable $impersonated): bool
87
    {
88 24
        if ( ! $this->canImpersonate($user)) {
89 8
            return false;
90
        }
91
92 16
        return $impersonated->canBeImpersonated();
93
    }
94
95
    /* -----------------------------------------------------------------
96
     |  Other Methods
97
     | -----------------------------------------------------------------
98
     */
99
100
    /**
101
     * Check if the impersonation is enabled.
102
     *
103
     * @return bool
104
     */
105 24
    private function isEnabled()
106
    {
107 24
        return config('impersonator.enabled', false);
108
    }
109
}
110