This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
/
Bungie.php
210 lines (179 loc) · 4.6 KB
/
Bungie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace App\Models;
use App\Account;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Database\Eloquent\Model;
/**
* Class Bungie.
*
* @property int $id
* @property int $membership_id
* @property Carbon $first_access
* @property Carbon $last_update
* @property string $unique_name
* @property string $display_name
* @property string $refresh_token
* @property Carbon $refresh_expires
* @property string $access_token
* @property Carbon $expires
* @property string $remember_token
* @property int $preferred_account_id
* @property-read Account $account
* @property-read Account[] $accounts
* @mixin \Eloquent
*/
class Bungie extends Model implements Authenticatable, UserProvider
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'bungie';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'membership_id', 'first_access', 'last_update', 'unique_name', 'display_name',
'refresh_token', 'refresh_expires', 'access_token', 'expires',
];
/**
* @var bool
*/
public $timestamps = false;
/**
* @var array
*/
public $dates = ['first_access', 'last_update', 'refresh_expires', 'expires'];
//---------------------------------------------------------------------------------
// Accessors & Mutators
//---------------------------------------------------------------------------------
public function setRefreshExpiresAttribute($value)
{
$this->attributes['refresh_expires'] = Carbon::now()->addSeconds($value);
}
public function setExpiresAttribute($value)
{
$this->attributes['expires'] = Carbon::now()->addSeconds($value);
}
public function setFirstAccessAttribute($value)
{
$this->attributes['first_access'] = Carbon::parse($value);
}
public function setLastUpdateAttribute($value)
{
$this->attributes['last_update'] = Carbon::parse($value);
}
//---------------------------------------------------------------------------------
// Public Methods
//---------------------------------------------------------------------------------
public function accounts()
{
return $this->hasMany(Account::class, 'bungie_id', 'id');
}
public function account()
{
return $this->hasOne(Account::class, 'id', 'preferred_account_id');
}
public function url() : string
{
return $this->account->url();
}
public function isActive() : bool
{
return $this->expires->isFuture();
}
public function isDonator()
{
$badges = $this->account->badges;
foreach ($badges as $badge) {
if ($badge->slug === 'donator') {
return true;
}
}
return false;
}
//---------------------------------------------------------------------------------
// Auth Functions
//---------------------------------------------------------------------------------
/**
* {@inheritdoc}
*/
public function getAuthIdentifierName()
{
return 'id';
}
/**
* {@inheritdoc}
*/
public function getAuthIdentifier()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getAuthPassword()
{
}
/**
* {@inheritdoc}
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* {@inheritdoc}
*/
public function setRememberToken($value)
{
$this->attributes['remember_token'] = $value;
}
/**
* {@inheritdoc}
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* {@inheritdoc}
*/
public function retrieveById($identifier)
{
return self::where('id', $identifier)->first();
}
/**
* {@inheritdoc}
*/
public function retrieveByToken($identifier, $token)
{
return self::where('id', $identifier)->where('remember_token', $token)->first();
}
/**
* {@inheritdoc}
*/
public function updateRememberToken(Authenticatable $user, $token)
{
$user->setRememberToken($token);
}
/**
* {@inheritdoc}
*/
public function retrieveByCredentials(array $credentials)
{
// no auth
}
/**
* {@inheritdoc}
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
return false; // no auth
}
}