1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/User.php

315 lines
6.4 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)
*
* @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
namespace App\Models;
2018-10-04 19:10:43 +02:00
use App\Models\Company;
2019-03-27 10:38:28 +01:00
use App\Models\CompanyToken;
use App\Models\CompanyUser;
2019-04-25 13:33:03 +02:00
use App\Models\Filterable;
use App\Models\Traits\UserTrait;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes;
use App\Utils\Traits\UserSettings;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
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;
use SoftDeletes;
use PresentableTrait;
use MakesHash;
use UserSessionAttributes;
use UserSettings;
2019-04-25 13:33:03 +02:00
use Filterable;
protected $guard = 'user';
protected $dates = ['deleted_at'];
protected $presenter = 'App\Models\Presenters\UserPresenter';
protected $with = ['companies','user_companies'];
protected $dateFormat = 'Y-m-d H:i:s.u';
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 = [
'first_name',
'last_name',
'email',
'phone',
'signature',
'avatar',
'accepted_terms_version',
'oauth_user_id',
'oauth_provider_id',
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 = [
'settings' => 'object',
'permissions' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
public function getHashedIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
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-04-18 08:11:37 +02:00
/**
* Returns all company tokens.
*
* @return Collection
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.
*
* @return Collection
*/
public function companies()
{
return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
}
/**
*
* 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.
*
*/
public function setCompany($company)
{
$this->company = $company;
}
/**
* Returns the currently set Company
*/
public function getCompany()
{
return $this->company;
}
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()
{
return $this->getCompany();
}
/**
* Returns the pivot tables for Company / User
*
* @return Collection
*
*/
public function user_companies()
{
return $this->hasMany(CompanyUser::class);
}
/**
* Alias of user_companies()
*/
public function company_users()
{
return $this->user_companies();
}
/**
* Returns the current company by
* querying directly on the pivot table relationship
*
* @return Collection
2019-03-27 10:38:28 +01:00
* @deprecated
*/
public function user_company()
{
2019-03-28 10:05:13 +01:00
return $this->user_companies->where('company_id', $this->companyId())->first();
}
/**
* 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;
}
/**
* Returns a object of user permissions
*
* @return stdClass
*/
public function permissions()
{
2019-03-28 03:36:36 +01:00
$permissions = json_decode($this->user_company()->permissions);
if (! $permissions)
return [];
return $permissions;
}
/**
* Returns a object of User Settings
*
* @return stdClass
*/
public function settings()
{
2019-03-28 03:36:36 +01:00
return json_decode($this->user_company()->settings);
}
/**
* Returns a boolean of the administrator status of the user
*
* @return bool
*/
public function isAdmin() : bool
{
return $this->user_company()->is_admin;
}
/**
* Returns all user created contacts
*
* @return Collection
*/
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;
}
/**
* Flattens a stdClass representation of the User Permissions
* into a Collection
*
* @return Collection
*/
public function permissionsFlat() :Collection
{
return collect($this->permissions())->flatten();
}
/**
* Returns true if permissions exist in the map
*
* @param string permission
* @return boolean
*/
public function hasPermission($permission) : bool
{
return $this->permissionsFlat()->contains($permission);
}
/**
* Returns a array of permission for the mobile application
*
* @return array
*/
public function permissionsMap() : array
{
$keys = array_values((array) $this->permissions());
$values = array_fill(0, count($keys), true);
return array_combine($keys, $values);
}
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
}