Skip to content

Commit

Permalink
factor out reAddDefaultRolesAndPermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Nov 26, 2017
1 parent 0adb7bf commit 69a5b95
Showing 1 changed file with 49 additions and 35 deletions.
84 changes: 49 additions & 35 deletions src/Bakery/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Upgrade extends BaseCommand

protected $legacyPrefix = '';

protected $lastOldDefaultPermissionId = 13;

protected function configure()
{
// the name of the command (the part after "php bakery")
Expand Down Expand Up @@ -118,42 +120,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->migrateActivities($renamedSourceTables[$this->legacyPrefix . 'user_event']);

// Re-add the default roles and permissions
$newPermissionsDictionary = [];
foreach ($this->defaultPermissions as $permission) {
$newId = DB::connection()->table('permissions')->insertGetId([
'slug' => $permission->slug,
'name' => $permission->name,
'conditions' => $permission->conditions,
'description' => $permission->description,
'created_at' => $permission->created_at,
'updated_at' => $permission->updated_at
]);

$newPermissionsDictionary[$permission->id] = $newId;
}

$newPermissionMappings = [];
foreach ($this->defaultRoles as $role) {
$newRoleId = DB::connection()->table('roles')->insertGetId([
'slug' => $role->slug,
'name' => $role->name,
'description' => $role->description,
'created_at' => $role->created_at,
'updated_at' => $role->updated_at
]);
$this->reAddDefaultRolesAndPermissions();

foreach ($role->permissions as $permission) {
$newPermissionId = $newPermissionsDictionary[$permission->id];
$newPermissionMappings[] = [
'permission_id' => $newPermissionId,
'role_id' => $newRoleId,
'created_at' => $permission->pivot->created_at,
'updated_at' => $permission->pivot->updated_at
];
}
}
// Complete installation
$command = $this->getApplication()->find('build-assets');
$command->run($input, $output);

DB::connection()->table('permission_roles')->insert($newPermissionMappings);
$command = $this->getApplication()->find('clear-cache');
$command->run($input, $output);
}

protected function migrateGroups($tableName)
Expand Down Expand Up @@ -201,7 +175,7 @@ protected function migratePermissions($tableName)
DB::connection()->table('permissions')->truncate();
DB::connection()->table('permission_roles')->truncate();

$legacyRows = DB::connection()->table($tableName)->where('id', '>', 10)->get();
$legacyRows = DB::connection()->table($tableName)->where('id', '>', $this->lastOldDefaultPermissionId)->get();

foreach ($legacyRows as $legacyRow) {
DB::connection()->table('permissions')->insert([
Expand Down Expand Up @@ -282,6 +256,46 @@ protected function migrateActivities($tableName)
}
}

protected function reAddDefaultRolesAndPermissions()
{
$newPermissionsDictionary = [];
foreach ($this->defaultPermissions as $permission) {
$newId = DB::connection()->table('permissions')->insertGetId([
'slug' => $permission->slug,
'name' => $permission->name,
'conditions' => $permission->conditions,
'description' => $permission->description,
'created_at' => $permission->created_at,
'updated_at' => $permission->updated_at
]);

$newPermissionsDictionary[$permission->id] = $newId;
}

$newPermissionMappings = [];
foreach ($this->defaultRoles as $role) {
$newRoleId = DB::connection()->table('roles')->insertGetId([
'slug' => $role->slug,
'name' => $role->name,
'description' => $role->description,
'created_at' => $role->created_at,
'updated_at' => $role->updated_at
]);

foreach ($role->permissions as $permission) {
$newPermissionId = $newPermissionsDictionary[$permission->id];
$newPermissionMappings[] = [
'permission_id' => $newPermissionId,
'role_id' => $newRoleId,
'created_at' => $permission->pivot->created_at,
'updated_at' => $permission->pivot->updated_at
];
}
}

DB::connection()->table('permission_roles')->insert($newPermissionMappings);
}

protected function mapOldTableNames($sourceTables)
{
$randomPrefix = str_random(10);
Expand Down

0 comments on commit 69a5b95

Please sign in to comment.