Files
firefly-iii/app/User.php

341 lines
7.6 KiB
PHP
Raw Normal View History

<?php
/**
* User.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-06 04:39:52 +01:00
namespace FireflyIII;
2015-02-06 04:39:52 +01:00
2016-11-22 21:21:11 +01:00
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Models\CurrencyExchangeRate;
2016-02-12 05:49:53 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2016-02-06 10:11:06 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
2017-12-19 05:20:05 +01:00
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Auth\User as Authenticatable;
2016-09-16 06:19:40 +02:00
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
2017-12-19 05:20:05 +01:00
use Log;
2016-11-22 21:21:11 +01:00
use Request;
2018-01-10 07:51:47 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2015-02-06 05:04:06 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class User.
*/
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
2016-01-09 08:51:49 +01:00
2015-02-06 05:04:06 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2015-07-26 15:51:07 +02:00
protected $fillable = ['email', 'password', 'blocked', 'blocked_code'];
2016-09-16 06:19:40 +02:00
2015-02-06 05:04:06 +01:00
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
2015-02-06 05:14:27 +01:00
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
2015-02-06 05:04:06 +01:00
2018-01-10 07:51:47 +01:00
/**
2018-02-07 11:15:36 +01:00
* @param $guard
2018-01-10 07:51:47 +01:00
* @param string $value
*
* @return User
*/
2018-02-07 11:15:36 +01:00
public static function routeBinder($guard, string $value): User
2018-01-10 07:51:47 +01:00
{
2018-02-07 11:15:36 +01:00
if ($guard->check()) {
2018-01-10 07:51:47 +01:00
$userId = intval($value);
$user = self::find($userId);
if (!is_null($user)) {
return $user;
}
}
throw new NotFoundHttpException;
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to accounts.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function accounts(): HasMany
2015-02-06 05:04:06 +01:00
{
2015-02-06 05:35:00 +01:00
return $this->hasMany('FireflyIII\Models\Account');
2015-02-06 05:04:06 +01:00
}
2015-02-06 05:01:24 +01:00
2016-02-12 05:49:53 +01: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-19 05:20:05 +01: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-12 05:49:53 +01:00
}
2015-07-18 09:49:19 +02:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to attachments
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-07-18 09:49:19 +02:00
*/
2016-02-06 10:11:06 +01:00
public function attachments(): HasMany
2015-07-18 09:49:19 +02:00
{
return $this->hasMany('FireflyIII\Models\Attachment');
}
2016-12-30 13:47:23 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to available budgets
*
2016-12-30 13:47:23 +01:00
* @return HasMany
*/
public function availableBudgets(): HasMany
{
return $this->hasMany('FireflyIII\Models\AvailableBudget');
}
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to bills.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
*/
2016-02-06 10:11:06 +01:00
public function bills(): HasMany
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Bill');
}
2016-01-12 21:37:48 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to budgets.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2016-01-12 21:37:48 +01:00
*/
2016-02-06 10:11:06 +01:00
public function budgets(): HasMany
2016-01-12 21:37:48 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Budget');
2016-01-12 21:37:48 +01:00
}
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to categories
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2016-01-12 21:37:48 +01:00
*/
2016-02-06 10:11:06 +01:00
public function categories(): HasMany
2016-01-12 21:37:48 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Category');
2016-01-12 21:37:48 +01:00
}
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to currency exchange rates
*
* @return HasMany
*/
public function currencyExchangeRates(): HasMany
{
return $this->hasMany(CurrencyExchangeRate::class);
}
2016-02-04 17:14:59 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to export jobs
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2016-02-04 17:14:59 +01:00
*/
public function exportJobs(): HasMany
2016-02-04 17:14:59 +01:00
{
return $this->hasMany('FireflyIII\Models\ExportJob');
}
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Generates access token.
*
* @return string
*/
public function generateAccessToken(): string
{
$bytes = random_bytes(16);
return strval(bin2hex($bytes));
}
2016-02-12 05:49:53 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2016-02-12 05:49:53 +01:00
* Checks if the user has a role by its name.
*
* Full credit goes to: https://github.com/Zizaco/entrust
*
2016-02-17 22:02:32 +01:00
* @param string $name
2016-02-12 05:49:53 +01:00
*
* @return bool
*/
2016-02-17 22:02:32 +01:00
public function hasRole(string $name): bool
2016-02-12 05:49:53 +01:00
{
2016-02-17 22:02:32 +01:00
foreach ($this->roles as $role) {
2017-07-15 16:41:07 +02:00
if ($role->name === $name) {
2016-02-17 22:02:32 +01:00
return true;
2016-02-12 05:49:53 +01:00
}
}
return false;
}
2016-08-26 08:21:31 +02:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to import jobs.
*
2016-08-26 08:21:31 +02:00
* @return HasMany
*/
public function importJobs(): HasMany
2016-08-26 08:21:31 +02:00
{
return $this->hasMany('FireflyIII\Models\ImportJob');
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to piggy banks.
*
2016-02-06 10:11:06 +01:00
* @return HasManyThrough
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function piggyBanks(): HasManyThrough
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasManyThrough('FireflyIII\Models\PiggyBank', 'FireflyIII\Models\Account');
2015-02-06 05:14:27 +01:00
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to preferences.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function preferences(): HasMany
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Preference');
2015-02-06 05:14:27 +01:00
}
2016-02-12 05:49:53 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to roles.
*
2016-02-12 05:49:53 +01:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany('FireflyIII\Models\Role');
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to rule groups.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function ruleGroups(): HasMany
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\RuleGroup');
2015-02-06 05:14:27 +01:00
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to rules.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function rules(): HasMany
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Rule');
2015-02-06 05:14:27 +01:00
}
2016-12-14 18:59:12 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2016-12-14 18:59:12 +01:00
* Send the password reset notification.
*
2017-11-15 12:25:49 +01:00
* @param string $token
2016-12-14 18:59:12 +01:00
*/
public function sendPasswordResetNotification($token)
{
$ipAddress = Request::ip();
2016-12-14 18:59:12 +01:00
event(new RequestedNewPassword($this, $token, $ipAddress));
2016-12-14 18:59:12 +01:00
}
2015-03-29 18:28:49 +02:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to tags.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-03-29 18:28:49 +02:00
*/
2016-02-06 10:11:06 +01:00
public function tags(): HasMany
2015-03-29 18:28:49 +02:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\Tag');
2015-03-29 18:28:49 +02:00
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to transaction journals.
*
2016-02-06 10:11:06 +01:00
* @return HasMany
2015-02-11 07:35:10 +01:00
*/
public function transactionJournals(): HasMany
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasMany('FireflyIII\Models\TransactionJournal');
2015-02-06 05:14:27 +01:00
}
2015-02-11 07:35:10 +01:00
/**
2017-12-27 17:22:44 +01:00
* @codeCoverageIgnore
2017-11-25 20:27:53 +01:00
* Link to transactions.
*
2016-02-06 10:11:06 +01:00
* @return HasManyThrough
2015-02-11 07:35:10 +01:00
*/
2016-02-06 10:11:06 +01:00
public function transactions(): HasManyThrough
2015-02-06 05:14:27 +01:00
{
2016-01-15 23:12:52 +01:00
return $this->hasManyThrough('FireflyIII\Models\Transaction', 'FireflyIII\Models\TransactionJournal');
2015-02-06 05:14:27 +01:00
}
2015-02-06 04:39:52 +01:00
}