1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Models/User.php

421 lines
10 KiB
PHP
Raw Normal View History

2018-10-04 19:10:43 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
2018-10-04 19:10:43 +02:00
namespace App\Models;
2018-10-04 19:10:43 +02:00
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Admin\ResetPasswordObject;
2020-10-28 11:10:49 +01:00
use App\Models\Presenters\UserPresenter;
2020-10-27 16:04:28 +01:00
use App\Notifications\ResetPasswordNotification;
2021-03-03 23:39:24 +01:00
use App\Services\User\UserService;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes;
use App\Utils\Traits\UserSettings;
use Illuminate\Contracts\Auth\MustVerifyEmail;
2020-10-01 12:49:47 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-10-28 11:10:49 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2019-10-04 13:54:03 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Laracasts\Presenter\PresentableTrait;
2018-10-04 19:10:43 +02:00
class User extends Authenticatable implements MustVerifyEmail
2018-10-04 19:10:43 +02:00
{
use Notifiable;
use SoftDeletes;
use PresentableTrait;
use MakesHash;
use UserSessionAttributes;
use UserSettings;
2019-04-25 13:33:03 +02:00
use Filterable;
2020-10-01 12:49:47 +02:00
use HasFactory;
2020-10-28 11:10:49 +01:00
protected $guard = 'user';
protected $dates = ['deleted_at'];
2020-10-28 11:10:49 +01:00
protected $presenter = UserPresenter::class;
protected $with = []; // ? companies also
protected $dateFormat = 'Y-m-d H:i:s.u';
public $company;
2019-08-02 02:31:48 +02:00
protected $appends = [
'hashed_id',
2019-08-02 02:31:48 +02:00
];
2018-10-04 19:10:43 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'phone',
'signature',
'avatar',
'accepted_terms_version',
'oauth_user_id',
'oauth_provider_id',
'oauth_user_token',
2020-05-13 11:02:38 +02:00
'oauth_user_refresh_token',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'is_deleted',
2021-02-25 11:41:18 +01:00
'google_2fa_secret',
2018-10-04 19:10:43 +02:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'remember_token',
'google_2fa_secret',
'google_2fa_phone',
'remember_2fa_token',
'slack_webhook_url',
2018-10-04 19:10:43 +02:00
];
protected $casts = [
'oauth_user_token' => 'object',
'settings' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
2021-02-22 10:54:46 +01:00
public function name()
{
return $this->first_name . ' ' . $this->last_name;
}
public function getEntityType()
{
return self::class;
}
public function getHashedIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2019-03-28 11:07:45 +01:00
/**
* Returns a account.
*
2020-10-28 11:10:49 +01:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-03-28 11:07:45 +01:00
*/
2019-03-28 03:36:36 +01:00
public function account()
{
return $this->belongsTo(Account::class);
}
2019-04-18 08:11:37 +02:00
/**
* Returns all company tokens.
*
2020-10-28 11:10:49 +01:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2019-04-18 08:11:37 +02:00
*/
public function tokens()
{
return $this->hasMany(CompanyToken::class)->orderBy('id', 'ASC');
}
2019-03-27 05:50:13 +01:00
/**
* Returns all companies a user has access to.
*
2020-10-28 11:10:49 +01:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function companies()
{
return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked')->withTimestamps();
}
/**
* As we are authenticating on CompanyToken,
* we need to link the company to the user manually. This allows
* us to decouple a $user and their attached companies.
2020-10-28 11:10:49 +01:00
* @param $company
*/
public function setCompany($company)
{
// config(['ninja.company_id' => $company->id]);
$this->company = $company;
}
/**
* Returns the currently set Company.
*/
public function getCompany()
{
2020-11-25 15:19:52 +01:00
if (request()->header('X-API-TOKEN')) {
$company_token = CompanyToken::with(['company'])->whereRaw('BINARY `token`= ?', [request()->header('X-API-TOKEN')])->first();
return $company_token->company;
}
elseif ($this->company){
return $this->company;
}
2021-05-18 04:13:00 +02:00
// return false;
throw new \Exception('No Company Found');
//return Company::find(config('ninja.company_id'));
}
2021-05-18 04:13:00 +02:00
public function companyIsSet()
{
if($this->company)
return true;
return false;
}
2019-03-28 11:07:45 +01:00
/**
* Returns the current company.
*
2019-03-28 11:07:45 +01:00
* @return Collection
*/
2019-03-28 10:05:13 +01:00
public function company()
{
return $this->getCompany();
}
private function setCompanyByGuard()
{
if (Auth::guard('contact')->check()) {
$this->setCompany(auth()->user()->client->company);
}
}
public function company_users()
{
return $this->hasMany(CompanyUser::class)->withTrashed();
}
2020-12-26 09:03:24 +01:00
public function co_user()
{
return $this->company_user();
}
public function company_user()
{
if (! $this->id && auth()->user()) {
$this->id = auth()->user()->id;
}
return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'user_id', 'id', 'user_id')
->withTrashed();
// if (request()->header('X-API-TOKEN')) {
// nlog("with an API token");
// nlog(request()->header('X-API-TOKEN'));
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
// ->where('company_tokens.token', request()->header('X-API-TOKEN'))
// ->withTrashed();
// } else {
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
// ->where('company_user.user_id', $this->id)
// ->withTrashed();
// }
}
/**
* Returns the currently set company id for the user.
*
* @return int
*/
public function companyId() :int
{
2019-03-28 10:05:13 +01:00
return $this->company()->id;
}
public function clients()
{
return $this->hasMany(Client::class);
}
/**
* Returns a comma separated list of user permissions.
*
* @return comma separated list
*/
public function permissions()
{
return $this->company_user->permissions;
}
/**
* Returns a object of User Settings.
*
* @return stdClass
*/
public function settings()
{
return json_decode($this->company_user->settings);
}
/**
* Returns a boolean of the administrator status of the user.
*
* @return bool
*/
public function isAdmin() : bool
{
return $this->company_user->is_admin;
}
public function isOwner() : bool
{
return $this->company_user->is_owner;
}
/**
* Returns all user created contacts.
*
2020-10-28 11:10:49 +01:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function contacts()
{
2019-07-08 02:08:57 +02:00
return $this->hasMany(ClientContact::class);
}
/**
* Returns a boolean value if the user owns the current Entity.
*
* @param string Entity
* @return bool
*/
public function owns($entity) : bool
{
return ! empty($entity->user_id) && $entity->user_id == $this->id;
}
/**
* Returns a boolean value if the user is assigned to the current Entity.
*
* @param string Entity
* @return bool
*/
public function assigned($entity) : bool
{
return ! empty($entity->assigned_user_id) && $entity->assigned_user_id == $this->id;
}
/**
* Returns true if permissions exist in the map.
*
* @param string permission
* @return bool
*/
public function hasPermission($permission) : bool
{
$parts = explode('_', $permission);
2020-04-01 14:34:50 +02:00
$all_permission = '';
if (count($parts) > 1) {
$all_permission = $parts[0].'_all';
}
2020-04-01 14:34:50 +02:00
return $this->isOwner() ||
$this->isAdmin() ||
(stripos($this->company_user->permissions, $all_permission) !== false) ||
(stripos($this->company_user->permissions, $permission) !== false);
}
2019-04-28 07:31:32 +02:00
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
2019-10-04 13:54:03 +02:00
2021-02-25 22:06:43 +01:00
public function isVerified()
{
return is_null($this->email_verified_at) ? false : true;
}
2019-10-04 13:54:03 +02:00
public function getEmailVerifiedAt()
{
if ($this->email_verified_at) {
2019-10-05 02:11:04 +02:00
return Carbon::parse($this->email_verified_at)->timestamp;
} else {
2019-10-04 13:54:03 +02:00
return null;
}
2019-10-04 13:54:03 +02:00
}
public function routeNotificationForSlack($notification)
{
if ($this->company_user->slack_webhook_url) {
return $this->company_user->slack_webhook_url;
}
}
public function routeNotificationForMail($notification)
{
return $this->email;
}
/**
* Retrieve the model for a bound value.
*
2020-10-28 11:10:49 +01:00
* @param mixed $value
* @param null $field
* @return Model|null
*/
2020-11-25 15:19:52 +01:00
public function resolveRouteBinding($value, $field = null)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
2020-10-27 16:04:28 +01:00
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer( (new ResetPasswordObject($token, $this, $this->account->default_company))->build());
$nmo->to_user = $this;
$nmo->settings = $this->account->default_company->settings;
$nmo->company = $this->account->default_company;
2021-05-22 06:45:09 +02:00
NinjaMailerJob::dispatch($nmo, true);
//$this->notify(new ResetPasswordNotification($token));
2020-10-27 16:04:28 +01:00
}
2021-03-03 23:39:24 +01:00
2021-03-04 00:12:34 +01:00
public function service()
2021-03-03 23:39:24 +01:00
{
return new UserService($this);
}
2018-10-04 19:10:43 +02:00
}