2016-01-08 09:02:15 -06:00
|
|
|
<?php
|
2018-03-19 06:08:50 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* User.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2015-02-05 21:39:52 -06:00
|
|
|
|
2016-01-08 09:02:15 -06:00
|
|
|
namespace FireflyIII;
|
2015-02-05 21:39:52 -06:00
|
|
|
|
2016-11-22 14:21:11 -06:00
|
|
|
use FireflyIII\Events\RequestedNewPassword;
|
2017-04-13 13:47:59 -05:00
|
|
|
use FireflyIII\Models\CurrencyExchangeRate;
|
2016-02-11 22:49:53 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2016-02-06 03:11:06 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
2017-12-18 22:20:05 -06:00
|
|
|
use Illuminate\Database\QueryException;
|
2016-01-08 09:02:15 -06:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2016-09-15 23:19:40 -05:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2018-02-04 01:14:03 -06:00
|
|
|
use Laravel\Passport\HasApiTokens;
|
2017-12-18 22:20:05 -06:00
|
|
|
use Log;
|
2016-11-22 14:21:11 -06:00
|
|
|
use Request;
|
2018-01-10 00:51:47 -06:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-04-27 23:23:13 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\Tag;
|
|
|
|
use FireflyIII\Models\Rule;
|
|
|
|
use FireflyIII\Models\RuleGroup;
|
|
|
|
use FireflyIII\Models\Role;
|
|
|
|
use FireflyIII\Models\Preference;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Models\ExportJob;
|
|
|
|
use FireflyIII\Models\Category;
|
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\Bill;
|
|
|
|
use FireflyIII\Models\AvailableBudget;
|
|
|
|
use FireflyIII\Models\Attachment;
|
2015-02-05 22:04:06 -06:00
|
|
|
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class User.
|
2016-01-09 01:20:55 -06:00
|
|
|
*/
|
2016-01-08 09:02:15 -06:00
|
|
|
class User extends Authenticatable
|
|
|
|
{
|
2018-02-04 01:14:03 -06:00
|
|
|
use Notifiable, HasApiTokens;
|
2016-01-09 01:51:49 -06:00
|
|
|
|
2018-03-03 01:12:18 -06:00
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts
|
|
|
|
= [
|
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
'blocked' => 'boolean',
|
|
|
|
];
|
2015-02-05 22:04:06 -06:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-07-26 08:51:07 -05:00
|
|
|
protected $fillable = ['email', 'password', 'blocked', 'blocked_code'];
|
2015-02-05 22:04:06 -06:00
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['password', 'remember_token'];
|
2015-02-05 22:14:27 -06:00
|
|
|
/**
|
|
|
|
* The database table used by the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'users';
|
2015-02-05 22:04:06 -06:00
|
|
|
|
2018-01-10 00:51:47 -06:00
|
|
|
/**
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return User
|
2018-04-02 07:17:11 -05:00
|
|
|
* @throws NotFoundHttpException
|
2018-01-10 00:51:47 -06:00
|
|
|
*/
|
2018-02-10 01:21:20 -06:00
|
|
|
public static function routeBinder(string $value): User
|
2018-01-10 00:51:47 -06:00
|
|
|
{
|
2018-02-10 01:21:20 -06:00
|
|
|
if (auth()->check()) {
|
2018-04-02 07:17:11 -05:00
|
|
|
$userId = (int)$value;
|
2018-01-10 00:51:47 -06:00
|
|
|
$user = self::find($userId);
|
2018-04-02 07:17:11 -05:00
|
|
|
if (null !== $user) {
|
2018-01-10 00:51:47 -06:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to accounts.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function accounts(): HasMany
|
2015-02-05 22:04:06 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Account::class);
|
2015-02-05 22:04:06 -06:00
|
|
|
}
|
2015-02-05 22:01:24 -06:00
|
|
|
|
2016-02-11 22:49:53 -06:00
|
|
|
/**
|
|
|
|
* Alias to eloquent many-to-many relation's attach() method.
|
|
|
|
*
|
|
|
|
* Full credit goes to: https://github.com/Zizaco/entrust
|
|
|
|
*
|
|
|
|
* @param mixed $role
|
|
|
|
*/
|
|
|
|
public function attachRole($role)
|
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\is_object($role)) {
|
2016-02-11 22:49:53 -06:00
|
|
|
$role = $role->getKey();
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\is_array($role)) {
|
2016-02-11 22:49:53 -06:00
|
|
|
$role = $role['id'];
|
|
|
|
}
|
2017-12-18 22:20:05 -06:00
|
|
|
try {
|
|
|
|
$this->roles()->attach($role);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
// don't care
|
|
|
|
Log::info(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
|
|
|
|
}
|
2016-02-11 22:49:53 -06:00
|
|
|
}
|
|
|
|
|
2015-07-18 02:49:19 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to attachments
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-07-18 02:49:19 -05:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function attachments(): HasMany
|
2015-07-18 02:49:19 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Attachment::class);
|
2015-07-18 02:49:19 -05:00
|
|
|
}
|
|
|
|
|
2016-12-30 06:47:23 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to available budgets
|
|
|
|
*
|
2016-12-30 06:47:23 -06:00
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function availableBudgets(): HasMany
|
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(AvailableBudget::class);
|
2016-12-30 06:47:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-28 01:58:01 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to bills.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-04-28 01:58:01 -05:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function bills(): HasMany
|
2015-04-28 01:58:01 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Bill::class);
|
2015-04-28 01:58:01 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 14:37:48 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to budgets.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2016-01-12 14:37:48 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function budgets(): HasMany
|
2016-01-12 14:37:48 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Budget::class);
|
2016-01-12 14:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to categories
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2016-01-12 14:37:48 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function categories(): HasMany
|
2016-01-12 14:37:48 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Category::class);
|
2016-01-12 14:37:48 -06:00
|
|
|
}
|
|
|
|
|
2017-04-13 13:47:59 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to currency exchange rates
|
|
|
|
*
|
2017-04-13 13:47:59 -05:00
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function currencyExchangeRates(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(CurrencyExchangeRate::class);
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:14:59 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to export jobs
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2016-02-04 10:14:59 -06:00
|
|
|
*/
|
2016-08-26 02:30:52 -05:00
|
|
|
public function exportJobs(): HasMany
|
2016-02-04 10:14:59 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(ExportJob::class);
|
2016-02-04 10:14:59 -06:00
|
|
|
}
|
|
|
|
|
2017-09-14 11:27:22 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Generates access token.
|
|
|
|
*
|
2017-09-14 11:27:22 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateAccessToken(): string
|
|
|
|
{
|
|
|
|
$bytes = random_bytes(16);
|
|
|
|
|
2018-04-02 07:17:11 -05:00
|
|
|
return (string)bin2hex($bytes);
|
2017-09-14 11:27:22 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 22:49:53 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2016-02-11 22:49:53 -06:00
|
|
|
* Checks if the user has a role by its name.
|
|
|
|
*
|
|
|
|
* Full credit goes to: https://github.com/Zizaco/entrust
|
|
|
|
*
|
2016-02-17 15:02:32 -06:00
|
|
|
* @param string $name
|
2016-02-11 22:49:53 -06:00
|
|
|
*
|
2018-03-30 15:40:20 -05:00
|
|
|
* @deprecated
|
2016-02-11 22:49:53 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-17 15:02:32 -06:00
|
|
|
public function hasRole(string $name): bool
|
2016-02-11 22:49:53 -06:00
|
|
|
{
|
2016-02-17 15:02:32 -06:00
|
|
|
foreach ($this->roles as $role) {
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($role->name === $name) {
|
2016-02-17 15:02:32 -06:00
|
|
|
return true;
|
2016-02-11 22:49:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-26 01:21:31 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to import jobs.
|
|
|
|
*
|
2016-08-26 01:21:31 -05:00
|
|
|
* @return HasMany
|
|
|
|
*/
|
2016-08-26 02:30:52 -05:00
|
|
|
public function importJobs(): HasMany
|
2016-08-26 01:21:31 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(ImportJob::class);
|
2016-08-26 01:21:31 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to piggy banks.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasManyThrough
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function piggyBanks(): HasManyThrough
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasManyThrough(PiggyBank::class, Account::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to preferences.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function preferences(): HasMany
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Preference::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2016-02-11 22:49:53 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to roles.
|
|
|
|
*
|
2016-02-11 22:49:53 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function roles(): BelongsToMany
|
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->belongsToMany(Role::class);
|
2016-02-11 22:49:53 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to rule groups.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function ruleGroups(): HasMany
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(RuleGroup::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to rules.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function rules(): HasMany
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Rule::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:59:12 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2016-12-14 11:59:12 -06:00
|
|
|
* Send the password reset notification.
|
|
|
|
*
|
2017-11-15 05:25:49 -06:00
|
|
|
* @param string $token
|
2016-12-14 11:59:12 -06:00
|
|
|
*/
|
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
{
|
2017-06-07 04:13:04 -05:00
|
|
|
$ipAddress = Request::ip();
|
2016-12-14 11:59:12 -06:00
|
|
|
|
2017-06-07 04:13:04 -05:00
|
|
|
event(new RequestedNewPassword($this, $token, $ipAddress));
|
2016-12-14 11:59:12 -06:00
|
|
|
}
|
|
|
|
|
2015-03-29 11:28:49 -05:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to tags.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-03-29 11:28:49 -05:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function tags(): HasMany
|
2015-03-29 11:28:49 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Tag::class);
|
2015-03-29 11:28:49 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to transaction journals.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasMany
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-08-26 02:30:52 -05:00
|
|
|
public function transactionJournals(): HasMany
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(TransactionJournal::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-12-27 10:22:44 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 13:27:53 -06:00
|
|
|
* Link to transactions.
|
|
|
|
*
|
2016-02-06 03:11:06 -06:00
|
|
|
* @return HasManyThrough
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
public function transactions(): HasManyThrough
|
2015-02-05 22:14:27 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasManyThrough(Transaction::class, TransactionJournal::class);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
2015-02-05 21:39:52 -06:00
|
|
|
}
|