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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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;
|
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;
|
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-01-19 11:35:21 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2019-03-28 06:03:18 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-10-22 14:04:37 +02:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2018-10-24 05:50:15 +02: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;
|
|
|
|
|
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-02-04 13:06:19 +01:00
|
|
|
protected $with = ['companies','user_companies'];
|
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',
|
|
|
|
'email',
|
|
|
|
'phone',
|
|
|
|
'signature',
|
|
|
|
'avatar',
|
2018-10-19 05:45:55 +02:00
|
|
|
'accepted_terms_version'
|
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',
|
|
|
|
'oauth_user_id',
|
|
|
|
'oauth_provider_id',
|
|
|
|
'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-03-28 11:07:45 +01:00
|
|
|
/**
|
|
|
|
* Returns a account.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-03-28 03:36:36 +01:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Account::class);
|
|
|
|
}
|
|
|
|
|
2019-03-28 11:07:45 +01:00
|
|
|
/**
|
|
|
|
* Returns all company tokens.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-03-27 05:50:13 +01:00
|
|
|
public function tokens()
|
|
|
|
{
|
2019-04-18 08:11:37 +02:00
|
|
|
return $this->hasMany(CompanyToken::class)->orderBy('id', 'ASC');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return first user token
|
|
|
|
*
|
|
|
|
* @return token object
|
|
|
|
*/
|
|
|
|
public function token()
|
|
|
|
{
|
|
|
|
return $this->tokens()->first();
|
2019-03-27 05:50:13 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns all companies a user has access to.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-01-07 12:30:28 +01:00
|
|
|
public function companies()
|
|
|
|
{
|
2019-05-22 02:56:47 +02:00
|
|
|
return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-03-28 11:07:45 +01:00
|
|
|
/**
|
|
|
|
* Returns the current company
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-03-28 10:05:13 +01:00
|
|
|
public function company()
|
2019-03-28 06:03:18 +01:00
|
|
|
{
|
2019-03-28 10:05:13 +01:00
|
|
|
return $this->tokens()->whereRaw("BINARY `token`= ?", [request()->header('X-API-TOKEN')])->first()->company;
|
2019-01-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
2019-01-22 10:47:26 +01:00
|
|
|
* Returns the pivot tables for Company / User
|
|
|
|
*
|
|
|
|
* @return Collection
|
2019-05-22 02:56:47 +02:00
|
|
|
*
|
2019-01-22 10:47:26 +01:00
|
|
|
*/
|
|
|
|
public function user_companies()
|
|
|
|
{
|
|
|
|
return $this->hasMany(CompanyUser::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current company by
|
|
|
|
* querying directly on the pivot table relationship
|
2019-01-15 23:00:25 +01:00
|
|
|
*
|
|
|
|
* @return Collection
|
2019-03-27 10:38:28 +01:00
|
|
|
* @deprecated
|
2019-01-15 23:00:25 +01:00
|
|
|
*/
|
2019-01-25 11:47:23 +01:00
|
|
|
public function user_company()
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2019-03-28 10:05:13 +01:00
|
|
|
|
2019-03-28 06:03:18 +01:00
|
|
|
return $this->user_companies->where('company_id', $this->companyId())->first();
|
2019-01-25 11:47:23 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2019-01-25 11:47:23 +01:00
|
|
|
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns a object of user permissions
|
|
|
|
*
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
2019-01-07 12:30:28 +01:00
|
|
|
public function permissions()
|
2018-10-29 04:16:17 +01:00
|
|
|
{
|
2019-01-07 12:30:28 +01:00
|
|
|
|
2019-03-28 03:36:36 +01:00
|
|
|
$permissions = json_decode($this->user_company()->permissions);
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
if (! $permissions)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
return $permissions;
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns a object of User Settings
|
|
|
|
*
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function settings()
|
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-03-28 03:36:36 +01:00
|
|
|
return json_decode($this->user_company()->settings);
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a boolean of the administrator status of the user
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function isAdmin() : bool
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-03-28 06:03:18 +01:00
|
|
|
return $this->user_company()->is_admin;
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns all user created contacts
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
public function contacts()
|
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
return $this->hasMany(Contact::class);
|
2019-01-22 10:47:26 +01:00
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
return ! empty($entity->user_id) && $entity->user_id == $this->id;
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|
2019-01-07 12:30:28 +01:00
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Flattens a stdClass representation of the User Permissions
|
|
|
|
* into a Collection
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function permissionsFlat() :Collection
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-07 12:30:28 +01:00
|
|
|
return collect($this->permissions())->flatten();
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 06:00:50 +01:00
|
|
|
/**
|
|
|
|
* Returns true if permissions exist in the map
|
|
|
|
*
|
|
|
|
* @param string permission
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function hasPermission($permission) : bool
|
|
|
|
{
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-16 10:28:06 +01:00
|
|
|
return $this->permissionsFlat()->contains($permission);
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* Returns a array of permission for the mobile application
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-01-22 10:47:26 +01:00
|
|
|
public function permissionsMap() : array
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
$keys = array_values((array) $this->permissions());
|
|
|
|
$values = array_fill(0, count($keys), true);
|
|
|
|
|
|
|
|
return array_combine($keys, $values);
|
2019-01-22 10:47:26 +01:00
|
|
|
|
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');
|
|
|
|
}
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|