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
This commit is contained in:
Nabeel S 2020-02-24 15:12:36 -05:00 committed by GitHub
parent 342fe31ceb
commit 11bbcd1df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 15 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}