Completed
Push — master ( 3ba18f...ea2023 )
by ARCANEDEV
8s
created

User::findUnconfirmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\User as Authenticatable;
4
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException;
5
use Arcanedev\LaravelAuth\Services\SocialAuthenticator;
6
use Arcanedev\LaravelAuth\Services\UserConfirmator;
7
use Arcanedev\LaravelAuth\Models\Traits\Activatable;
8
use Arcanedev\LaravelAuth\Models\Traits\AuthUserTrait;
9
use Arcanesoft\Contracts\Auth\Models\User as UserContract;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class     User
15
 *
16
 * @package  Arcanedev\LaravelAuth\Models
17
 * @author   ARCANEDEV <[email protected]>
18
 *
19
 * @property  int                                       id
20
 * @property  string                                    username
21
 * @property  string                                    first_name
22
 * @property  string                                    last_name
23
 * @property  string                                    full_name
24
 * @property  string                                    email
25
 * @property  string                                    password
26
 * @property  string                                    remember_token
27
 * @property  bool                                      is_admin
28
 * @property  bool                                      is_active
29
 * @property  bool                                      is_confirmed       (Optional)
30
 * @property  string                                    confirmation_code  (Optional)
31
 * @property  \Carbon\Carbon                            confirmed_at       (Optional)
32
 * @property  \Carbon\Carbon                            last_activity
33
 * @property  \Carbon\Carbon                            created_at
34
 * @property  \Carbon\Carbon                            updated_at
35
 * @property  \Carbon\Carbon                            deleted_at
36
 * @property  \Illuminate\Database\Eloquent\Collection  roles
37
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
38
 *
39
 * @method  static  bool                                   insert(array $values)
40
 * @method          \Illuminate\Database\Eloquent\Builder  unconfirmed(string $code)
41
 * @method          \Illuminate\Database\Eloquent\Builder  lastActive(int $minutes = null)
42
 */
