mirror of
https://github.com/flarum/framework.git
synced 2024-12-03 23:53:43 +08:00
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.
This commit is contained in:
parent
5aa15f5b27
commit
a13175600f
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* 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
|
||||
]);
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* 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
|
||||
}
|
||||
];
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user