firefly-iii/app/User.php

321 lines
7.0 KiB
PHP
Raw Normal View History

<?php
/**
* User.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
declare(strict_types=1);
2015-02-05 21:39:52 -06:00
namespace FireflyIII;
2015-02-05 21:39:52 -06:00
2016-11-22 14:21:11 -06:00
use FireflyIII\Events\RequestedNewPassword;
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;
use Illuminate\Foundation\Auth\User as Authenticatable;
2016-09-15 23:19:40 -05:00
use Illuminate\Notifications\Notifiable;
2017-12-18 22:20:05 -06:00
use Log;
2016-11-22 14:21:11 -06:00
use Request;
2015-02-05 22:04:06 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class User.
*/
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
/**
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
{
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'];
}
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
{
return $this->hasMany('FireflyIII\Models\Attachment');
}
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
{
return $this->hasMany('FireflyIII\Models\AvailableBudget');
}
/**
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
*/
2016-02-06 03:11:06 -06:00
public function bills(): HasMany
{
2016-01-15 16:12:52 -06:00
return $this->hasMany('FireflyIII\Models\Bill');
}
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
{
2016-01-15 16:12:52 -06:00
return $this->hasMany('FireflyIII\Models\Budget');
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
{
2016-01-15 16:12:52 -06:00
return $this->hasMany('FireflyIII\Models\Category');
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 currency exchange rates
*
* @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
*/
public function exportJobs(): HasMany
2016-02-04 10:14:59 -06:00
{
return $this->hasMany('FireflyIII\Models\ExportJob');
}
/**
2017-12-27 10:22:44 -06:00
* @codeCoverageIgnore
2017-11-25 13:27:53 -06:00
* Generates access token.
*
* @return string
*/
public function generateAccessToken(): string
{
$bytes = random_bytes(16);
return strval(bin2hex($bytes));
}
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
*
* @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
*/
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
/**
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
{
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
/**
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
{
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
/**
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
{
return $this->belongsToMany('FireflyIII\Models\Role');
}
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
{
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
/**
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
{
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
/**
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)
{
$ipAddress = Request::ip();
2016-12-14 11:59:12 -06: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
{
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
/**
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
*/
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
/**
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
{
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
}
2015-02-05 21:39:52 -06:00
}