Skip to content

Commit 383d345

Browse files
committed
Use the original idea for fixing oauth clients
1 parent 1d2cc90 commit 383d345

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

ProcessMaker/Providers/ProcessMakerServiceProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Laravel\Horizon\Horizon;
2424
use Laravel\Horizon\SystemProcessCounter;
2525
use Laravel\Horizon\WorkerCommandString;
26+
use Laravel\Passport\Client as PassportClient;
2627
use Lavary\Menu\Menu;
2728
use OpenApi\Analysers\AttributeAnnotationFactory;
2829
use OpenApi\Analysers\DocBlockAnnotationFactory;
@@ -354,6 +355,24 @@ protected static function bootObservers(): void
354355
Models\ProcessRequestToken::observe(Observers\ProcessRequestTokenObserver::class);
355356

356357
Models\ProcessCollaboration::observe(Observers\ProcessCollaborationObserver::class);
358+
359+
// Due to this change https://github.com/laravel/passport/blob/ea020190123953426a439f0267c6cfa478f6e6e7/src/Guards/TokenGuard.php#L146
360+
// user ID is now required for bearer tokens clients. Any user will work here, the token itself
361+
// is what's associated with the real user. For now, we'll use the first administrator user.
362+
PassportClient::creating(function (PassportClient $client): void {
363+
if (!$client->personal_access_client || $client->user_id !== null) {
364+
return;
365+
}
366+
367+
$adminUserId = Models\User::query()
368+
->where('is_administrator', true)
369+
->orderBy('id')
370+
->value('id');
371+
372+
if ($adminUserId !== null) {
373+
$client->user_id = $adminUserId;
374+
}
375+
});
357376
}
358377

359378
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\DB;
4+
use ProcessMaker\Upgrades\UpgradeMigration as Upgrade;
5+
6+
class SetUserIdOnOauthClient extends Upgrade
7+
{
8+
/**
9+
* Run any validations/pre-run checks to ensure the environment, settings,
10+
* packages installed, etc. are right correct to run this upgrade.
11+
*
12+
* Throw a \RuntimeException if the conditions are *NOT* correct for this
13+
* upgrade migration to run. If this is not a required upgrade, then it
14+
* will be skipped. Otherwise the exception thrown will be caught, noted,
15+
* and will prevent the remaining migrations from continuing to run.
16+
*
17+
* Returning void or null denotes the checks were successful.
18+
*
19+
* @return void
20+
*
21+
* @throws RuntimeException
22+
*/
23+
public function preflightChecks()
24+
{
25+
//
26+
}
27+
28+
/**
29+
* Run the upgrade migration.
30+
*
31+
* @return void
32+
*/
33+
public function up()
34+
{
35+
$adminUserId = DB::table('users')
36+
->where('is_administrator', true)
37+
->orderBy('id')
38+
->value('id');
39+
40+
if ($adminUserId === null) {
41+
return;
42+
}
43+
44+
DB::table('oauth_clients')
45+
->where('personal_access_client', true)
46+
->whereNull('user_id')
47+
->update(['user_id' => $adminUserId]);
48+
}
49+
50+
/**
51+
* Reverse the upgrade migration.
52+
*
53+
* @return void
54+
*/
55+
public function down()
56+
{
57+
$adminUserId = DB::table('users')
58+
->where('is_administrator', true)
59+
->orderBy('id')
60+
->value('id');
61+
62+
if ($adminUserId === null) {
63+
return;
64+
}
65+
66+
DB::table('oauth_clients')
67+
->where('personal_access_client', true)
68+
->where('user_id', $adminUserId)
69+
->update(['user_id' => null]);
70+
}
71+
}

0 commit comments

Comments
 (0)