Completed
Push — master ( d42b43...5ac77a )
by ARCANEDEV
8s
created

src/Models/User.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException;
5
use Arcanedev\LaravelAuth\Services\UserConfirmator;
6
use Arcanedev\LaravelAuth\Traits\Activatable;
7
use Arcanedev\LaravelAuth\Traits\AuthUserTrait;
8
use Arcanesoft\Contracts\Auth\Models\User as UserContract;
9
use Illuminate\Auth\Authenticatable;
10
use Illuminate\Auth\Passwords\CanResetPassword;
11
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
12
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
13
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14
use Illuminate\Database\Eloquent\SoftDeletes;
15
use Illuminate\Foundation\Auth\Access\Authorizable;
16
use Illuminate\Support\Str;
17
18
/**
19
 * Class     User
20
 *
21
 * @package  Arcanedev\LaravelAuth\Models
22
 * @author   ARCANEDEV <[email protected]>
23
 *
24
 * @property  int                                       id
25
 * @property  string                                    username
26
 * @property  string                                    first_name
27
 * @property  string                                    last_name
28
 * @property  string                                    full_name
29
 * @property  string                                    email
30
 * @property  string                                    password
31
 * @property  string                                    remember_token
32
 * @property  bool                                      is_admin
33
 * @property  bool                                      is_active
34
 * @property  bool                                      is_confirmed       (Optional)
35
 * @property  string                                    confirmation_code  (Optional)
36
 * @property  \Carbon\Carbon                            confirmed_at       (Optional)
37
 * @property  \Carbon\Carbon                            created_at
38
 * @property  \Carbon\Carbon                            updated_at
39
 * @property  \Carbon\Carbon                            deleted_at
40
 * @property  \Illuminate\Database\Eloquent\Collection  roles
41
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
42
 *
43
 * @method  static  bool                                   insert(array $values)
44
 * @method          \Illuminate\Database\Eloquent\Builder  unconfirmed(string $code)
45
 */
46
class User
0 ignored issues
show
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...
47
    extends Model
48
    implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, UserContract
49
{
50
    /* ------------------------------------------------------------------------------------------------
51
     |  Traits
52
     | ------------------------------------------------------------------------------------------------
53
     */
54
    use Activatable, Authenticatable, Authorizable, AuthUserTrait, CanResetPassword, SoftDeletes;
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Properties
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * The attributes that are mass assignable.
62
     *
63
     * @var array
64
     */
65
    protected $fillable = [
66
        'username', 'first_name', 'last_name', 'email', 'password',
67
    ];
68
69
    /**
70
     * The attributes excluded from the model's JSON form.
71
     *
72
     * @var array
73
     */
74
    protected $hidden   = [
75
        'password', 'remember_token', 'confirmation_code',
76
    ];
77
78
    /**
79
     * The attributes that should be casted to native types.
80
     *
81
     * @var array
82
     */
83
    protected $casts = [
84
        'is_admin'     => 'boolean',
85
        'is_active'    => 'boolean',
86
        'is_confirmed' => 'boolean',
87
    ];
88
89
    /**
90
     * The attributes that should be mutated to dates.
91
     *
92
     * @var array
93
     */
94
    protected $dates = [
95
        'confirmed_at', 'deleted_at'
96
    ];
97
98
    /* ------------------------------------------------------------------------------------------------
99
     |  Constructor
100
     | ------------------------------------------------------------------------------------------------
101
     */
102
    /**
103
     * Create a new Eloquent model instance.
104
     *
105
     * @param  array  $attributes
106 528
     */
107
    public function __construct(array $attributes = [])
108 528
    {
109
        $this->setTable(config('laravel-auth.users.table', 'users'));
110 528
111 528
        parent::__construct($attributes);
112
    }
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 32
     */
126
    public function scopeUnconfirmed($query, $code)
127 32
    {
128 32
        return $query->where('is_confirmed', false)
129 32
            ->where('confirmation_code', $code)
130
            ->whereNull('confirmed_at');
131
    }
132
133
    /* ------------------------------------------------------------------------------------------------
134
     |  Getters & Setters
135
     | ------------------------------------------------------------------------------------------------
136
     */
137
    /**
138
     * Set the `username` attribute.
139
     *
140
     * @param  string  $username
141 8
     */
142
    public function setUsernameAttribute($username)
143 8
    {
144
        $username = Str::slug(
145
            trim($username),
146
            config('laravel-auth.slug-separator', '.')
147
        );
148
149
        $this->attributes['username'] = $username;
150
    }
151 160
152
    /**
153 160
     * Get the `full_name` attribute.
154 160
     *
155
     * @return string
156
     */
157
    public function getFullNameAttribute()
158
    {
159
        return $this->first_name . ' ' . $this->last_name;
160
    }
161
162
    /**
163
     * Set the `password` attribute.
164
     *
165
     * @param  string  $password
166
     */
167
    public function setPasswordAttribute($password)
168
    {
169 32
        $this->attributes['password'] = bcrypt($password);
170
    }
171 32
172
    /* ------------------------------------------------------------------------------------------------
173 32
     |  CRUD Functions
174 8
     | ------------------------------------------------------------------------------------------------
175
     */
176
    /**
177 24
     * Confirm the unconfirmed user account by confirmation code.
178
     *
179
     * @param  string  $code
180
     *
181
     * @return \Arcanesoft\Contracts\Auth\Models\User
182
     *
183
     * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException
184
     */
185
    public function findUnconfirmed($code)
186
    {
187 16
        $unconfirmedUser = self::unconfirmed($code)->first();
188
189 16
        if ( ! $unconfirmedUser instanceof self) {
190 8
            throw (new UserConfirmationException)->setModel(self::class);
191 6
        }
192
193 16
        return $unconfirmedUser;
194
    }
195 16
196
    /**
197
     * Confirm the new user account.
198
     *
199
     * @param  \Arcanesoft\Contracts\Auth\Models\User|string  $code
200
     *
201
     * @return \Arcanesoft\Contracts\Auth\Models\User
202
     */
203
    public function confirm($code)
204
    {
205
        if ($code instanceof self) {
206
            $code = $code->confirmation_code;
207 40
        }
208
209 40
        $user = $this->findUnconfirmed($code);
210
211
        return (new UserConfirmator)->confirm($user);
212
    }
213
214
    /* ------------------------------------------------------------------------------------------------
215
     |  Check Functions
216
     | ------------------------------------------------------------------------------------------------
217 24
     */
218
    /**
219 24
     * Check if user is an administrator.
220
     *
221
     * @return bool
222
     */
223
    public function isAdmin()
224
    {
225
        return $this->is_admin;
226
    }
227 16
228
    /**
229 16
     * Check if user is a moderator.
230
     *
231
     * @return bool
232
     */
233
    public function isModerator()
234
    {
235
        // Override this method to give more privileges than members.
236
        return false;
237
    }
238
239
    /**
240
     * Check if user is a member.
241
     *
242
     * @return bool
243
     */
244
    public function isMember()
245
    {
246
        return ! $this->isAdmin();
247
    }
248
249
    /**
250
     * Check if user has a confirmed account.
251
     *
252
     * @return bool
253
     */
254
    public function isConfirmed()
255
    {
256
        return $this->is_confirmed;
257
    }
258
259
    /**
260
     * Check if user is on force deleting.
261
     *
262
     * @return bool
263
     */
264
    public function isForceDeleting()
265
    {
266
        return $this->forceDeleting;
267
    }
268
}
269