2016-01-08 09:02:15 -06:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* User.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-05 21:39:52 -06:00
|
|
|
|
2016-09-15 23:19:40 -05: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;
|
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;
|
2016-11-22 14:21:11 -06:00
|
|
|
use Request;
|
2015-02-05 22:04:06 -06:00
|
|
|
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
|
|
|
* Class User
|
|
|
|
*
|
|
|
|
* @package FireflyIII
|
|
|
|
*/
|
2016-01-08 09:02:15 -06:00
|
|
|
class User extends Authenticatable
|
|
|
|
{
|
2016-09-15 23:19:40 -05:00
|
|
|
use Notifiable;
|
2016-01-09 01:51:49 -06:00
|
|
|
|
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'];
|
2016-09-15 23:19:40 -05:00
|
|
|
|
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
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2015-02-05 22:35:00 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Account');
|
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)
|
|
|
|
{
|
|
|
|
if (is_object($role)) {
|
|
|
|
$role = $role->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($role)) {
|
|
|
|
$role = $role['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->roles()->attach($role);
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:49:19 -05:00
|
|
|
/**
|
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
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\Attachment');
|
|
|
|
}
|
|
|
|
|
2016-12-30 06:47:23 -06:00
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function availableBudgets(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\AvailableBudget');
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:58:01 -05:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Bill');
|
2015-04-28 01:58:01 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 14:37:48 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Budget');
|
2016-01-12 14:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Category');
|
2016-01-12 14:37:48 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
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
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\ExportJob');
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:27:22 -05:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateAccessToken(): string
|
|
|
|
{
|
|
|
|
$bytes = random_bytes(16);
|
|
|
|
|
|
|
|
return strval(bin2hex($bytes));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
2016-08-26 02:30:52 -05:00
|
|
|
public function importJobs(): HasMany
|
2016-08-26 01:21:31 -05:00
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\ImportJob');
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasManyThrough('FireflyIII\Models\PiggyBank', 'FireflyIII\Models\Account');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Preference');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2016-02-11 22:49:53 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function roles(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany('FireflyIII\Models\Role');
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\RuleGroup');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Rule');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:59:12 -06:00
|
|
|
/**
|
|
|
|
* Send the password reset notification.
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\Tag');
|
2015-03-29 11:28:49 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasMany('FireflyIII\Models\TransactionJournal');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
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
|
|
|
{
|
2016-01-15 16:12:52 -06:00
|
|
|
return $this->hasManyThrough('FireflyIII\Models\Transaction', 'FireflyIII\Models\TransactionJournal');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2016-09-15 23:19:40 -05:00
|
|
|
|
2015-02-05 21:39:52 -06:00
|
|
|
}
|