User::firstHashedOrFail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Auth\Models;
2
3
use Arcanedev\LaravelAuth\Models\User as BaseUser;
4
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
5
use Arcanedev\LaravelImpersonator\Traits\CanImpersonate;
6
use Illuminate\Database\Eloquent\Builder;
7
8
/**
9
 * Class     User
10
 *
11
 * @package  Arcanesoft\Auth\Models
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @property  \Arcanesoft\Auth\Models\PasswordReset  passwordReset
15
 */
16
class User extends BaseUser implements Impersonatable
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
23
    use Presenters\UserPresenter,
24
        CanImpersonate;
25
26
    /* -----------------------------------------------------------------
27
     |  Relationships
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * Password reset relationship.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
35
     */
36
    public function passwordReset()
37
    {
38
        return $this->hasOne(PasswordReset::class, 'email', 'email');
39
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Scopes
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Protect admins.
48
     *
49
     * @param  \Illuminate\Database\Eloquent\Builder  $query
50
     *
51
     * @return \Illuminate\Database\Eloquent\Builder
52
     */
53
    public function scopeProtectAdmins(Builder $query)
54
    {
55
        /** @var  self  $user */
56
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
58
        return ($user && $user->is_admin)
59
            ? $query
60
            : $query->where('is_admin', false);
61
    }
62
63
    /* -----------------------------------------------------------------
64
     |  Main Methods
65
     | -----------------------------------------------------------------
66
     */
67
68
    /**
69
     * Get a user from a hashed id or fail if not found.
70
     *
71
     * @param  string  $hashedId
72
     *
73
     * @return self
74
     *
75
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
76
     */
77
    public static function firstHashedOrFail($hashedId)
78
    {
79
        return self::withTrashed()->protectAdmins()->withHashedId($hashedId)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method withTrashed() does not exist on Arcanesoft\Auth\Models\User. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
    }
81
82
    /* -----------------------------------------------------------------
83
     |  Check Methods
84
     | -----------------------------------------------------------------
85
     */
86
87
    /**
88
     * Check if user is an administrator.
89
     *
90
     * @return bool
91
     */
92
    public function isAdmin()
93
    {
94
        return parent::isAdmin() || $this->hasRoleSlug(Role::ADMINISTRATOR);
95
    }
96
97
    /**
98
     * Check if user is a moderator.
99
     *
100
     * @return bool
101
     */
102
    public function isModerator()
103
    {
104
        return $this->hasRoleSlug(Role::MODERATOR);
105
    }
106
107
    /**
108
     * Check if user is a member.
109
     *
110
     * @return bool
111
     */
112
    public function isMember()
113
    {
114
        return $this->hasRoleSlug(Role::MEMBER);
115
    }
116
117
    /**
118
     * Check if the user is deletable.
119
     *
120
     * @return bool
121
     */
122
    public function isDeletable()
123
    {
124
        return ! $this->isAdmin();
125
    }
126
127
    /**
128
     * Check if user has a password reset.
129
     *
130
     * @return bool
131
     */
132
    public function hasPasswordReset()
133
    {
134
        return ! is_null($this->passwordReset);
135
    }
136
137
    /**
138
     * Check if the current modal can impersonate other models.
139
     *
140
     * @return  bool
141
     */
142
    public function canImpersonate()
143
    {
144
        return impersonator()->isEnabled() && $this->isAdmin();
145
    }
146
147
    /**
148
     * Check if the current model can be impersonated.
149
     *
150
     * @return  bool
151
     */
152
    public function canBeImpersonated()
153
    {
154
        return impersonator()->isEnabled() && ! $this->isAdmin();
155
    }
156
}
157