Skip to content

Commit

Permalink
Merge pull request #57 from bobbybouwmann/roles
Browse files Browse the repository at this point in the history
Convert Bolt3 roles to Bol5 roles
  • Loading branch information
bobdenotter authored Feb 11, 2022
2 parents c0db380 + 55945ce commit 26b9a96
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,11 @@ private function importUsers(array $data): void

$user = new User();

$roles = $importUser->get('roles');

// Bolt 3 fallback
if (! in_array('ROLE_USER', $roles, true) && ! in_array('ROLE_EDITOR', $roles, true)) {
$roles[] = 'ROLE_EDITOR';
}

$user->setDisplayName($importUser->get('displayName', $importUser->get('displayname')));
$user->setUsername($importUser->get('username'));
$user->setEmail($importUser->get('email'));
$user->setPassword($importUser->get('password'));
$user->setRoles($roles);
$user->setRoles($this->parseRoles($importUser->get('roles')));
$user->setLocale($importUser->get('locale', 'en'));
$user->setBackendTheme($importUser->get('backendTheme', 'default'));
$user->setStatus($importUser->get('status', ($importUser->get('enabled') ? 'enabled' : 'disabled')));
Expand Down Expand Up @@ -523,4 +516,29 @@ private function getMultipleValues(array $item): array

return $result;
}

private function parseRoles(array $roles = []): array
{
$mapping = [
'root' => 'ROLE_ADMIN',
'admin' => 'ROLE_ADMIN',
'chief-editor' => 'ROLE_CHIEF_EDITOR',
'editor' => 'ROLE_EDITOR',
'developer' => 'ROLE_DEVELOPER',
'everyone' => 'ROLE_USER',
];

foreach ($roles as $key => $role) {
if (array_key_exists($role, $mapping)) {
$roles[$key] = $mapping[$role];
}
}

// Bolt 3 fallback
if (empty(array_intersect(['ROLE_USER', 'ROLE_EDITOR'], $roles))) {
$roles[] = 'ROLE_EDITOR';
}

return array_unique($roles);
}
}

0 comments on commit 26b9a96

Please sign in to comment.