osb/app/Models/ProviderOauth.php

79 lines
1.7 KiB
PHP
Raw Normal View History

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