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