1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
45 | class User |
||
|
|||
46 | extends Model |
||
47 | implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, UserContract |
||
48 | { |
||
49 | /* ------------------------------------------------------------------------------------------------ |
||
50 | | Traits |
||
51 | | ------------------------------------------------------------------------------------------------ |
||
52 | */ |
||
53 | use Activatable, Authenticatable, Authorizable, AuthUserTrait, CanResetPassword, 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', 'first_name', 'last_name', 'email', 'password', |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * The attributes excluded from the model's JSON form. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $hidden = [ |
||
74 | 'password', 'remember_token', 'confirmation_code', |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * The attributes that should be casted to native types. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $casts = [ |
||
83 | 'is_admin' => 'boolean', |
||
84 | 'is_active' => 'boolean', |
||
85 | 'is_confirmed' => 'boolean', |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * The attributes that should be mutated to dates. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $dates = [ |
||
94 | 'confirmed_at', 'deleted_at' |
||
95 | ]; |
||
96 | |||
97 | /* ------------------------------------------------------------------------------------------------ |
||
98 | | Constructor |
||
99 | | ------------------------------------------------------------------------------------------------ |
||
100 | */ |
||
101 | /** |
||
102 | * Create a new Eloquent model instance. |
||
103 | * |
||
104 | * @param array $attributes |
||
105 | */ |
||
106 | 528 | public function __construct(array $attributes = []) |
|
107 | { |
||
108 | 528 | $this->setTable(config('laravel-auth.users.table', 'users')); |
|
109 | |||
110 | 528 | parent::__construct($attributes); |
|
111 | 528 | } |
|
112 | |||
113 | /* ------------------------------------------------------------------------------------------------ |
||
114 | | Scopes |
||
115 | | ------------------------------------------------------------------------------------------------ |
||
116 | */ |
||
117 | /** |
||
118 | * Scope unconfirmed users by code. |
||
119 | * |
||
120 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
121 | * @param string $code |
||
122 | * |
||
123 | * @return \Illuminate\Database\Eloquent\Builder |
||
124 | */ |
||
125 | 32 | public function scopeUnconfirmed($query, $code) |
|
126 | { |
||
127 | 32 | return $query->where('is_confirmed', false) |
|
128 | 32 | ->where('confirmation_code', $code) |
|
129 | 32 | ->whereNull('confirmed_at'); |
|
130 | } |
||
131 | |||
132 | /* ------------------------------------------------------------------------------------------------ |
||
133 | | Getters & Setters |
||
134 | | ------------------------------------------------------------------------------------------------ |
||
135 | */ |
||
136 | /** |
||
137 | * Get the full name attribute. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 8 | public function getFullNameAttribute() |
|
142 | { |
||
143 | 8 | return $this->first_name . ' ' . $this->last_name; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Set the password attribute. |
||
148 | * |
||
149 | * @param string $password |
||
150 | */ |
||
151 | 160 | public function setPasswordAttribute($password) |
|
152 | { |
||
153 | 160 | $this->attributes['password'] = bcrypt($password); |
|
154 | 160 | } |
|
155 | |||
156 | /* ------------------------------------------------------------------------------------------------ |
||
157 | | CRUD Functions |
||
158 | | ------------------------------------------------------------------------------------------------ |
||
159 | */ |
||
160 | /** |
||
161 | * Confirm the unconfirmed user account by confirmation code. |
||
162 | * |
||
163 | * @param string $code |
||
164 | * |
||
165 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
166 | * |
||
167 | * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
||
168 | */ |
||
169 | 32 | public function findUnconfirmed($code) |
|
170 | { |
||
171 | 32 | $unconfirmedUser = self::unconfirmed($code)->first(); |
|
172 | |||
173 | 32 | if ( ! $unconfirmedUser instanceof self) { |
|
174 | 8 | throw (new UserConfirmationException)->setModel(self::class); |
|
175 | } |
||
176 | |||
177 | 24 | return $unconfirmedUser; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Confirm the new user account. |
||
182 | * |
||
183 | * @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
||
184 | * |
||
185 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
186 | */ |
||
187 | 16 | public function confirm($code) |
|
188 | { |
||
189 | 16 | if ($code instanceof self) { |
|
190 | 8 | $code = $code->confirmation_code; |
|
191 | 6 | } |
|
192 | |||
193 | 16 | $user = $this->findUnconfirmed($code); |
|
194 | |||
195 | 16 | return (new UserConfirmator)->confirm($user); |
|
196 | } |
||
197 | |||
198 | /* ------------------------------------------------------------------------------------------------ |
||
199 | | Check Functions |
||
200 | | ------------------------------------------------------------------------------------------------ |
||
201 | */ |
||
202 | /** |
||
203 | * Check if user is an administrator. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 40 | public function isAdmin() |
|
211 | |||
212 | /** |
||
213 | * Check if user has a confirmed account. |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 24 | public function isConfirmed() |
|
221 | |||
222 | /** |
||
223 | * Check if user is on force deleting. |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 16 | public function isForceDeleting() |
|
231 | } |
||
232 |