mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 05:32:39 +01:00
0c1fc0d904
* Wire up Create Entity Button to create route * Refactor permissions, we must also ensure the user company id and entity id matches at the Gate:: * Add translations for Status filters * Bug fix for initial list view not displaying * Apply actions to menu for list items * Wire up list view actions, individual * Place permission filters on datatable lists
175 lines
3.8 KiB
PHP
175 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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 Laracasts\Presenter\PresentableTrait;
|
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
{
|
|
use Notifiable;
|
|
use SoftDeletes;
|
|
use PresentableTrait;
|
|
use MakesHash;
|
|
use UserSessionAttributes;
|
|
use UserSettings;
|
|
|
|
protected $guard = 'user';
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
protected $presenter = 'App\Models\Presenters\UserPresenter';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'phone',
|
|
'signature',
|
|
'avatar',
|
|
'accepted_terms_version'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'remember_token',
|
|
'oauth_user_id',
|
|
'oauth_provider_id',
|
|
'google_2fa_secret',
|
|
'google_2fa_phone',
|
|
'remember_2fa_token',
|
|
'slack_webhook_url',
|
|
];
|
|
|
|
/**
|
|
* Returns all companies a user has access to.
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function companies()
|
|
{
|
|
return $this->belongsToMany(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
|
|
}
|
|
|
|
/**
|
|
* Returns the current company
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function company()
|
|
{
|
|
return $this->companies()->where('company_id', $this->getCurrentCompanyId())->first();
|
|
}
|
|
|
|
/**
|
|
* Returns a object of user permissions
|
|
*
|
|
* @return stdClass
|
|
*/
|
|
public function permissions()
|
|
{
|
|
|
|
$permissions = json_decode($this->company()->pivot->permissions);
|
|
|
|
if (! $permissions)
|
|
return [];
|
|
|
|
return $permissions;
|
|
}
|
|
|
|
/**
|
|
* Returns a object of User Settings
|
|
*
|
|
* @return stdClass
|
|
*/
|
|
public function settings()
|
|
{
|
|
return json_decode($this->company()->pivot->settings);
|
|
}
|
|
|
|
/**
|
|
* Returns a boolean of the administrator status of the user
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isAdmin() : bool
|
|
{
|
|
return (bool) $this->company()->pivot->is_admin;
|
|
}
|
|
|
|
/**
|
|
* Returns all user created contacts
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function contacts()
|
|
{
|
|
return $this->hasMany(Contact::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()
|
|
{
|
|
|
|
$keys = array_values((array) $this->permissions());
|
|
$values = array_fill(0, count($keys), true);
|
|
|
|
return array_combine($keys, $values);
|
|
}
|
|
|
|
}
|