Skip to content

Commit

Permalink
Cleaning config table (LycheeOrg#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Sep 5, 2022
1 parent b31d345 commit e18c16b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Actions/Diagnostics/Checks/ConfigSanityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function check(array &$errors): void
$settings = Configs::get();

$keys_checked = [
'username', 'password', 'sorting_photos_col', 'sorting_albums_col',
'sorting_photos_col', 'sorting_albums_col',
'imagick', 'skip_duplicates', 'check_for_updates', 'version',
];

Expand Down
8 changes: 8 additions & 0 deletions app/Models/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,12 @@ public function scopeAdmin(FixedQueryBuilder $query): FixedQueryBuilder
{
return $query->where('confidentiality', '<=', 3);
}

/**
* Reset the cache.
*/
public static function invalidateCache(): void
{
self::$cache = [];
}
}
15 changes: 8 additions & 7 deletions database/migrations/2020_12_12_203153_migrate_admin_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Exceptions\ModelDBException;
use App\Models\Configs;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand All @@ -17,13 +18,13 @@ class MigrateAdminUser extends Migration
*/
public function up(): void
{
DB::table('users')->insert([
'id' => 0,
'username' => Configs::getValueAsString('username', ''),
'password' => Configs::getValueAsString('password', ''),
'lock' => false,
'upload' => true,
]);
$user = User::query()->findOrNew(0);
$user->incrementing = false; // disable auto-generation of ID
$user->id = 0;
Configs::invalidateCache();
$user->username = Configs::getValueAsString('username', '');
$user->password = Configs::getValueAsString('password', '');
$user->save();
}

/**
Expand Down
47 changes: 47 additions & 0 deletions database/migrations/2022_08_27_110209_drop_admin_user_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class DropAdminUserConfig extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::table('configs')
->whereIn('key', ['username', 'password'])
->delete();
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
defined('STRING_REQ') or define('STRING_REQ', 'string_required');

DB::table('configs')->insert([
[
'key' => 'username',
'value' => '',
'confidentiality' => '4',
'cat' => 'Admin',
'type_range' => STRING_REQ,
],
[
'key' => 'password',
'value' => '',
'confidentiality' => '4',
'cat' => 'Admin',
'type_range' => STRING_REQ,
],
]
);
}
}
2 changes: 2 additions & 0 deletions tests/Feature/InstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Tests\Feature;

use App\Models\Configs;
use App\Models\User;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -143,6 +144,7 @@ public function testInstall(): void
/**
* We now should NOT be redirected.
*/
Configs::invalidateCache();
$response = $this->get('/');
$response->assertOk();

Expand Down

0 comments on commit e18c16b

Please sign in to comment.