|
| 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