Skip to content

Commit

Permalink
Refactor and add importer to Installer module #443
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Nov 21, 2019
1 parent ea3ab21 commit 7c76ab9
Show file tree
Hide file tree
Showing 37 changed files with 1,208 additions and 772 deletions.
704 changes: 0 additions & 704 deletions app/Console/Services/Importer.php

This file was deleted.

2 changes: 2 additions & 0 deletions app/Contracts/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @property bool $skip_mutator
*
* @method static where(array $array)
* @method static updateOrCreate(array $array, array $attrs)
* @method static truncate()
*/
abstract class Model extends \Illuminate\Database\Eloquent\Model
{
Expand Down
22 changes: 0 additions & 22 deletions app/Database/migrations/2019_07_16_141152_users_add_pilot_id.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,6 @@ public function up()

// Migrate the current pilot IDs
DB::update('UPDATE `users` SET `pilot_id`=`id`');

// Drop the old ID column and add a new one
/*Schema::table('users', function (Blueprint $table) {
$table->dropPrimary('users_id_primary');
$table->dropColumn('id');
$table->string('id', Model::ID_MAX_LENGTH)->primary();
});
// Update the users to use the `pilot_id` (so we don't need to migrate data from other tables)
$users = DB::table('users')->get(['id']);
foreach ($users as $user) {
$user->id = $user->pilot_id;
$user->save();
}*/

// role_user
// permission_user
// sessions
// pireps
// bids
// news
// user_awards
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Database/seeds/permissions.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# All of the different permissions that can be assigned to roles
---
- name: admin-access
display_name: Admin Access
display_name: Administrator
description: Access the admin panel
- name: airlines
display_name: Airlines
Expand Down
28 changes: 13 additions & 15 deletions app/Http/Controllers/Admin/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@
use App\Http\Requests\UpdateRoleRequest;
use App\Repositories\PermissionsRepository;
use App\Repositories\RoleRepository;
use Flash;
use App\Services\RoleService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laracasts\Flash\Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
* Class AirlinesController
*/
class RolesController extends Controller
{
private $permsRepo;
private $rolesRepo;
private $roleSvc;

/**
* AirlinesController constructor.
*
* @param PermissionsRepository $permsRepo
* @param RoleRepository $rolesRepo
* @param $roleSvc
*/
public function __construct(PermissionsRepository $permsRepo, RoleRepository $rolesRepo)
{
public function __construct(
PermissionsRepository $permsRepo,
RoleRepository $rolesRepo,
RoleService $roleSvc
) {
$this->permsRepo = $permsRepo;
$this->rolesRepo = $rolesRepo;
$this->roleSvc = $roleSvc;
}

/**
Expand Down Expand Up @@ -145,14 +149,8 @@ public function update($id, UpdateRoleRequest $request)
return redirect(route('admin.roles.index'));
}

$this->rolesRepo->update($request->all(), $id);

// Update the permissions, filter out null/invalid values
$perms = collect($request->permissions)->filter(static function ($v, $k) {
return $v;
});

$role->permissions()->sync($perms);
$this->roleSvc->updateRole($role, $request->all());
$this->roleSvc->setPermissionsForRole($role, $request->permissions);

Flash::success('Roles updated successfully.');
return redirect(route('admin.roles.index'));
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Laratrust\Models\LaratrustPermission;

/**
* @method static firstOrCreate(array $array, array $array1)
*/
class Permission extends LaratrustPermission
{
}
1 change: 1 addition & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @method static where(string $string, $group)
* @method static firstOrCreate(array $array, array $array1)
*/
class Role extends LaratrustRole
{
Expand Down
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* @property int state
* @property bool opt_in
* @property string last_pirep_id
*
* @method static updateOrCreate(array $array, array $attrs)
* @method static where()
* @method static truncate()
* @mixin \Illuminate\Notifications\Notifiable
* @mixin \Laratrust\Traits\LaratrustUserTrait
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
use App\Repositories\SettingRepository;
use App\Services\ModuleService;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use View;

class AppServiceProvider extends ServiceProvider
{
Expand Down
21 changes: 19 additions & 2 deletions app/Services/Installer/SeederService.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ private function settingsSeedsPending(): bool
return true;
}

// See if any of these column values have changed
foreach (['name', 'description'] as $column) {
if ($row[$column] !== $setting[$column]) {
return true;
}
}

// See if any of the options have changed
if ($row->type === 'select') {
if ($row->options !== $setting['options']) {
Expand All @@ -259,10 +266,20 @@ private function permissionsSeedsPending(): bool
$yml = Yaml::parse($data);

foreach ($yml as $perm) {
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
if ($count === 0) {
$row = DB::table('permissions')
->where('name', $perm['name'])
->first();

if (!$row) {
return true;
}

// See if any of these column values have changed
foreach (['display_name', 'description'] as $column) {
if ($row[$column] !== $perm[$column]) {
return true;
}
}
}

return false;
Expand Down
47 changes: 47 additions & 0 deletions app/Services/RoleService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Services;

use App\Contracts\Service;
use App\Models\Role;
use App\Repositories\RoleRepository;

class RoleService extends Service
{
private $roleRepo;

public function __construct(RoleRepository $roleRepo)
{
$this->roleRepo = $roleRepo;
}

/**
* Update a role with the given attributes
*
* @param Role $role
* @param array $attrs
*
* @return Role
*/
public function updateRole(Role $role, array $attrs)
{
$role->update($attrs);
$role->save();

return $role;
}

/**
* @param Role $role
* @param array $permissions
*/
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;
});

$role->permissions()->sync($perms);
}
}
46 changes: 33 additions & 13 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public function __construct(
* Register a pilot. Also attaches the initial roles
* required, and then triggers the UserRegistered event
*
* @param User $user User model
* @param array $groups Additional groups to assign
* @param User $user User model
* @param array $roles List of "display_name" of groups to assign
*
* @throws \Exception
*
* @return mixed
*/
public function createUser(User $user, array $groups = null)
public function createUser(User $user, array $roles = null)
{
// Determine if we want to auto accept
if (setting('pilots.auto_accept') === true) {
Expand All @@ -66,15 +66,10 @@ public function createUser(User $user, array $groups = null)

$user->save();

// Attach the user roles
// $role = Role::where('name', 'user')->first();
// $user->attachRole($role);

// Attach any additional roles
if (!empty($groups) && is_array($groups)) {
foreach ($groups as $group) {
$role = Role::where('name', $group)->first();
$user->attachRole($role);
if (!empty($roles) && is_array($roles)) {
foreach ($roles as $role) {
$this->addUserToRole($user, $role);
}
}

Expand All @@ -87,6 +82,32 @@ public function createUser(User $user, array $groups = null)
return $user;
}

/**
* Add a user to a given role
*
* @param User $user
* @param string $roleName
*
* @return User
*/
public function addUserToRole(User $user, $roleName): User
{
$role = Role::where('name', $roleName)->first();
$user->attachRole($role);

return $user;
}

/**
* Find and return the next available pilot ID (usually just the max+1)
*
* @return int
*/
public function getNextAvailablePilotId(): int
{
return (int) User::max('pilot_id') + 1;
}

/**
* Find the next available pilot ID and set the current user's pilot_id to that +1
* Called from UserObserver right now after a record is created
Expand All @@ -101,8 +122,7 @@ public function findAndSetPilotId(User $user): User
return $user;
}

$max = (int) User::max('pilot_id');
$user->pilot_id = $max + 1;
$user->pilot_id = $this->getNextAvailablePilotId();
$user->save();

Log::info('Set pilot ID for user '.$user->id.' to '.$user->pilot_id);
Expand Down
21 changes: 13 additions & 8 deletions intellij_style.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<code_scheme name="Laravel" version="173">
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="USE_TAB_CHARACTER" value="true" />
</value>
</option>
<code_scheme name="phpVMS" version="173">
<PHPCodeStyleSettings>
<option name="ALIGN_KEY_VALUE_PAIRS" value="true" />
<option name="ALIGN_PHPDOC_PARAM_NAMES" value="true" />
<option name="ALIGN_PHPDOC_COMMENTS" value="true" />
<option name="CONCAT_SPACES" value="false" />
<option name="COMMA_AFTER_LAST_ARRAY_ELEMENT" value="true" />
<option name="PHPDOC_KEEP_BLANK_LINES" value="false" />
<option name="PHPDOC_BLANK_LINE_BEFORE_TAGS" value="true" />
<option name="PHPDOC_BLANK_LINES_AROUND_PARAMETERS" value="true" />
<option name="PHPDOC_WRAP_LONG_LINES" value="true" />
<option name="THROWS_WEIGHT" value="1" />
<option name="RETURN_WEIGHT" value="2" />
<option name="LOWER_CASE_BOOLEAN_CONST" value="true" />
<option name="LOWER_CASE_NULL_CONST" value="true" />
<option name="ELSE_IF_STYLE" value="COMBINE" />
Expand All @@ -21,11 +20,17 @@
<option name="SPACE_AFTER_UNARY_NOT" value="true" />
<option name="SPACES_WITHIN_SHORT_ECHO_TAGS" value="false" />
<option name="FORCE_SHORT_DECLARATION_ARRAY_STYLE" value="true" />
<option name="PHPDOC_USE_FQCN" value="true" />
</PHPCodeStyleSettings>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="JavaScript">
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="PHP">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
Expand Down
1 change: 1 addition & 0 deletions modules/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/*/
!.gitignore
!/Awards
!/Importer
!/Installer
!/Sample
!/Vacentral
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace App\Console\Commands;
namespace Modules\Installer\Console\Commands;

use App\Contracts\Command;
use Modules\Installer\Services\Importer\Importer;

class ImportFromClassic extends Command
class ImportFromClassicCommand extends Command
{
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?} {table_prefix=phpvms_}';
protected $description = 'Import from an older version of phpVMS';
Expand All @@ -22,7 +23,7 @@ public function handle()
'table_prefix' => $this->argument('table_prefix'),
];

$importerSvc = new \App\Console\Services\Importer($db_creds);
$importerSvc->run();
$importerSvc = new Importer();
$importerSvc->run($db_creds);
}
}
8 changes: 8 additions & 0 deletions modules/Installer/Contracts/ImporterContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Modules\Installer\Contracts;

interface ImporterContract
{
public function run();
}
Loading

0 comments on commit 7c76ab9

Please sign in to comment.