|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Integrations\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
7
|
|
|
|
|
8
|
|
|
class Provider extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
use SoftDeletes; |
|
11
|
|
|
|
|
12
|
|
|
// Meta ======================================================================== |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The attributes that are not mass-assignable. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $guarded = ['id', 'login_count', 'created_at', 'updated_at', 'deleted_at']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* What should be returned when this model is converted to string. |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __toString() |
|
27
|
|
|
{ |
|
28
|
|
|
return (string) $this->name; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the human-friendly singular name of the resource. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getSingularAttribute() |
|
37
|
|
|
{ |
|
38
|
|
|
return _('Provider'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the human-friendly plural name of the resource. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getPluralAttribute() |
|
47
|
|
|
{ |
|
48
|
|
|
return _('Providers'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Relationships =============================================================== |
|
52
|
|
|
|
|
53
|
|
|
public function users() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->hasMany('App\Models\User'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Events ====================================================================== |
|
59
|
|
|
|
|
60
|
|
|
// Static Methods ============================================================== |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get all usable providers. |
|
64
|
|
|
* |
|
65
|
|
|
* @return \Illuminate\Database\Eloquent\Collection (of AuthProvider) |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getUsable() |
|
68
|
|
|
{ |
|
69
|
|
|
return self::orderBy('name')->get()->filter( |
|
70
|
|
|
function ($provider) { |
|
71
|
|
|
return $provider->isUsable(); |
|
72
|
|
|
} |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Bussiness logic ============================================================= |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Determine if provider is enabled and has been configured in config/services.php file. |
|
80
|
|
|
* |
|
81
|
|
|
* @return boolean |
|
82
|
|
|
*/ |
|
83
|
|
|
public function isUsable() |
|
84
|
|
|
{ |
|
85
|
|
|
// Check database fields |
|
86
|
|
|
if(! $this->id or empty($this->name) or empty($this->slug) or $this->trashed()) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Check config |
|
91
|
|
|
$config = \Illuminate\Support\Facades\Config::get("services.{$this->slug}.client_id", \Illuminate\Support\Facades\Config::get("services.{$this->slug}.client_secret")); |
|
92
|
|
|
|
|
93
|
|
|
return ! empty($config); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|