1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Models/CompanyUser.php

209 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Models;
2023-08-11 10:04:41 +02:00
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany;
2023-10-26 04:57:44 +02:00
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
2023-03-08 08:33:42 +01:00
/**
* App\Models\CompanyUser
*
* @property int $id
* @property int $company_id
* @property int $account_id
* @property int $user_id
* @property string|null $permissions
* @property object|null $notifications
* @property object|null $settings
* @property string $slack_webhook_url
2023-04-26 15:03:32 +02:00
* @property bool $is_owner
* @property bool $is_admin
* @property bool $is_locked
2023-03-08 08:33:42 +01:00
* @property int|null $deleted_at
* @property int|null $created_at
* @property int|null $updated_at
* @property int $permissions_updated_at
* @property string $ninja_portal_url
2023-06-07 05:53:38 +02:00
* @property object|null $react_settings
2023-03-08 08:33:42 +01:00
* @property-read \App\Models\Account $account
* @property-read \App\Models\Company $company
2023-04-26 15:03:32 +02:00
* @property-read \App\Models\CompanyUser $cu
2023-03-08 08:33:42 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $token
* @property-read int|null $token_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $tokens
* @property-read int|null $tokens_count
* @property-read \App\Models\User $user
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $users
* @property-read int|null $users_count
2023-08-11 10:04:41 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser authCompany()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser query()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereAccountId($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereCompanyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereIsAdmin($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereIsLocked($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereIsOwner($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereNinjaPortalUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereNotifications($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser wherePermissions($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser wherePermissionsUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereReactSettings($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereSettings($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereSlackWebhookUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser whereUserId($value)
2023-03-08 08:33:42 +01:00
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyUser withoutTrashed()
2023-03-16 05:20:38 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $token
2023-03-29 04:13:50 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $tokens
2023-04-26 00:43:54 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $users
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $token
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $tokens
2023-04-26 08:55:49 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $users
2023-04-26 15:03:32 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyUser> $cu
2023-04-26 08:55:49 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $token
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyToken> $tokens
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
*/
class CompanyUser extends Pivot
{
use SoftDeletes;
2022-03-15 13:28:16 +01:00
use \Awobaz\Compoships\Compoships;
protected $dateFormat = 'Y-m-d H:i:s.u';
/**
* The attributes that should be cast to native types.
*
*/
protected $casts = [
2021-01-29 13:29:42 +01:00
'permissions_updated_at' => 'timestamp',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'settings' => 'object',
'notifications' => 'object',
'permissions' => 'string',
2023-06-05 11:38:38 +02:00
'react_settings' => 'object',
];
protected $fillable = [
'account_id',
'permissions',
'notifications',
'settings',
2023-02-14 11:04:07 +01:00
'react_settings',
'is_admin',
'is_owner',
'is_locked',
'slack_webhook_url',
'shop_restricted',
];
protected $touches = ['user'];
protected $with = ['user', 'account'];
2022-04-11 09:45:37 +02:00
public function getEntityType()
{
return self::class;
}
2023-08-07 00:23:13 +02:00
public function account(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
2019-09-11 02:37:53 +02:00
return $this->belongsTo(Account::class);
}
2023-08-07 00:23:13 +02:00
/**
2023-08-11 10:23:59 +02:00
* @return \Illuminate\Database\Eloquent\Relations\HasOne
2023-08-07 00:23:13 +02:00
*/
2023-08-11 10:23:59 +02:00
public function user_pivot(): \Illuminate\Database\Eloquent\Relations\HasOne
{
2023-02-14 11:04:07 +01:00
return $this->hasOne(User::class)->withPivot('permissions', 'settings', 'react_settings', 'is_admin', 'is_owner', 'is_locked', 'slack_webhook_url', 'migrating');
}
2023-08-07 00:23:13 +02:00
/**
2023-08-11 10:23:59 +02:00
* @return \Illuminate\Database\Eloquent\Relations\HasOne
2023-08-07 00:23:13 +02:00
*/
2023-08-11 10:23:59 +02:00
public function company_pivot(): \Illuminate\Database\Eloquent\Relations\HasOne
{
2023-02-14 11:04:07 +01:00
return $this->hasOne(Company::class)->withPivot('permissions', 'settings', 'react_settings', 'is_admin', 'is_owner', 'is_locked', 'slack_webhook_url', 'migrating');
}
2023-08-11 10:04:41 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2023-10-26 04:57:44 +02:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
2023-08-11 10:04:41 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class);
}
2023-08-11 09:58:43 +02:00
/**
2023-08-11 10:04:41 +02:00
* @return HasMany
2023-08-11 09:58:43 +02:00
*/
2023-08-11 10:04:41 +02:00
public function users()
{
2021-11-07 12:18:15 +01:00
return $this->hasMany(User::class)->withTrashed();
}
2023-08-11 10:04:41 +02:00
/**
* @return HasMany
*/
public function token()
{
return $this->hasMany(CompanyToken::class, 'user_id', 'user_id');
}
2023-08-11 10:04:41 +02:00
/**
2023-10-26 04:57:44 +02:00
* @return HasMany
2023-08-11 10:04:41 +02:00
*/
public function tokens()
{
return $this->hasMany(CompanyToken::class, 'user_id', 'user_id');
}
2023-08-11 10:04:41 +02:00
public function scopeAuthCompany($query): \Illuminate\Database\Eloquent\Builder
{
2023-06-07 06:29:06 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$query->where('company_id', $user->companyId());
return $query;
}
2024-01-14 05:05:00 +01:00
2023-06-07 06:29:06 +02:00
/**
2023-06-07 07:45:44 +02:00
* Determines if the notifications should be React or Flutter links
2023-06-07 06:29:06 +02:00
*
* @return bool
*/
2023-08-11 10:04:41 +02:00
public function portalType(): bool
2024-02-13 05:25:18 +01:00
{
2023-06-07 06:29:06 +02:00
return isset($this->react_settings->react_notification_link) && $this->react_settings->react_notification_link;
}
2023-10-26 04:57:44 +02:00
}