43
class User extends Authenticatable implements UserContract
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, getAuthIdentifier, getAuthIdentifierName, getAuthPassword, getEmailForPasswordReset, getRememberToken, getRememberTokenName, sendPasswordResetNotification, setAttribute, setRememberToken
Loading history...
44
{
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Traits
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    use AuthUserTrait, Activatable, SoftDeletes;
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Properties
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * The attributes that are mass assignable.
57
     *
58
     * @var array
59
     */
60
    protected $fillable = [
61
        'username',
62
        'first_name',
63
        'last_name',
64
        'email',
65
        'password',
66
    ];
67
68
    /**
69
     * The attributes excluded from the model's JSON form.
70
     *
71
     * @var array
72
     */
73
    protected $hidden   = ['password', 'remember_token', 'confirmation_code'];
74
75
    /**
76
     * The attributes that should be casted to native types.
77
     *
78
     * @var array
79
     */
80
    protected $casts = [
81
        'is_admin'     => 'boolean',
82
        'is_active'    => 'boolean',
83
        'is_confirmed' => 'boolean',
84
    ];
85
86
    /**
87
     * The attributes that should be mutated to dates.
88
     *
89
     * @var array
90
     */
91
    protected $dates = ['confirmed_at', 'last_activity', 'deleted_at'];
92
93
    /* ------------------------------------------------------------------------------------------------
94
     |  Constructor
95
     | ------------------------------------------------------------------------------------------------
96
     */
97
    /**
98
     * Create a new Eloquent model instance.
99
     *
100
     * @param  array  $attributes
101
     */
102 675
    public function __construct(array $attributes = [])
103
    {
104 675
        $this->setTable(config('laravel-auth.users.table', 'users'));
105
106 675
        if (SocialAuthenticator::isEnabled()) {
107 675
            $this->hidden   = array_merge($this->hidden, ['social_provider_id']);
108 675
            $this->fillable = array_merge($this->fillable, ['social_provider', 'social_provider_id']);
109 300
        }
110
111 675
        parent::__construct($attributes);
112 675
    }
113
114
    /* ------------------------------------------------------------------------------------------------
115
     |  Scopes
116
     | ------------------------------------------------------------------------------------------------
117
     */
118
    /**
119
     * Scope unconfirmed users by code.
120
     *
121
     * @param  \Illuminate\Database\Eloquent\Builder  $query
122
     * @param  string                                 $code
123
     *
124
     * @return \Illuminate\Database\Eloquent\Builder
125
     */
126 36
    public function scopeUnconfirmed($query, $code)
127
    {
128 36
        return $query->where('is_confirmed', false)
129 36
            ->where('confirmation_code', $code)
130 36
            ->whereNull('confirmed_at');
131
    }
132
133
    /**
134
     * Scope last active users.
135
     *
136
     * @param  \Illuminate\Database\Eloquent\Builder  $query
137
     * @param  int|null                               $minutes
138
     *
139
     * @return \Illuminate\Database\Eloquent\Builder
140
     */
141 9
    public function scopeLastActive($query, $minutes = null)
142
    {
143 9
        $minutes = $minutes ?: config('laravel_auth.track-activity.minutes', 5);
144
145 9
        $date = \Carbon\Carbon::now()->subMinutes($minutes);
146
147 9
        return $query->where('last_activity', '>=', $date->toDateTimeString());
148
    }
149
150
    /* ------------------------------------------------------------------------------------------------
151
     |  Getters & Setters
152
     | ------------------------------------------------------------------------------------------------
153
     */
154
    /**
155
     * Set the `username` attribute.
156
     *
157
     * @param  string  $username
158
     */
159 207
    public function setUsernameAttribute($username)
160
    {
161 207
        $this->attributes['username'] = $this->slugify($username);
162 207
    }
163
164
    /**
165
     * Get the `full_name` attribute.
166
     *
167
     * @return string
168
     */
169 9
    public function getFullNameAttribute()
170
    {
171 9
        return $this->first_name . ' ' . $this->last_name;
172
    }
173
174
    /**
175
     * Set the `password` attribute.
176
     *
177
     * @param  string  $password
178
     */
179 207
    public function setPasswordAttribute($password)
180
    {
181 207
        $this->attributes['password'] = bcrypt($password);
182 207
    }
183
184
    /* ------------------------------------------------------------------------------------------------
185
     |  CRUD Functions
186
     | ------------------------------------------------------------------------------------------------
187
     */
188
    /**
189
     * Confirm the unconfirmed user account by confirmation code.
190
     *
191
     * @param  string  $code
192
     *
193
     * @return \Arcanesoft\Contracts\Auth\Models\User
194
     *
195
     * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException
196
     */
197 36
    public function findUnconfirmed($code)
198
    {
199 36
        $unconfirmedUser = self::unconfirmed($code)->first();
200
201 36
        if ( ! $unconfirmedUser instanceof self)
202 21
            throw (new UserConfirmationException)->setModel(self::class);
203
204 27
        return $unconfirmedUser;
205
    }
206
207
    /**
208
     * Confirm the new user account.
209
     *
210
     * @param  \Arcanesoft\Contracts\Auth\Models\User|string  $code
211
     *
212
     * @return \Arcanesoft\Contracts\Auth\Models\User
213
     */
214 18
    public function confirm($code)
215
    {
216 18
        if ($code instanceof self)
217 13
            $code = $code->confirmation_code;
218
219 18
        $user = $this->findUnconfirmed($code);
220
221 18
        return (new UserConfirmator)->confirm($user);
222
    }
223
224
    /**
225
     * Update the user's last activity.
226
     *
227
     * @param  bool  $save
228
     */
229 18
    public function updateLastActivity($save = true)
230
    {
231 18
        $this->forceFill(['last_activity' => \Carbon\Carbon::now()]);
232
233 18
        if ($save) $this->save();
234 18
    }
235
236
    /* ------------------------------------------------------------------------------------------------
237
     |  Check Functions
238
     | ------------------------------------------------------------------------------------------------
239
     */
240
    /**
241
     * Check if user is an administrator.
242
     *
243
     * @return bool
244
     */
245 54
    public function isAdmin()
246
    {
247 54
        return $this->is_admin;
248
    }
249
250
    /**
251
     * Check if user is a moderator.
252
     *
253
     * @return bool
254
     */
255 18
    public function isModerator()
256
    {
257
        // Override this method to give more privileges than members.
258 18
        return false;
259
    }
260
261
    /**
262
     * Check if user is a member.
263
     *
264
     * @return bool
265
     */
266 27
    public function isMember()
267
    {
268 27
        return ! $this->isAdmin();
269
    }
270
271
    /**
272
     * Check if user has a confirmed account.
273
     *
274
     * @return bool
275
     */
276 27
    public function isConfirmed()
277
    {
278 27
        return $this->is_confirmed;
279
    }
280
281
    /**
282
     * Check if user is on force deleting.
283
     *
284
     * @deprecated since Laravel v5.3.11
285
     * @see https://github.com/laravel/framework/pull/15580
286
     *
287
     * @return bool
288
     */
289 18
    public function isForceDeleting()
290
    {
291 18
        return $this->forceDeleting;
292
    }
293
294
    /**
295
     * Check if user can be impersonated.
296
     *
297
     * @return bool
298
     */
299 9
    public function canBeImpersonated()
300
    {
301 9
        return $this->isMember();
302
    }
303
304
    /* ------------------------------------------------------------------------------------------------
305
     |  Other Functions
306
     | ------------------------------------------------------------------------------------------------
307
     */
308
    /**
309
     * Slugify the value.
310
     *
311
     * @param  string  $value
312
     *
313
     * @return string
314
     */
315 207
    protected function slugify($value)
316
    {
317 207
        return Str::slug($value, config('laravel-auth.users.slug-separator', '.'));
318
    }
319
}
320