2018-10-04 19:10:43 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. 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
|
|
|
|
2018-10-12 13:29:34 +02:00
|
|
|
namespace App\Models;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2019-03-28 06:03:18 +01:00
|
|
|
use App\Models\Company;
|
2019-03-27 10:38:28 +01:00
|
|
|
use App\Models\CompanyToken;
|
2019-01-22 10:47:26 +01:00
|
|
|
use App\Models\CompanyUser;
|
2019-04-25 13:33:03 +02:00
|
|
|
use App\Models\Filterable;
|
2019-11-04 01:22:59 +01:00
|
|
|
use App\Models\Language;
|
2018-10-21 00:26:21 +02:00
|
|
|
use App\Models\Traits\UserTrait;
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-01-07 12:30:28 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2019-01-15 23:00:25 +01:00
|
|
|
use App\Utils\Traits\UserSettings;
|
2018-10-15 07:00:48 +02:00
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
2019-11-04 01:22:59 +01:00
|
|
|
use Illuminate\Contracts\Translation\HasLocalePreference;
|
2018-11-20 05:36:56 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2018-10-15 07:00:48 +02:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2018-11-20 05:36:56 +01:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2019-10-04 13:54:03 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-01-19 11:35:21 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2019-11-04 21:50:10 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-10-22 14:04:37 +02:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2019-11-04 21:50:10 +01:00
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
2018-10-04 19:10:43 +02:00
|
|
|
{
|
|
|
|
use Notifiable;
|
2018-10-15 14:40:34 +02:00
|
|
|
use SoftDeletes;
|
2018-10-22 14:04:37 +02:00
|
|
|
use PresentableTrait;
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2019-01-07 12:30:28 +01:00
|
|
|
use UserSessionAttributes;
|
2019-01-15 23:00:25 +01:00
|
|
|
use UserSettings;
|
2019-04-25 13:33:03 +02:00
|
|
|
use Filterable;
|
2019-11-22 22:10:53 +01:00
|
|
|
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
2019-04-25 13:33:03 +02:00
|
|
|
|
2018-10-15 07:00:48 +02:00
|
|
|
protected $guard = 'user';
|
2018-10-19 05:45:55 +02:00
|
|
|
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
protected $presenter = 'App\Models\Presenters\UserPresenter';
|
|
|
|
|
2019-12-29 23:06:42 +01:00
|
|
|
protected $with = []; // ? companies also
|
2019-06-12 01:15:17 +02:00
|
|
|
|
2019-07-09 02:01:29 +02:00
|
|
|
protected $dateFormat = 'Y-m-d H:i:s.u';
|
|
|
|
|
2019-06-12 01:15:17 +02:00
|
|
|
public $company;
|
|
|
|
|
2019-08-02 02:31:48 +02:00
|
|
|
protected $appends = [
|
|
|
|
'hashed_id'
|
|
|
|
];
|
|
|
|
|
2018-10-04 19:10:43 +02:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2018-10-15 14:40:34 +02:00
|
|
|
'first_name',
|
|
|
|
'last_name',
|
2019-11-04 01:22:59 +01:00
|
|
|
'email',
|
|
|
|
'phone',
|
2018-10-15 14:40:34 +02:00
|
|
|
'signature',
|
|
|
|
'avatar',
|
2019-09-24 13:22:41 +02:00
|
|
|
'accepted_terms_version',
|
|
|
|
'oauth_user_id',
|
|
|
|
'oauth_provider_id',
|
2019-12-04 02:14:55 +01:00
|
|
|
'oauth_user_token',
|
2019-12-17 11:57:15 +01:00
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
2020-02-26 04:26:07 +01:00
|
|
|
'is_deleted',
|
2018-10-04 19:10:43 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
2018-10-15 14:40:34 +02:00
|
|
|
'remember_token',
|
|
|
|
'google_2fa_secret',
|
|
|
|
'google_2fa_phone',
|
|
|
|
'remember_2fa_token',
|
|
|
|
'slack_webhook_url',
|
2018-10-04 19:10:43 +02:00
|
|
|
];
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2019-07-09 02:01:29 +02:00
|
|
|
protected $casts = [
|
|
|
|
'settings' => 'object',
|
2019-09-26 00:27:26 +02:00
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-10-04 13:54:03 +02:00
|
|
|
//'last_login' => 'timestamp',
|
2019-07-09 02:01:29 +02:00
|
|
|
];
|
|
|
|
|
2019-09-26 00:27:26 +02:00
|
|
|
public function getHashedIdAttribute()
|
|
|
|
{
|
|
|
|
return $this->encodePrimaryKey($this->id);
|
|
|
|
}
|
|
|
|
|
2019-03-28 11:07:45 +01:00
|
|
|
/**
|
|
|
|
* Returns a account.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-03-28 11:07:45 +01:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-03-28 03:36:36 +01:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Account::class);
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:11:37 +02:00
|
|
|
/**
|
2019-06-12 01:15:17 +02:00
|
|
|
* Returns all company tokens.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-06-12 01:15:17 +02:00
|
|
|
* @return Collection
|
2019-04-18 08:11:37 +02:00
|
|
|
*/
|
2019-06-12 01:15:17 +02:00
|
|
|
public function tokens()
|
|
|
|
{
|
|
|
|
return $this->hasMany(CompanyToken::class)->orderBy('id', 'ASC');
|
|
|
|
}
|
2019-03-27 05:50:13 +01:00
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns all companies a user has access to.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-15 23:00:25 +01:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-01-07 12:30:28 +01:00
|
|
|
public function companies()
|
|
|
|
{
|
2020-01-07 01:13:47 +01:00
|
|
|
return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked')->withTimestamps();
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-06-12 01:15:17 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
* As we are authenticating on CompanyToken,
|
2019-06-12 01:15:17 +02:00
|
|
|
* we need to link the company to the user manually. This allows
|
|
|
|
* us to decouple a $user and their attached companies.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setCompany($company)
|
|
|
|
{
|
|
|
|
$this->company = $company;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently set Company
|
|
|
|
*/
|
|
|
|
public function getCompany()
|
|
|
|
{
|
2020-02-24 11:15:30 +01:00
|
|
|
if($this->company)
|
|
|
|
return $this->company;
|
|
|
|
|
|
|
|
return Company::find( config('ninja.company_id') );
|
2019-06-12 01:15:17 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 11:07:45 +01:00
|
|
|
/**
|
|
|
|
* Returns the current company
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-03-28 11:07:45 +01:00
|
|
|
* @return Collection
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2019-03-28 10:05:13 +01:00
|
|
|
public function company()
|
2019-03-28 06:03:18 +01:00
|
|
|
{
|
2019-06-12 01:15:17 +02:00
|
|
|
return $this->getCompany();
|
2019-01-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 21:50:10 +01:00
|
|
|
private function setCompanyByGuard()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (Auth::guard('contact')->check()) {
|
2019-11-04 21:50:10 +01:00
|
|
|
$this->setCompany(auth()->user()->client->company);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-04 21:50:10 +01:00
|
|
|
}
|
|
|
|
|
2019-07-14 11:34:49 +02:00
|
|
|
public function company_users()
|
|
|
|
{
|
2020-03-01 11:45:23 +01:00
|
|
|
return $this->hasMany(CompanyUser::class)->withTrashed();
|
2019-01-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 12:32:53 +01:00
|
|
|
public function company_user()
|
|
|
|
{
|
2020-03-03 13:18:24 +01:00
|
|
|
if (!$this->id && auth()->user()) {
|
2019-11-24 09:19:53 +01:00
|
|
|
$this->id = auth()->user()->id;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-24 09:19:53 +01:00
|
|
|
|
2020-03-01 11:45:23 +01:00
|
|
|
return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')->where('company_user.user_id', $this->id)->withTrashed();
|
2019-11-13 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
/**
|
|
|
|
* Returns the currently set company id for the user
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-25 11:47:23 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function companyId() :int
|
|
|
|
{
|
2019-03-28 10:05:13 +01:00
|
|
|
return $this->company()->id;
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
public function clients()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Client::class);
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
2019-12-10 21:25:54 +01:00
|
|
|
* Returns a comma separated list of user permissions
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-10 21:25:54 +01:00
|
|
|
* @return comma separated list
|
2019-01-15 23:00:25 +01:00
|
|
|
*/
|
2019-01-07 12:30:28 +01:00
|
|
|
public function permissions()
|
2018-10-29 04:16:17 +01:00
|
|
|
{
|
2019-12-10 21:25:54 +01:00
|
|
|
return $this->company_user->permissions;
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns a object of User Settings
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-15 23:00:25 +01:00
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function settings()
|
|
|
|
{
|
2019-11-22 22:10:53 +01:00
|
|
|
return json_decode($this->company_user->settings);
|
2019-01-15 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a boolean of the administrator status of the user
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-15 23:00:25 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function isAdmin() : bool
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2019-11-22 22:10:53 +01:00
|
|
|
return $this->company_user->is_admin;
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 03:45:18 +01:00
|
|
|
public function isOwner() : bool
|
|
|
|
{
|
|
|
|
return $this->company_user->is_owner;
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns all user created contacts
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-15 23:00:25 +01:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
public function contacts()
|
|
|
|
{
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->hasMany(ClientContact::class);
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns a boolean value if the user owns the current Entity
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-15 23:00:25 +01:00
|
|
|
* @param string Entity
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-07 12:30:28 +01:00
|
|
|
public function owns($entity) : bool
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
|
|
|
return ! empty($entity->user_id) && $entity->user_id == $this->id;
|
|
|
|
}
|
2019-01-07 12:30:28 +01:00
|
|
|
|
2019-11-05 11:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns a boolean value if the user is assigned to the current Entity
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-05 11:16:38 +01: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-07 12:30:28 +01:00
|
|
|
|
2019-01-20 06:00:50 +01:00
|
|
|
/**
|
|
|
|
* Returns true if permissions exist in the map
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-01-20 06:00:50 +01:00
|
|
|
* @param string permission
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function hasPermission($permission) : bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-11-22 22:10:53 +01:00
|
|
|
return (stripos($this->company_user->permissions, $permission) !== false);
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
2019-01-15 23:00:25 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
public function getEmailVerifiedAt()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->email_verified_at) {
|
2019-10-05 02:11:04 +02:00
|
|
|
return Carbon::parse($this->email_verified_at)->timestamp;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-10-04 13:54:03 +02:00
|
|
|
return null;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-04 13:54:03 +02:00
|
|
|
}
|
2019-11-04 01:22:59 +01:00
|
|
|
|
|
|
|
public function routeNotificationForSlack($notification)
|
|
|
|
{
|
2020-02-24 11:15:30 +01:00
|
|
|
|
|
|
|
if($this->company())
|
2020-02-28 22:57:47 +01:00
|
|
|
return $this->company()->slack_webhook_url;
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2019-11-04 01:22:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 21:50:10 +01:00
|
|
|
|
|
|
|
public function routeNotificationForMail($notification)
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
2019-11-05 00:26:15 +01:00
|
|
|
|
2019-11-05 23:52:57 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the model for a bound value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
|
|
*/
|
|
|
|
public function resolveRouteBinding($value)
|
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->withTrashed()
|
|
|
|
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
|
|
|
}
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|