Skip to content

Commit

Permalink
User country mapping; ignore unused groups #443 (#584)
Browse files Browse the repository at this point in the history
* Fix the user country import #443

* Ignore the old "core" groups because they're unused #443

* Properly sync roles and individual permissions #443
  • Loading branch information
nabeelio committed Feb 24, 2020
1 parent 342fe31 commit 11bbcd1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/Services/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setPermissionsForRole(Role $role, array $permissions)
{
// Update the permissions, filter out null/invalid values
$perms = collect($permissions)->filter(static function ($v, $k) {
return $v;
return !empty($v);
});

$role->permissions()->sync($perms);
Expand Down
6 changes: 2 additions & 4 deletions app/Support/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class Countries
/**
* Get a select box list of all the countries
*
* @return static
* @return \Illuminate\Support\Collection
*/
public static function getSelectList()
{
$countries = collect((new ISO3166())->all())
return collect((new ISO3166())->all())
->mapWithKeys(static function ($item, $key) {
return [strtolower($item['alpha2']) => $item['name']];
});

return $countries;
}
}
44 changes: 40 additions & 4 deletions modules/Importer/Services/Importers/GroupImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Modules\Importer\Services\Importers;

use App\Models\Permission;
use App\Models\Role;
use App\Services\RoleService;
use Modules\Importer\Services\BaseImporter;
Expand Down Expand Up @@ -66,9 +67,12 @@ public function run($start = 0)
{
$this->comment('--- ROLES/GROUPS IMPORT ---');

/** @var \App\Services\RoleService $roleSvc */
$roleSvc = app(RoleService::class);
$permMappings = $this->getPermissions();

$count = 0;
$permCount = 0;
$rows = $this->db->readRows($this->table, $this->idField, $start);
foreach ($rows as $row) {
// Legacy "administrator" role is now "admin", just map that 1:1
Expand All @@ -78,6 +82,14 @@ public function run($start = 0)
continue;
}

// Map the "core" roles, which are active/inactive pilots to a new ID of
// -1; so then we can ignore/not add these groups, and then ignore them
// for any of the users that are being imported. these groups are unused
if ($row->core === 1 || $row->core === '1') {
$this->idMapper->addMapping('group', $row->groupid, -1);
continue;
}

$name = str_slug($row->name);
$role = Role::firstOrCreate(
['name' => $name],
Expand All @@ -90,22 +102,46 @@ public function run($start = 0)
// Add all of the ones which apply, and then set them on the new role
$permissions = [];
foreach ($this->legacy_permission_set as $legacy_name => $mask) {
if (($row->permissions & $mask) === true) {
$val = $row->permissions & $mask;
if ($val === $mask) {
// Map this legacy permission to what it is under the new system
if (!array_key_exists($legacy_name, $this->legacy_to_permission)) {
continue;
}

$permissions[] = $this->legacy_to_permission[$legacy_name];
// Get the ID of the permission
$permissions[] = $permMappings[$this->legacy_to_permission[$legacy_name]];
}
}

$roleSvc->setPermissionsForRole($role, $permissions);
if (count($permissions) > 0) {
$roleSvc->setPermissionsForRole($role, $permissions);
$permCount += count($permissions);
}

if ($role->wasRecentlyCreated) {
$count++;
}
}

$this->info('Imported '.$count.' ranks');
$this->info('Imported '.$count.' roles, synced '.$permCount.' permissions');
}

/**
* Get all of the permissions from locally and return a kvp with the
* key being the permission short-name and the value being the ID
*
* @return array
*/
private function getPermissions(): array
{
$mappings = [];
$permissions = Permission::all();
/** @var \App\Models\Permission $p */
foreach ($permissions as $p) {
$mappings[$p->name] = $p->id;
}

return $mappings;
}
}
11 changes: 5 additions & 6 deletions modules/Importer/Services/Importers/UserImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function run($start = 0)
'rank_id' => $rank_id,
'home_airport_id' => $row->hub,
'curr_airport_id' => $row->hub,
'country' => $row->location,
'flights' => (int) $row->totalflights,
'flight_time' => Time::hoursToMinutes($row->totalhours),
'state' => $state,
Expand Down Expand Up @@ -99,7 +100,6 @@ protected function updateUserRoles(User $user, $old_pilot_id)
{
// Be default add them to the user role, and then determine if they
// belong to any other groups, and add them to that
$roleMappings = [];
$newRoles = [];

// Figure out what other groups they belong to... read from the old table, and map
Expand All @@ -108,12 +108,11 @@ protected function updateUserRoles(User $user, $old_pilot_id)
foreach ($old_user_groups as $oldGroup) {
$newRoleId = $this->idMapper->getMapping('group', $oldGroup->groupid);

// Only lookup a new role ID if found
// if (!in_array($newRoleId, $roleMappings)) {
// $roleMappings[$newRoleId] = Role::where(['id' => $newRoleId])->first();
// }
// This role should be ignored
if ($newRoleId === -1) {
continue;
}

// $newRoles[] = $roleMappings[$newRoleId];
$newRoles[] = $newRoleId;
}

Expand Down

0 comments on commit 11bbcd1

Please sign in to comment.