From a13175600fc4a469ba72f51aa1cbc154b5337e6e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 21 Jul 2018 23:02:44 +0930 Subject: [PATCH] Fix group/permission seeding Updating the Migration::addPermission helper table name means we need to move the seed migration to after the table rename migration. We also add a sanity check for each permission's group since the foreign key will fail if the group doesn't exist. Of course, the only way to make sure groups are seeded before permissions is to move them into another migration. --- ...003952_allow_hide_posts_for_moderators.php | 17 --------- .../2018_07_21_000000_seed_default_groups.php | 38 +++++++++++++++++++ ...000100_seed_default_group_permissions.php} | 0 framework/core/src/Database/Migration.php | 26 +++++++------ .../src/Install/Console/InstallCommand.php | 24 ------------ 5 files changed, 53 insertions(+), 52 deletions(-) delete mode 100644 framework/core/migrations/2018_07_16_003952_allow_hide_posts_for_moderators.php create mode 100644 framework/core/migrations/2018_07_21_000000_seed_default_groups.php rename framework/core/migrations/{2015_02_25_000000_setup_default_permissions.php => 2018_07_21_000100_seed_default_group_permissions.php} (100%) diff --git a/framework/core/migrations/2018_07_16_003952_allow_hide_posts_for_moderators.php b/framework/core/migrations/2018_07_16_003952_allow_hide_posts_for_moderators.php deleted file mode 100644 index 57203b7b1..000000000 --- a/framework/core/migrations/2018_07_16_003952_allow_hide_posts_for_moderators.php +++ /dev/null @@ -1,17 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Flarum\Database\Migration; -use Flarum\Group\Group; - -return Migration::addPermissions([ - 'discussion.hidePosts' => Group::MODERATOR_ID -]); diff --git a/framework/core/migrations/2018_07_21_000000_seed_default_groups.php b/framework/core/migrations/2018_07_21_000000_seed_default_groups.php new file mode 100644 index 000000000..12566ba02 --- /dev/null +++ b/framework/core/migrations/2018_07_21_000000_seed_default_groups.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Group\Group; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + $db = $schema->getConnection(); + + $groups = [ + [Group::ADMINISTRATOR_ID, 'Admin', 'Admins', '#B72A2A', 'fas fa-wrench'], + [Group::GUEST_ID, 'Guest', 'Guests', null, null], + [Group::MEMBER_ID, 'Member', 'Members', null, null], + [Group::MODERATOR_ID, 'Mod', 'Mods', '#80349E', 'fas fa-bolt'] + ]; + + foreach ($groups as $group) { + if ($db->table('groups')->where('id', $group[0])->exists()) { + continue; + } + + $db->table('groups')->insert(array_combine(['id', 'name_singular', 'name_plural', 'color', 'icon'], $group)); + } + }, + + 'down' => function (Builder $schema) { + // do nothing so as to preserve user data + } +]; diff --git a/framework/core/migrations/2015_02_25_000000_setup_default_permissions.php b/framework/core/migrations/2018_07_21_000100_seed_default_group_permissions.php similarity index 100% rename from framework/core/migrations/2015_02_25_000000_setup_default_permissions.php rename to framework/core/migrations/2018_07_21_000100_seed_default_group_permissions.php diff --git a/framework/core/src/Database/Migration.php b/framework/core/src/Database/Migration.php index 6469b847f..58f23acba 100644 --- a/framework/core/src/Database/Migration.php +++ b/framework/core/src/Database/Migration.php @@ -137,11 +137,11 @@ abstract class Migration */ public static function addPermissions(array $permissions) { - $keys = []; + $rows = []; foreach ($permissions as $permission => $groups) { foreach ((array) $groups as $group) { - $keys[] = [ + $rows[] = [ 'group_id' => $group, 'permission' => $permission, ]; @@ -149,23 +149,27 @@ abstract class Migration } return [ - 'up' => function (Builder $schema) use ($keys) { + 'up' => function (Builder $schema) use ($rows) { $db = $schema->getConnection(); - foreach ($keys as $key) { - $instance = $db->table('permissions')->where($key)->first(); - - if (is_null($instance)) { - $db->table('permissions')->insert($key); + foreach ($rows as $row) { + if ($db->table('group_permission')->where($row)->exists()) { + continue; } + + if ($db->table('groups')->where('id', $row['group_id'])->doesntExist()) { + continue; + } + + $db->table('group_permission')->insert($row); } }, - 'down' => function (Builder $schema) use ($keys) { + 'down' => function (Builder $schema) use ($rows) { $db = $schema->getConnection(); - foreach ($keys as $key) { - $db->table('permissions')->where($key)->delete(); + foreach ($rows as $row) { + $db->table('group_permission')->where($row)->delete(); } } ]; diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php index 758c1702c..b91ec48b6 100644 --- a/framework/core/src/Install/Console/InstallCommand.php +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -193,8 +193,6 @@ class InstallCommand extends AbstractCommand $this->application->register(SearchServiceProvider::class); $this->application->register(PostServiceProvider::class); - $this->seedGroups(); - $this->createAdminUser(); $this->enableBundledExtensions(); @@ -280,28 +278,6 @@ class InstallCommand extends AbstractCommand } } - protected function seedGroups() - { - Group::unguard(); - - $groups = [ - [Group::ADMINISTRATOR_ID, 'Admin', 'Admins', '#B72A2A', 'fas fa-wrench'], - [Group::GUEST_ID, 'Guest', 'Guests', null, null], - [Group::MEMBER_ID, 'Member', 'Members', null, null], - [Group::MODERATOR_ID, 'Mod', 'Mods', '#80349E', 'fas fa-bolt'] - ]; - - foreach ($groups as $group) { - Group::create([ - 'id' => $group[0], - 'name_singular' => $group[1], - 'name_plural' => $group[2], - 'color' => $group[3], - 'icon' => $group[4], - ]); - } - } - protected function createAdminUser() { $admin = $this->adminUser;