2022-08-12 04:53:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-05-12 10:09:51 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2022-08-12 04:53:06 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2022-08-18 13:29:42 +00:00
|
|
|
use App\Traits\SiteID;
|
|
|
|
|
2022-08-12 04:53:06 +00:00
|
|
|
class ProviderOauth extends Model
|
|
|
|
{
|
2022-08-18 13:29:42 +00:00
|
|
|
use SiteID;
|
|
|
|
|
2022-08-12 04:53:06 +00:00
|
|
|
protected $table = 'provider_oauth';
|
|
|
|
|
|
|
|
protected $fillable = ['name','active'];
|
|
|
|
|
|
|
|
/* RELATIONS */
|
|
|
|
|
2022-08-18 13:29:42 +00:00
|
|
|
public function accounts()
|
|
|
|
{
|
2023-05-10 03:59:42 +00:00
|
|
|
return $this->belongsToMany(Account::class,'account__provider')
|
|
|
|
->where('account__provider.site_id',$this->site_id)
|
|
|
|
->withPivot('ref','synctoken','created_at','updated_at');
|
|
|
|
}
|
|
|
|
|
2023-05-12 10:09:51 +00:00
|
|
|
public function invoices()
|
2023-05-10 03:59:42 +00:00
|
|
|
{
|
2023-05-12 10:09:51 +00:00
|
|
|
return $this->belongsToMany(Invoice::class,'invoice__provider')
|
|
|
|
->where('invoice__provider.site_id',$this->site_id)
|
|
|
|
->withPivot('ref','synctoken','created_at','updated_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taxes()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Tax::class,'tax__provider')
|
|
|
|
->where('tax__provider.site_id',$this->site_id)
|
2022-08-18 13:29:42 +00:00
|
|
|
->withPivot('ref','synctoken','created_at','updated_at');
|
|
|
|
}
|
|
|
|
|
2022-08-12 04:53:06 +00:00
|
|
|
public function tokens()
|
|
|
|
{
|
|
|
|
return $this->hasMany(ProviderToken::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->hasMany(UserOauth::class);
|
|
|
|
}
|
2022-08-18 13:29:42 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
2023-05-12 10:09:51 +00:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2022-08-18 13:29:42 +00:00
|
|
|
public function api_class(): ?string
|
|
|
|
{
|
|
|
|
return config('services.provider.'.strtolower($this->name).'.api');
|
|
|
|
}
|
|
|
|
|
2023-05-12 10:09:51 +00:00
|
|
|
/**
|
|
|
|
* Return a list of the provider OAUTH details
|
|
|
|
*/
|
|
|
|
public static function providers(): Collection
|
2022-08-18 13:29:42 +00:00
|
|
|
{
|
2023-05-12 10:09:51 +00:00
|
|
|
return (new self)::whereIn('name',array_keys(config('services.provider')))->get();
|
2022-08-18 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-12 10:09:51 +00:00
|
|
|
* Return the token object for a specific user
|
2022-08-18 13:29:42 +00:00
|
|
|
*
|
2023-05-12 10:09:51 +00:00
|
|
|
* @param User $uo
|
|
|
|
* @return ProviderToken|null
|
2022-08-18 13:29:42 +00:00
|
|
|
*/
|
2023-05-12 10:09:51 +00:00
|
|
|
public function token(User $uo): ?ProviderToken
|
2022-08-18 13:29:42 +00:00
|
|
|
{
|
2023-05-12 10:09:51 +00:00
|
|
|
return (($x=$this->tokens->where('user_id',$uo->id))->count() === 1) ? $x->pop() : NULL;
|
2022-08-18 13:29:42 +00:00
|
|
|
}
|
2022-08-12 04:53:06 +00:00
|
|
|
}
|