From 11bbcd1df706f0a096c73f3baa8e65c22211085b Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Mon, 24 Feb 2020 15:12:36 -0500 Subject: [PATCH] User country mapping; ignore unused groups #443 (#584) * Fix the user country import #443 * Ignore the old "core" groups because they're unused #443 * Properly sync roles and individual permissions #443 --- app/Services/RoleService.php | 2 +- app/Support/Countries.php | 6 +-- .../Services/Importers/GroupImporter.php | 44 +++++++++++++++++-- .../Services/Importers/UserImport.php | 11 +++-- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/Services/RoleService.php b/app/Services/RoleService.php index 1b6c0058..2fce2aca 100644 --- a/app/Services/RoleService.php +++ b/app/Services/RoleService.php @@ -39,7 +39,7 @@ class RoleService extends Service { // 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); diff --git a/app/Support/Countries.php b/app/Support/Countries.php index 47151f95..ba881039 100644 --- a/app/Support/Countries.php +++ b/app/Support/Countries.php @@ -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; } } diff --git a/modules/Importer/Services/Importers/GroupImporter.php b/modules/Importer/Services/Importers/GroupImporter.php index 07e5673e..de6f3e7f 100644 --- a/modules/Importer/Services/Importers/GroupImporter.php +++ b/modules/Importer/Services/Importers/GroupImporter.php @@ -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; @@ -66,9 +67,12 @@ class GroupImporter extends BaseImporter { $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 @@ -78,6 +82,14 @@ class GroupImporter extends BaseImporter 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], @@ -90,22 +102,46 @@ class GroupImporter extends BaseImporter // 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; } } diff --git a/modules/Importer/Services/Importers/UserImport.php b/modules/Importer/Services/Importers/UserImport.php index 6e19fbe0..6f4c8a6d 100644 --- a/modules/Importer/Services/Importers/UserImport.php +++ b/modules/Importer/Services/Importers/UserImport.php @@ -70,6 +70,7 @@ class UserImport extends BaseImporter '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, @@ -99,7 +100,6 @@ class UserImport extends BaseImporter { // 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 @@ -108,12 +108,11 @@ class UserImport extends BaseImporter 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; }