Completed
Pull Request — master (#23)
by ARCANEDEV
02:45
created

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