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

User::isConfirmed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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