Completed
Push — master ( ae7583...062bf2 )
by ARCANEDEV
04:07
created

User::isDeletable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: can, forceDelete, forceFill, getAuthIdentifier, getAuthIdentifierName, getAuthPassword, getEmailForPasswordReset, getKey, getRememberToken, getRememberTokenName, isForceDeleting, restore, sendPasswordResetNotification, setAttribute, setRememberToken, trashed
Loading history...
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();
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();
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