-
Notifications
You must be signed in to change notification settings - Fork 2
/
SolidIdentity.php
131 lines (109 loc) · 3.62 KB
/
SolidIdentity.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
<?php
namespace Fleetbase\Solid\Models;
use Fleetbase\Casts\Json;
use Fleetbase\Models\Company;
use Fleetbase\Models\Model;
use Fleetbase\Models\User;
use Fleetbase\Solid\Client\SolidClient;
use Fleetbase\Support\Utils;
use Fleetbase\Traits\HasUuid;
use Illuminate\Support\Str;
class SolidIdentity extends Model
{
use HasUuid;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'solid_identities';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['company_uuid', 'user_uuid', 'token_response', 'identifier'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'token_response' => Json::class,
];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'user_uuid', 'uuid');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class, 'company_uuid', 'uuid');
}
public function getAccessToken(): ?string
{
return data_get($this, 'token_response.access_token');
}
public function getRedirectUri(array $query = [], int $port = 8000): string
{
return Utils::apiUrl('solid/int/v1/oidc/complete-registration/' . $this->identifier, $query, $port);
}
public function generateRequestCode(): SolidIdentity
{
$requestCode = static::generateUniqueRequestCode();
$this->update(['identifier' => $requestCode]);
return $this;
}
/**
* Generate a unique request code.
*/
public static function generateUniqueRequestCode(): string
{
do {
// Generate a random string.
$requestCode = Str::random(16);
// Check if it's unique in the identifier column
$exists = static::where('identifier', $requestCode)->exists();
} while ($exists);
return $requestCode;
}
/**
* Initializes a SolidIdentity instance for the current session.
*
* This method retrieves an existing SolidIdentity based on the user's and company's UUIDs
* from the current session. If no identity is found, a new one is created.
* In both cases, a new unique request code is generated and updated for the SolidIdentity.
* The updated SolidIdentity instance is then returned.
*
* @return SolidIdentity the SolidIdentity instance with updated request code
*/
public static function initialize(): SolidIdentity
{
$requestCode = static::generateUniqueRequestCode();
$solidIdentity = static::firstOrCreate(['user_uuid' => session('user'), 'company_uuid' => session('company')]);
$solidIdentity->update(['identifier' => $requestCode]);
return $solidIdentity;
}
public static function current(): SolidIdentity
{
$solidIdentity = static::where(['user_uuid' => session('user'), 'company_uuid' => session('company')])->first();
if (!$solidIdentity) {
return static::initialize();
}
// If no request code
if (empty($solidIdentity->identifier)) {
$solidIdentity->generateRequestCode();
}
return $solidIdentity;
}
public function request(string $method, string $uri, array $data = [], array $options = [])
{
$solidClient = new SolidClient();
return $solidClient->requestWithIdentity($this, $method, $uri, $data, $options);
}
}